pointer allocation

Guy Harris guy at auspex.UUCP
Tue Dec 20 04:39:09 AEST 1988


 >I am try to allocate a list of integer pointers by the following short
 >program.  But for some reason, after I compiled it and tried to run,
 >it gave me a Segmentation fault message. I couldn't see what's wrong
 >there. Could anyone out there give me some guide?
 >
 >#include <stdio.h>
 >int *(*B);
 >
 >main()
 >{
 > *B = (int *) calloc(18,sizeof(int));
 >}

The statement "*B = <expression>" means "assign the value of the
<expression> to the object pointed to by 'B'".  "B" is implicitly
assigned a NULL pointer value by its declaration; therefore, it doesn't
point to anything.  As such, "assign the value of the <expression> to
the object pointed to by 'B'" is a meaningless command, since there *is*
no object pointed to by "B".

The RT PC presumably catches attempts to dereference NULL pointers, as
do several other C implementations.

Why did you not just do

	...
	int *B;

	main()
	{
		B = (int *) calloc(18, sizeof(int));
	}



More information about the Comp.lang.c mailing list