Copying from one struct to another, simple assignment?

C

C. J. Clegg

Can I do this?

typedef struct _aStruct
{
int a;
long b;
char c;
} MYSTRUCT;

MYSTRUCT foo;
MYSTRUCT bar;

foo.a = 1;
foo.b = 2L;
foo.c = 3;

bar = foo; // does this copy all elements of foo
// into corresponding elements of bar?

I seem to recall that this works in C++ but not (necessarily) in C.
Correct?
 
E

Eric Sosman

C. J. Clegg said:
Can I do this?

typedef struct _aStruct
{
int a;
long b;
char c;
} MYSTRUCT;

MYSTRUCT foo;
MYSTRUCT bar;

foo.a = 1;
foo.b = 2L;
foo.c = 3;

bar = foo; // does this copy all elements of foo
// into corresponding elements of bar?

Yes. Padding (if there is any) may or may not be copied,
but all the elements you can refer to are.
I seem to recall that this works in C++ but not (necessarily) in C.
Correct?

Sorry; I don't know what C++ does.
 
J

James Kuyper

C. J. Clegg said:
Can I do this?

typedef struct _aStruct
{
int a;
long b;
char c;
} MYSTRUCT;

MYSTRUCT foo;
MYSTRUCT bar;

foo.a = 1;
foo.b = 2L;
foo.c = 3;

bar = foo; // does this copy all elements of foo
// into corresponding elements of bar?

I seem to recall that this works in C++ but not (necessarily) in C.
Correct?

False. It works in both languages. In fact, making code like that work
in C++ the same way it does in C was one of the key objectives when C++
was designed. However, in both languages it's simpler to write

MYSTRUCT foo = {1, 2L, 3};

C99 adds some additional features:

// Compound literals:
bar = (MYSTRUCT){1, 2L, 3};

// Designated initializers:
bar = (MYSTRUCT){b=2L, c=3, a=1};
 
L

luser-ex-troll

False. It works in both languages. In fact, making code like that work
in C++ the same way it does in C was one of the key objectives when C++
was designed. However, in both languages it's simpler to write

MYSTRUCT foo = {1, 2L, 3};

C99 adds some additional features:

// Compound literals:
bar = (MYSTRUCT){1, 2L, 3};

// Designated initializers:
bar = (MYSTRUCT){b=2L, c=3, a=1};

itym
bar = (MYSTRUCT){.b=2L, .c=3, .a=1};
 
M

Mark Wooding

C. J. Clegg said:
MYSTRUCT foo;
MYSTRUCT bar; [...]
bar = foo; // does this copy all elements of foo
// into corresponding elements of bar?

That's fine. It's explicitly permitted under the constraints of C90
6.3.16.1.

-- [mdw]
 
K

Keith Thompson

C. J. Clegg said:
Can I do this?

typedef struct _aStruct
{
int a;
long b;
char c;
} MYSTRUCT;
[...]

(As others have said, yes, C supports struct assignment.)

You should avoid using identifiers with leading underscores. If you
want to check the standard or a good reference, you can find out
exactly which forms are permitted in which circumstances, but it's
easier just to avoid declaring such identifiers altogether.

If you're going to declare two names for a struct type, there's no
need to use two different identifiers. For example, this is perfectly
legal:

typedef struct foo {
int a;
} foo;

The type now has two names, "struct foo" (the struct keyword followed
by the tag name) and "foo" (the typedef name). Since struct tags are
in their own namespace, they aren't going to conflict with anything
else.

If you want to use distinct identifiers for some reason (doing so
might make things easier in some programming environments), you can
establish some consistent convention to avoid the confusion of coming
up with two different names for the same thing (in your case,
"_aStruct" and "MYSTRUCT"):

typedef struct foo_s {
int a;
} foo;

Or you might consider just dropping the typedef altogether, since it
doesn't really add any value:

struct foo {
int a;
};

struct foo obj;

The latter is a matter of some controversy; plenty of people think
that the benefit of having a one-word name for the type is worth the
effort of adding a typedef.
 
J

James Kuyper

luser-ex-troll said:
itym
bar = (MYSTRUCT){.b=2L, .c=3, .a=1};

I knew I should have done a quick compiler test; but my wife was
calling, so I posted it without testing.

I'm still not authorized to use C99 at work, so I have a lot less
practical experience with it than I'd like.
 
C

CBFalconer

Anthony said:
CBFalconer wrote:
.... snip ...


And yet the = (assignment operator) somehow does.

No it doesn't. It just copies one struct, complete with padding,
to another. It doesn't care what's in the padding bytes. == does
care.
 
F

Flash Gordon

CBFalconer said:
No it doesn't.

No, it might or might not, and it might be different on different lines
in the same source file.
It just copies one struct, complete with padding,
to another.

NO. It is explicitly allowed to NOT copy the padding. Or it could
generate completely new bit-patterns for the copied structure which are
nothing like the original or what was in the variable before.

Many implementation *might* (and probably do) copy including the padding
but it is not required.
It doesn't care what's in the padding bytes.

True(ish) which is why it is allowed to NOT copy it.
== does
care.

Surely you mean that if it was implemented then == would have to avoid
comparing the padding bytes (i.e. care where they are but NOT care what
is in them).
 
C

CBFalconer

Flash said:
NO. It is explicitly allowed to NOT copy the padding.

You are wrong. There is NO padding at the beginning of a structure,
so there is nothing to copy there. That is the kind of padding I
was discussing. Read the standard.
 
F

Flash Gordon

CBFalconer said:
You are wrong. There is NO padding at the beginning of a structure,
so there is nothing to copy there. That is the kind of padding I
was discussing. Read the standard.

Now tell me where I mentioned the beginning of the of the structure? I
can't see it in what you quote, can't remember it and can't see it in
the log of my posted message.

You said that structure assignment copies the padding, you were wrong.
Where the padding is has absolutely nothing to do with it and was not
under discussion.
 
C

CBFalconer

Flash said:
You said that structure assignment copies the padding, you were wrong.

No, I am right. Check the standard. Structure padding alternates 1
and 0 bits. It HAS to be copied. But there is NO structure padding
at the beginning of a structure. Some languages, such as Pascal, don't
even HAVE C's structure utility.
 
B

Ben Bacarisse

Richard Heathfield said:
CBFalconer said:


That was not evident from your "discussion".

I am pretty sure the message is a fake (along with several others).
There are several things that point to it being a fake, but I'd rather
not say exactly what these are since that will just facilitate better
fakes in the future.

Of course, your message may be a fake too, as might this one, but if
so they are both better than CBF fake!
 
M

Mark Wooding

CBFalconer said:
No, I am right. Check the standard.

Chapter and verse would be nice at this point. I don't recall it
mentioning you by name at all. (Sorry, cheap shot.)
Structure padding alternates 1 and 0 bits. It HAS to be copied.

This is an astonishing and outlandish claim, contradicted by (for
example) 6.2.6.1p6. If you disagree, chapter and verse, please.
But there is NO structure padding at the beginning of a structure.

This is indeed correct.
Some languages, such as Pascal, don't even HAVE C's structure utility.

There are record types in Pascal, which are pretty similar.

-- [mdw]
 
W

Willem

Mark Wooding wrote:
)> No, I am right. Check the standard.
)
) Chapter and verse would be nice at this point. I don't recall it
) mentioning you by name at all. (Sorry, cheap shot.)

Sheesh! This 'CBF' is so obviously fake it's painful.
I find it hard to believe that so many people would fall for it.

Is you guys a bit fick or sumtin ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Willem said:
Sheesh! This 'CBF' is so obviously fake it's painful.
I find it hard to believe that so many people would fall for it.
[snip]
But the elephant in the room here is the stupid flooding, which
should've put everyone on alert for fake CBF posts [snip]
I don't think the intent is to get away with pretending to be
CBF -- a signing solution would easily fix that.
[snip]

But, of course, we all know how much /some people/ dislike usenet posts that
are signed such that the sender can be positively identified (at some
level)

- --
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
- ---------- Slackware - Because I know what I'm doing. ------


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Armoured with GnuPG

iD8DBQFJtDMjagVFX4UWr64RAt/HAKCK1at5yUbA0f8eo+w9x9G0UwMBWACgm6iS
GWS9qaGGp3iexsUe0UyF4lM=
=NYDX
-----END PGP SIGNATURE-----
 
C

CBFalconer

Flash said:
Now tell me where I mentioned the beginning of the of the structure?
I can't see it in what you quote, can't remember it and can't see it
in the log of my posted message.

Some idiot is entering messages under my name. He, she, or it has
been copying some messages of mine as responses to something
entirely different. If you examine the path involved, they do not
originate on Motzarella.com. This may change.
 
C

CBFalconer

CBFalconer said:
No, I am right. Check the standard. Structure padding alternates 1
and 0 bits. It HAS to be copied. But there is NO structure padding
at the beginning of a structure. Some languages, such as Pascal, don't
even HAVE C's structure utility.

This did not originate with me. Some idiot is generating fake
messages from me. I don't have a reliable means of detecting them
yet.
 
C

CBFalconer

Mark said:
Chapter and verse would be nice at this point. I don't recall it
mentioning you by name at all. (Sorry, cheap shot.)

See my earlier answer. This is some idiot trolling.
 
L

luser-ex-troll

I knew I should have done a quick compiler test; but my wife was
calling, so I posted it without testing.

I'm still not authorized to use C99 at work, so I have a lot less
practical experience with it than I'd like.

I only caught it because I just figured this out a few
weeks ago. The dots looks weird, don't they?
Of course they make sense after a little consideration.

lxt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top