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
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