pointer & struct
SAVAGE CHARLES L
savage at tramp.Colorado.EDU
Tue Feb 7 12:05:36 AEST 1989
In article <7208 at pyr.gatech.EDU> dvu at pyr.gatech.EDU (Dinh Vu) writes:
>The small program below compiled fine, but
>it gave core dump when I run it. Would someone give me
>some light on this matter.
>
> #include <stdio.h>
>
> struct abc {
> int x;
> int y;
> };
>
> main()
> {
> struct abc *var;
--------------->var = (abc *) malloc ((unsigned) sizeof(struct abc));
> var->x = 5;
> var->y = 10;
> }
>
Sombody has probably already fixed this error, but you must alocate
memory for the pointer to point too. If not, all the pointer has is a
random address, and when you assign a value to the pointer it overwrites
some memory in storage that may be used by your source code or may not be
yours at all.
Savage.
D
D
D
More information about the Comp.lang.c
mailing list