confusion with char *a and char a[NUM]

Jim Klavetter jjk at jupiter.astro.umd.edu
Mon Dec 3 18:38:58 AEST 1990


I am not a beginner and I have checked my texts, man pages, and the
faq list (Thanks Steve, I think it is a good job).  I am still
confused.  I think part of the problem is with an inconsistency in c
and part is my understanding.

I can have two identical files except that one I declare a to be
	char a[NUM]
and the other has
	char *a with a malloc of NUM+1 characters.

I guess I can stop there and ask the general question, "what is the
difference between those two?"  If done properly, they will both be
NUM+1 bytes (or whatever a char is) of memory and should be accessible
either by a[3] or *(a+3) for the forth element, for example.  Yet,
there are differences.

In both cases, the following is accepted by both my sun4 compiler and
gcc
	strcpy(string, a)
and there is no problem.

However, if I have
	a=strchr(string, ":");
I get the error message
	121: incompatible types in assignment
or some such thing (that one is from gcc).

The man page treats both the arguement of strcpy() and the return value
of strchr() as type (char *).  So why this inconsistency?  Am I using
strcpy() wrong above and just getting away with a flaw in the
compiler, or is there actually an inconsistency here.

Thanks in advance.

jjk at astro.umd.edu
Jim Klavetter (accepting mail for Athabasca and Reudi)
Astronomy
UMD
College Park, MD  20742



More information about the Comp.lang.c mailing list