Jump to content

Struct in C and C++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
Is struct in C and C++ the same?

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts

dirkfirst said:

Is struct in C and C++ the same?

No they are not

#3
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Errm, you use them exactly the same way:

C:

typedef struct blabla
{
int x;
char z;
}

c++:
typedef struct blabla
{
int x;
char z;
}

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Well, accually let me correct myself.
In some compilers ( Turbo C for example ), all C struct operations and declares must begin with `struct`:

typedef struct MNT_DEV{
byte oport;
byte iport;
};

void main(void)
{
struct MNT_DEV mnt_pt;

struct mnt_pt.iport = 0x02; /* Normally mnt_pt.iport = 0x02; would be all you need */

}

But other than that you use them the exact same way.

#5
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
So they are the same or they are not? lol

#6
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
yes, the only differnce is that sometimes c compilers will require you put the keyword "struct" infront of all structure operations, ie:
struct header.i = 0; //Works
header.i = 0; //Nope, no worky


#7
Guest_Kaabi_*

Guest_Kaabi_*
  • Guests
I'd always thought the structure was the same, and it appears I was right, although I didn't know about that "struct" thing.

#8
kia

kia

    Newbie

  • Members
  • PipPip
  • 24 posts
hi according to what i undrestand from the link jonh gave us there should a difference btw structs in C and c++


structs don't copy in C. That is, if a and b are structs then the line a = b; doesn't work. Nor will structs be fed into functions as arguments. The only way to deal with them sensibly is to use pointers to them, or to write functions to copy their elements explicitly

from :Differences between C and C++

#9
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Please don't bump years-old threads, kia!

#10
kia

kia

    Newbie

  • Members
  • PipPip
  • 24 posts
ok but tel me make things clear first i didnt do this by porpose if i choosed these topic it is because i m actualy taking some course in struct i mean we reach the chapter about struct so this why anyway sorry and i didnt know that i may get warnig for this so sory then for this mistake :( :(
hope my sory ll be accepted