Difference and relation between char * and char * []

I'm having trouble to understand the difference between the following two types:

 
  char * name {"Ahmed"};


and

 
 char * names[] {"Ahmed", "Kevin", "Susan", "Fatimah"};


The first, I assume, is a pointer to a zero terminated string constant.
The second, is a pointer to the first zero terminated string constant in an array of them?

So, could I say name = names[0]?
Could I say char ** newNames = names ?

How about creating a reference type for the second declaration (char * names[]).

I'm a little confused because the second type seems to be a pointer of pointers, so to speak, and I can't fully put this in my head.

Thank you!
Last edited on
You are correct about the firat.

The second is just an array of the first.

Hence, the first is a single string, while the second is .multiple strings.

Hope this helps.
Hi Duthomhas,

Yes that helps, but can you please elaborate more?

What about the rest?

Are the following statements also correct?
So, could I say name = names[0]?
Could I say char ** newNames = names ?

How about creating a reference type for the second declaration (char * names[]).

Thanks!
char *name is the same as char name[]

char *names[] is the same as char names[][]

1
2
3
4
5
6
7
8
9
int main()
{
	const char *name{ "Ahmed" };
	const char *names[]{ "Ahmed", "Kevin", "Susan", "Fatimah" };
	std::cout << name << ' ' << name[1] << '\n';
	std::cout << names[0] << ' ' << names[0][1];

	return 0;
}


So, could I say name = names[0]?

Yes.

Could I say char ** newNames = names ?

Yes, but be careful. There will be NO difference between newNames and names, if you change one you'd change the other.

If you want newNames to be SEPARATE from names but still copy the values, do this:

const char *nam[]{ *names };
Last edited on
I see.
I also added some code to test char ** which worked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
int main()
{
	const char *name{ "Ahmed" };
	const char *names[]{ "Ahmed", "Kevin", "Susan", "Fatimah" };
	std::cout << name << ' ' << name[1] << '\n';
	std::cout << names[0] << ' ' << names[0][1] << '\n';
	
	const char ** moreNames = names;
	std::cout << moreNames[0] << ' ' << moreNames[0][1] << '\n';
	
	std::cout << names << ' ' << moreNames << std::endl;

	return 0;
}


How can I create a reference to char ** or char [][]

Thanks!
char *name is the same as char name[]
char *names[] is the same as char names[][]

No, this is misleading. Arrays are not pointers. Arrays can be converted to pointers, but they are not pointers.

1
2
3
char *x = "foo"; // error: conversion from const char[] to char* discards qualifiers
char y[] = "foo"; // okay: aggregate initialization
// y has the array type char[4] 


How about creating a reference type for the second declaration (char * names[]).
1
2
// reference to array of 4 pointers to char
char* (&rnames)[4] = names; 
See my last posts, I edited it a bit ago.

To create a reference, do exactly as you did with this: char ** newNames = names

The two variables will have different names but will point at the same memory.

I don't know why you insist on using * instead of [], know that using * can make things look more complicated and isn't always the same as [].
I understand the first part, but don't understand your reference to pointers.
Can you please elaborate?
No, this is misleading. Arrays are not pointers. Arrays can be converted to pointers, but they are not pointers.

Yes, my bad, I meant in the context of your code. char *x is what your array would decay to if you were to pass it into a function. You can still use it as if it were an array to an extent through "[]".

You can use [] until you have a good reason not to. If you're doing this to learn, then my advice doesn't apply.


EDIT:
const char *x = "foo"; - Would work
^ Some compilers will run it even without the "const", but it may give you a warning.
Last edited on
They both generate data in memory, {h,e,l,l,o,/0}. The fundamental difference is that in one char* you are assigning it to a pointer, which is a variable. In char[] you are assigning it to an array which is not a variable. ... char* is a variable.

Example:

int main ()
{
char str1[] = "Test";
char *str2 = "Test";
cout << "sizeof array " << sizeof(str1) << endl;
cout << "sizeof pointer " << sizeof(str2) << endl;
}

To know more about this visit at: http://www.cetpainfotech.com/technology/C-Language-Training
Topic archived. No new replies allowed.