473,805 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why use this struct format?

I see this all the time in code:
typedef struct a_struct
{
...
}differentName, *differentNameP tr;
I understand how I can use it, but could someone tell me why the above is
superior to:

struct differentName
{
...
};

It seems that if you're creating the struct and giving it an alternate name
immediately (and never using it's original name), then why not just give it
the alternate name?
I also don't understand what you're saving by declaring differentNamePt r as
opposed to just using differentName *. differentName * actually seems more
clear to me when reading code.
Be as brutal as need be to get the point across, I'm ready for it.
Thanks,

-Kilana

Jul 19 '05 #1
21 4658
Kilana wrote:

It seems that if you're creating the struct and giving it an alternate name
immediately (and never using it's original name), then why not just give it
the alternate name?


It's a holdover from C, when the syntax in your second example was not
permitted.

--
Mike Smith

Jul 19 '05 #2
Mike Smith <mi************ *******@acm.DOT .org> wrote in
news:vm******** ****@news.super news.com:
Kilana wrote:

It seems that if you're creating the struct and giving it an
alternate name immediately (and never using it's original name), then
why not just give it the alternate name?


It's a holdover from C, when the syntax in your second example was not
permitted.

--
Mike Smith


Ohhhh,

Thanks :), I've been wondering about that for years, but never asked.
-K

Jul 19 '05 #3

"Kilana" <kp@...> wrote in message news:nh******** **********@fe03 .atl2.webusenet .com...
I see this all the time in code:
typedef struct a_struct
{

Carry over from C.
Jul 19 '05 #4
Kilana wrote:
I see this all the time in code:
typedef struct a_struct
{
...
}differentName, *differentNameP tr;


It's a leftover from C. In C, giving the above definition, the following
rules apply:
a_struct a; // ERROR, not allowed in C, but allowed in C++
struct a_struct b; // That's alright in both C++ and C
differentName c; // Also good

So the typedef was in place to prevent typing 'struct' before every
definition. Of course, since 'a_struct a;' is alright in C++, this
construction is obsolete. But you still see it, either from C people that
are still in the habit, or in headers that are for both C and C++ (like the
Windows Platform SDK headers).

The same thing is true for enum.

--
Unforgiven

"Most people make generalisations "
Freek de Jonge

Jul 19 '05 #5
"Mike Smith" <mi************ *******@acm.DOT .org> wrote in message
news:vm******** ****@news.super news.com...
Kilana wrote:

It seems that if you're creating the struct and giving it an alternate name immediately (and never using it's original name), then why not just give it the alternate name?


It's a holdover from C, when the syntax in your second example was not
permitted.

--
Mike Smith

The second example

struct a_struct {
/* ... */
}

is so permitted in C. The difference is that in C you cannot use

a_struct x;

to define a variable of this type, but instead must say

struct a_struct x;

so in C the typedefs make it more convenient to use the struct. But you are
right that the use of this in C++ is probably due to C programming habits.

--
Gregg
Jul 19 '05 #6
Kilana wrote:

I see this all the time in code:

typedef struct a_struct
{
...
}differentName, *differentNameP tr;

This is required in C, but not C++. People sometimes use it to promote
cross-language compatibility, or because they don't know better.
I understand how I can use it, but could someone tell me why the above is
superior to:

struct differentName
{
...
};


It's not. Use this form for C++. However, you should really ask why you
are using a struct at all. In most cases, it is recommended to use
classes only.

Brian Rodenborn
Jul 19 '05 #7
Default User wrote:
However, you should really ask why
you are using a struct at all. In most cases, it is recommended to use
classes only.


structs *are* classes. The *only* difference is their default protection
level. struct members are public by default and structs inherit other class
types public by default. This is the _only_ difference. Please do not
spread fals information here. The use of the class keyword over the struct
keyword is only a matter of style or preference. The language has
absolutely no preference for one over the other:

class foo {
// private elements
public:
// public interface
};
Some prefer (eg: for easy reading) writing it as:

struct foo {
// public interface first, no scrolling required
private:
// internals
};

Some write it like

class foo {
public:
// public interface first, no scrolling required
private:
// internals
};

The only - sort of - argument to support the 3rd form is to avoid confusion
of those (like you) who think struct is not a class type.

--
WW aka Attila
Jul 19 '05 #8
White Wolf wrote:

Default User wrote:
However, you should really ask why
you are using a struct at all. In most cases, it is recommended to use
classes only.
structs *are* classes.


No kidding.
The *only* difference is their default protection
Really?
struct members are public by default and structs inherit other class
types public by default.
No kidding.
This is the _only_ difference.
Whoop-de-do. It's a BIG difference.
Please do not
spread fals information here.
I'm not spreading false information, I'm expressing an opinion. Newbies
should not be using structs, with their default public behavior. They
should be using classes, with default private, so they learn proper
handling of member data. The tendency for newbies is to use structs just
like C-style ones, with direct access to the attributes. That's not the
way they should be starting out.
The only - sort of - argument to support the 3rd form is to avoid confusion
of those (like you) who think struct is not a class type.


It's amazing how you can determine what I do and do not know. You happen
to be extraordinarily wrong.

Just admit you're still crying about that previous thread. You saw what
you thought was an opportunity to show me up. Well, you was wrong bucko.

Brian Rodenborn
Jul 19 '05 #9
Default User wrote:
White Wolf wrote:

Default User wrote:
However, you should really ask why
you are using a struct at all. In most cases, it is recommended to
use classes only.
This is the _only_ difference.


Whoop-de-do. It's a BIG difference.


No, it is not. Bjarne Stroustrup, The C++ Programming Language Spec. Ed.:

"By definition a struct is a class..."

Breaking here. Your sentences (see above) suggest to the poor newbie that
structs are *different* from classes. This is false information. The
creator of the language says otherwise:

"
By definition a struct is a class in which members are by default public;
that is,

struct s { ...

is simply a shorthand for

class s { public: ...
"

Please do not
spread fals information here.


I'm not spreading false information, I'm expressing an opinion.


You are spreading the false information that classes are preferred over
struct. Struct *is* a class. Did you say "the use of the class keyword is
preferred over the use of the struct keyword when defining a class type in
C++" that would have been a completely different statement. And it would
even become "expressing of an opinion" had it been starting with IMHO, IMO,
"I think", "My opinion is" etc.
Newbies should not be using structs, with their default public
behavior.
You think...
They should be using classes, with default private,
You are still confused: structs are classes. You mean they should use the
class keyword for their class declarations instead of the struct keyword.
so they learn proper handling of member data.
I see. I do not believe that it will in any way help in that but I can
accept your opinion. (I do not believe because I have seen many people to
use C++, they did not know that they can use struct to define classes and
still all their members were public.)
The tendency for newbies is to use structs
just like C-style ones, with direct access
to the attributes.
You mean newbies coming from C. Since newbies using C++ will learn about
classes and struct and on the same page of a decent book they will learn
about not to make everything public.
That's not the way they should be starting out.
I have just too much bad experiences to say that it has nothing to do with
using the struct or the class keyword.
The only - sort of - argument to support the 3rd form is to avoid
confusion of those (like you) who think struct is not a class type.


It's amazing how you can determine what I do and do not know. You
happen to be extraordinarily wrong.


Even in your reply you say _again_:
"Newbies should not be using structs... They should be using classes"

So what can you assume would this statement? That you are not aware of the
fact that types defined by the struct keyword *are* classes. Since you say
that instead of them newbies have to use classes. But they *are* classes.
Just admit you're still crying about that previous thread.
You saw what you thought was an opportunity to show me up.
Well, you was wrong bucko.


You should really need to that medication. You are getting worse.

I do not know why do you think that you are a bit important for me to
remember you or any thread of yours. When I reply in a thread I look at the
content of the post and not the poster. I suggest you start doing the same.

BTW start to get used to the fact that you are not that important or
extraordinary that people will just reply to your posts because you posted
them. At least I don't.

--
WW aka Attila
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3567
by: Grant Edwards | last post by:
Perhaps I'm doing something wrong: the struct module docs say it's IEE 754, but I can't figure out how to get it to handle NaN values correctly (either packing or unpacking). >>> x = float('nan') >>> struct.pack("<f",x) Traceback (most recent call last): File "<stdin>", line 1, in ? SystemError: frexp() result out of range
0
2305
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It includes support to pack and unpack the c types: byte, char, short, long, long long, char, *, and...
5
13870
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD' # the above was printed using repr(xbuffer). # Note that int(0x13) = 19 which is exactly the length of the visible text #
2
3697
by: Sergey Dorofeev | last post by:
I can use string.unpack if string in struct uses fixed amount of bytes. But is there some extension to struct modue, which allows to unpack zero-terminated string, size of which is unknown? E.g. such struct: long, long, some bytes (string), zero, short, short, short.
3
2707
by: Chandu | last post by:
In using the following struct format I get the size as 593. The same C struct is 590 if packed on byte boundary and 596 when using pragma pack(4). I am using pack(4) and added 3 spares at the end to get by. hdrFormat = '16s 32s 32s B 8s H 8s H 4s H H 20s 64s 64s 64s 32s 32s 64s L L B B B B B 64s 64s' Any ideas on what I am doing wrong?
3
1537
by: Jérôme de Lagausie | last post by:
Hello, In one hand, I've got a set of more than 130 different structures, mostly rather simple (a set of simple data members such as int, long, char , float ...) sample : #ifndef _H2_H_ #define _H2_H_
4
5414
by: Abubakar | last post by:
Hi, I am writing a server in C# and client in C++ (pure, not managed). The communication goes on through sockets. In C# I am using NetworkStream to send text data (by converting it to byte array) to the c++ client. In c++ client I have "recv" (winsock) function through which I receive everything. Now there is a need to pass a "struct" of C# through the "write" function of network stream. I want to know how this can be done. Also I would...
10
2281
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it for non-trivial tasks, I always happen to write small wrapper functions to adjust the values returned by struct. An example API would be the following: ============================================
3
4845
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack str size does not match format I thought ip addresses were unsigned 32 bit integers.
2
10786
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible documentation over known issues with Python 2.4.1 so I try this... Here's the thing:
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.