Hi all,

This is something very basic i know. I just wanted to avoid some confusion.

What is the difference between char *names[100]; and char names[100];

This is what i know

char names[100]; - its just a character array which can hold 99 characters and the last one will be '/0'

char *names[100]; - All the 100 array elements are character pointers and are pointing to the starting element of 100 names somewhere in the memory location.

Am i correct?

if so, in the second case (pointer version), if i want to print all those names cout<<*names[i] where i iterate from 0 to 99 will work?

char names[100]; - its just a character array which can hold 99 characters and the last one will be '/0'

Yup.

char *names[100]; - All the 100 array elements are character pointers and are pointing to the starting element of 100 names somewhere in the memory location.

Close. Depending on where this array is defined, the pointers could point to nowhere (ie. null) or some unpredictable addresses by default. It's not quite correct to say that they point to names unless you've already allocated memory to the pointers and populated that memory with strings representing names.

if i want to print all those names cout<<*names[i] where i iterate from 0 to 99 will work?

Close. Dereferencing the pointer will give you the first character that it points to, so an iteration loop (assuming the pointers actually point somewhere you own) would be:

for (int i = 0; i < 100; i++)
{
    cout << names[i] << '\n';
}

The reason this works is the << operator knows how to interpret a pointer to char as a string and will do the "right" thing.

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.