char array vs char pointer
First of all, I recommend you take a look at this:
C Strings (Arrays vs. Pointers)
It describes the exact thing you are looking for, although in a course designed to teach C++. However, the part you are interested in is readable for someone with no experience in C++.
In short when you say:
Code:
char name[10] = "hello";
- you are allocating memory on the STACK for a string of 10 characters (including the string termination character \0) out of which only 6 characters are used ('h','e','l','l','o','\0')
Code:
char name[] = "hello";
- you are allocating memory on the STACK for a string which is IMPLICITLY as long as the string you are assigning to it (i.e. 6 characters (hello + \0)
- you are declaring a pointer to a string which can be allocated on the HEAP. For example, a ten character string can be allocated on the HEAP using the name pointer as such:
Code:
name = (char*)(malloc (sizeof(char) * 10));
If you are not familiar with the difference between STACK and HEAP memory you should look it up. It is an important aspect of programming in general, particularly in C.
Hope this clears things up a little.