what is difference between char*name and char name[]

Recommended Answers

All 3 Replies

char* name is a pointer, while char name[] is illegal because it is attempting to create an array of 0 elements. Some compilers may support declaring arrays with 0 elements, but that is an extension of the standards. It's quite dangerous to use non-standard constructs because that makes the program itself non-standard and may not compile with other compilers. If you instructor or someone else wants to compile your program with another compiler it mahy fail.

None, ecept that char nam[] is usually followed by a string to intitialise it:

char name[] = "George Bush";

Whereas

char *name;

just declares a pointer, to be pointed at something later.

char name[] = "George Bush";

In the above case name is a variable that resides in writable memory and you can change it's contents later however you wish (assuming you stay within the bounds of the array).

char* name = "George Bush"

In this case name is just a pointer that refers to a string in read-only memory. The string can not be changed, but the pointer can be changed to refer to something else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.