Struct assignment, historical question

  • Thread starter William J. Leary Jr.
  • Start date
W

William J. Leary Jr.

I've got a history question, and several of you seem familiar with the history
of C, so perhaps someone knows.
The following:

#include<stdio.h>

struct checkit {
int a;
int b;
char c;
} wocka, tocka;

int main(void)
{
wocka.a = 10;
wocka.b = 20;
wocka.c = 'a';

printf("wocka = %d %d %c\n", wocka.a, wocka.b, wocka.c);

tocka = wocka;

printf("tocka = %d %d %c\n", tocka.a, tocka.b, tocka.c);

exit(0);
}

produces this for output:

wocka = 10 20 a
tocka = 10 20 a

That is, tocka = wocka; copied the entire contents of the structure.

But I'd swear that when I was working with C back in the '70's, this did not
work. To perform this copy, I had to do this:

tocka.a = wocka.a;
tocka.b = wocka.b;
tocka.c = wocka.c;

or

memcpy(&tocka, &wocka, sizeof(struct checkit));

Am I misremembering this, or was this actually the case? Or perhaps just a
problem with the Sun compiler I was using? I seem to have a vague recollection
of the original K&R saying that assignments wouldn't work, but likely would
some day.

- Bill
 
J

Jack Klein

I've got a history question, and several of you seem familiar with the history
of C, so perhaps someone knows.
The following:

#include<stdio.h>

struct checkit {
int a;
int b;
char c;
} wocka, tocka;

int main(void)
{
wocka.a = 10;
wocka.b = 20;
wocka.c = 'a';

printf("wocka = %d %d %c\n", wocka.a, wocka.b, wocka.c);

tocka = wocka;

printf("tocka = %d %d %c\n", tocka.a, tocka.b, tocka.c);

exit(0);
}

produces this for output:

wocka = 10 20 a
tocka = 10 20 a

That is, tocka = wocka; copied the entire contents of the structure.

But I'd swear that when I was working with C back in the '70's, this did not
work. To perform this copy, I had to do this:

tocka.a = wocka.a;
tocka.b = wocka.b;
tocka.c = wocka.c;

or

memcpy(&tocka, &wocka, sizeof(struct checkit));

Am I misremembering this, or was this actually the case? Or perhaps just a
problem with the Sun compiler I was using? I seem to have a vague recollection
of the original K&R saying that assignments wouldn't work, but likely would
some day.

- Bill

The first 1978 edition of K&R mentioned that structure assignments and
the passing and returning of structures by value were common
extensions that were expected to become universal, but were not yet
allowed on all implementations.
 
S

Suman

William said:
I've got a history question, and several of you seem familiar with the history
of C, so perhaps someone knows.

[ ... cnip code ]
Am I misremembering this, or was this actually the case? Or perhaps just a
problem with the Sun compiler I was using? I seem to have a vague recollection
of the original K&R saying that assignments wouldn't work, but likely would
some day.

No. Struct assignment came in with the ANSI standard.
1. K&R 2: Preface: Paragraph 3: Line 1
2. K&R 2: Chapter 6: Paragraph 3: Line 1
 
W

William J. Leary Jr.

I've got a history question, and several of you seem familiar with the history
of C, so perhaps someone knows.
((..omitted..))
Am I misremembering this, or was this actually the case? Or perhaps just a
problem with the Sun compiler I was using? I seem to have a vague recollection
of the original K&R saying that assignments wouldn't work, but likely would
some day.
[/QUOTE]

Jack Klein said:
The first 1978 edition of K&R mentioned that structure assignments and
the passing and returning of structures by value were common
extensions that were expected to become universal, but were not yet
allowed on all implementations.

.... and ...

Suman said:
No. Struct assignment came in with the ANSI standard.
1. K&R 2: Preface: Paragraph 3: Line 1
2. K&R 2: Chapter 6: Paragraph 3: Line 1

When I remember where my 1978 K&R is, I'll look it up. But you've both just
settled and argument I was having here.

Thank you both.

- Bill
 
S

Suman

William J. Leary Jr. wrote:
[ snip ]
When I remember where my 1978 K&R is, I'll look it up. But you've both just
settled and argument I was having here.

Will you be able to find the lines I mentioned? I doubt. You'd
probably need one a good ten years younger :)
 
S

SuperKoko

William said:
I've got a history question, and several of you seem familiar with the history
of C, so perhaps someone knows.
The following:

#include<stdio.h>

struct checkit {
int a;
int b;
char c;
} wocka, tocka;

int main(void)
{
wocka.a = 10;
wocka.b = 20;
wocka.c = 'a';

printf("wocka = %d %d %c\n", wocka.a, wocka.b, wocka.c);

tocka = wocka;

printf("tocka = %d %d %c\n", tocka.a, tocka.b, tocka.c);

exit(0);
}

produces this for output:

wocka = 10 20 a
tocka = 10 20 a

That is, tocka = wocka; copied the entire contents of the structure.

But I'd swear that when I was working with C back in the '70's, this did not
work. To perform this copy, I had to do this:

tocka.a = wocka.a;
tocka.b = wocka.b;
tocka.c = wocka.c;

or

memcpy(&tocka, &wocka, sizeof(struct checkit));

Am I misremembering this, or was this actually the case? Or perhaps just a
problem with the Sun compiler I was using? I seem to have a vague recollection
of the original K&R saying that assignments wouldn't work, but likely would
some day.
Strictly speaking, K&R C had no structure assignment.
"Classic C" is a term used for pre-standard C, including structure
assignment, enumerations and void (but not void*).
C89 adopted void*, const and prototypes from C++.

I think that the following links are revelant:
http://cm.bell-labs.com/cm/cs/who/dmr/cchanges.pdf
More info on Dennis Ritchie web site : http://cm.bell-labs.com/who/dmr/
Interesting articles by Bjarne Stroustrup :
http://www.research.att.com/~bs/siblings_short.pdf
and http://www.research.att.com/~bs/sibling_rivalry.pdf

And a brief history of the C language by Dennis Ritchie :
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

Dennis' home page is full of interesting information on the C and B
languages and on UNIX.
 
L

lawrence.jones

Suman said:
No. Struct assignment came in with the ANSI standard.

On the contrary, struct parameters, return values, and assignment were
added to the Unix C compilers just about the time that K&R was actually
published (the "Recent Changes to C" paper noting them is dated Nov. 15,
1978), leading to a great deal of frustration many years later when
MS-DOS compilers appeared that implemented K&R exactly and thus didn't
support them.

-Larry Jones

He doesn't complain, but his self-righteousness sure gets on my nerves.
-- Calvin
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top