Difference between a[] and char* p


I would like to know what's going on under the hood in this situation:

int _tmain(int argc, _TCHAR* argv[])
	char a[] = "orbis";
	char* p = "tertius";

	a[3] = 't';		// no problem
	p[3] = 't';		// throws an exception

	return 0;

In the first case I am defining an array of 6 char elements (5 characters plus null): no problem. In the second case I have a pointer to what I believe is in essence another way to declare an array of characters (since an array is basically a pointer to the first element of the array). My first impression was that it would be possible to modify the elements of the array pointed at by p by using a [ ] notation: turns out that's not valid.

Can someone please clarify this point for me?

When [] appears in a function definition or prototype, it's a placeholder for a pointer and is considered to be an incomplete type.

Just to be clear, it's NOT an array. That means within the function you cannot know the size of the array passed it. This is because it resolves to a pointer.

Thus, in C, wherever an array is passed, the size has to be passed too, or some other indication of the size.

In your example, an array (argv) is passed along with the array's size (argc).

This is why strncpy is safe and strcpy isn't; snprintf is safe and sprintf isn't; etc.

When [] is used in a string declaration, the size is worked out by the compiler. So the size is known.
char a[] = "orbis";
a is an array of char, 6 elements long and initialised with 'orbis\0'

This is an error, and later compilers will flag it as such.
char* p = "tertius";
p is a mutable pointer to a constant string. Wth any lock, the string will be placed in a read-only segment and any attempt to write to it will generate a SEGFAULT. If you're unlucky, you won't get any warning at all.

You can use [] on both a and p, but the write into p is an error. So luckily (believe it or not), you're seeing and error as you cannot change anything in "tertius" thru p.
Last edited on
Topic archived. No new replies allowed.