473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

different struct

what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;

================================
struct {

.... etc

} SetupRecord;
Mar 11 '08 #1
5 1352
Jim Johnson wrote:
what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;
This defines a type in C.
================================
struct {

.... etc

} SetupRecord;
this does not.

Only use the first form if you are sharing types between C and C++.

The second form is unusual, use

struct SetupRecord {...};

--
Ian Collins.
Mar 11 '08 #2
So there are 2 ways to declare a struct

1) C way of declaring
typedef struct {

.... etc

} SetupRecord;
2) C++ way of declaring a struct
struct SetupRecord {

.... etc

} ;
may I correct?

and also the following is incorrect synatx to declare a struct
struct {

.... etc

} SetupRecord;
Mar 11 '08 #3
On Mon, 10 Mar 2008 21:26:12 -0800, Jim Johnson <ao*******@yahoo.com>
wrote in comp.lang.c++:
So there are 2 ways to declare a struct

1) C way of declaring
No, this is not declaring anything, this is defining a type, and also
creating an alias for it.
typedef struct {

.... etc

} SetupRecord;
This is also perfectly valid C++. It defines a structure type with no
name, and defines SetupRecord as an alias for that type. This is
actually not a particularly good idea in either language.
2) C++ way of declaring a struct
Again, your sample below does not declare a struct, it defines a
struct type.
struct SetupRecord {

.... etc

} ;
This is perfectly valid C does as well. In both languages, it defines
a structure type named SetupRecord. In either language, the type can
be referred to by the two-word phrase "struct SetupRecord". In C++
only, it can also be referred to merely as SetupRecord.
may I correct?

and also the following is incorrect synatx to declare a struct
Your terminology is still incorrect. Your snippet below defines a
structure type, and also defines an object of that structure type.
struct {

.... etc

} SetupRecord;
You have defined an unnamed structure type, and an instance of that
type named SetupRecord. In this situation, SetupRecord is the name of
a single object of this type, and not of the structure type itself.

It is perfectly valid code, and this object can be accessed anywhere
within its scope. Compile and execute the following example:

#include <iostream>

struct { int x; int y; } ss;

void show_me()
{
std::cout << ss.x << " + " << ss.y << " = "
<< (ss.x + ss.y) << std::endl;
}

int main()
{
ss.x = 2;
ss.y = 3;
show_me();
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Mar 11 '08 #4
Jim Johnson <ao*******@yahoo.comwrote:
>what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;
This defines SetupRecord to be a type, which corresponds to this
structure. You can define other items of type SetupRecord:

SetupRecord xyz;

"xyz" is now a variable of type SetupRecord.
>================================
struct {

.... etc

} SetupRecord;
This defines a variable named SetupRecord. It's a structure whose
properties you've just defined.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Mar 11 '08 #5
Ian Collins wrote:
Jim Johnson wrote:
>what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;
This defines a type in C.
>================================
struct {

.... etc

} SetupRecord;

this does not.

Only use the first form if you are sharing types between C and C++.

The second form is unusual, use

struct SetupRecord {...};
The latter doesn't define a type at all, it defines a variable of
unnamed type called SetupRecord.

Mar 12 '08 #6

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

Similar topics

2
by: learning_C++ | last post by:
Hi, I found some example code about server and client applications. But I can not run server and client in different computers. Now, I only run them in the different windows in Linux. I hope to...
10
by: Xavier Noria | last post by:
Given two structures of the same size but different type, does C99 guarantee that pointers to them can be casted one to each other, and that the order of the elements will be kind of respected? ...
82
by: zardoz | last post by:
I've got this problem: unsigned long long lTemp; char cLargeNum="1324567890"; sscanf(clargeNum,"%llu",&lTemp); which under Win32 isn't working*. My program needs to compile under posix so...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
3
by: Scott Langham | last post by:
Hi, can anybody help please! I've got an app with a few dlls. When I run the app I get the following message in a message box: The procedure entry point...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
8
by: nobrow | last post by:
Okay ... Im out of practice. Is it not possible to have a 2D array where each column is of a different type, say an int and a struct*? The following seg faults for me and I cant figure out what I...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
16
by: Stefano Sabatini | last post by:
Hi all, I'm facing this design problem. I have a table which defines the behaviour of an Object in a given state according to the type of event it is receiving. So for each couple...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.