473,471 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

typedef-ing a struct compared to not typedef-ing a struct.

Markus
6,050 Recognized Expert Expert
Are there any major differences between the following, apart from syntactically:

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     /** Struct members */
  3. } stack;
  4.  
  5. struct sstack {
  6.     /** Struct members */
  7. } stack;
  8.  
Thanks,
Mark.
Oct 24 '09 #1
6 2081
donbock
2,426 Recognized Expert Top Contributor
In the first case, "stack" is the name of a type; in the second case, "stack" is the name of a variable.

If you intended to compare these cases:
Expand|Select|Wrap|Line Numbers
  1. struct sstruct {
  2.     ....
  3. };
  4. struct sstruct stack1;
  5.  
  6. typedef struct sstruct stack_t;
  7. stack_t stack2;
Then I believe there is no meaningful or useful difference; but I'm eager to see if somebody else is aware of one.
Oct 24 '09 #2
Markus
6,050 Recognized Expert Expert
@donbock
Ah. My bad. I was looking at it wrong - when I glanced over it, I thought 'stack' was a variable in both cases. Now I see that it isn't.
Oct 24 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
Remember in C++ you don't need to typedef at all the name of the srtuct is automatically defined as a type

Expand|Select|Wrap|Line Numbers
  1. struct sstack {
  2.     /** Struct members */
  3. };
  4.  
  5. sstack stack;
  6.  
Valid C++, not valid C.
Oct 24 '09 #4
Markus
6,050 Recognized Expert Expert
@Banfa
Oh? I'm sure the book, on C, I was reading just did:
Expand|Select|Wrap|Line Numbers
  1. struct struct_name {
  2.     /** ... */
  3. };
  4.  
as a way of defining the struct. I cannot check now as I've taken it back to the library.

I guess VSC++ never picked that up because it doesn't know whether I'm using C++ or C. Speaking of which, is there a way to get VS to work specifically with C? Furthermore, does it matter if I write C code for a C++ compiler?
Oct 25 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
It is not the declaration of the structure but the declaration of the variable following that is valid in C++ and not C
Expand|Select|Wrap|Line Numbers
  1. sstack stack;
Many C++ compilers also compile C code. Normally the decision on which language to use is based on the file extension .c = C, .cpp and .cxx = C++. However some compilers include switches to force the compilation to C or C++ (something I found particularly useful when I wanted to compile some auto generated C files as C++.

VSC++ and gnu gcc both have switches like this but you will have to check your compiler documentation to find out what they are.
Oct 25 '09 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
typedef struct {
/** Struct members */
} stack;

struct sstack {
/** Struct members */
} stack;

There is a huge difference between these two lines of code.

The typedef says that a struct with those members is a type. However, the typedef does not create a stack object. So if you use the typedef to chaange stack members you get program crashes becuse there is no stack variable.
Oct 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
0
by: Peter Demeyer | last post by:
I have this problem: 'pno' is a pointer to a struct of type PRINTER_NOTIFY_OPTIONS. This struct is holding another struct called 'not' (of type PRINTER_NOTIFY_OPTIONS_TYPE) which contains a couple...
3
by: monkeydragon | last post by:
hi, i am making a struct within a struct like this.. struct upper { struct lower{ BYTE* row; UINT width_row; } }
0
by: Alvaro Enriquez de Luna | last post by:
Hello everybody. I am trying to use in C# a dll developed in C. I am founding problems with C structs. While my C struct does not include anything related with char or char *, everyting works ok,...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
5
by: karthikbalaguru | last post by:
Hi, struct sky { int stars; struct ocean *oceanptr; }; struct ocean { int waves; struct sky *skyptr;
3
by: bernd | last post by:
Hi folks, the code below "runs" with the following message: Before structure assignment Bus Error(coredump) What I am trying to do is to define an "outer" structure containing an inner one...
1
by: joohoo | last post by:
here is my question. I have two structs, one holds a bunch of info in it. it is passed to the function by reference and i want to paste it into another struct as a member of it. ex.
5
by: emmanuel.rivoire | last post by:
Hello, I spent almost a week to be able to embed Python within my C++ game engine. I wrote a mini-tutorial of what I was able to do so far here :...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.