Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to string and char catch 22

Tags:

c++

c

pointers

I'm studying on pointers and I'm stuck when I see char *p[10]. Because something is misunderstood. Can someone explain step-by-step and blow-by-blow why my logic is wrong and what the mistakes are and where did I think wrong and how should I think. Because I want to learn exactly. Also what about int *p[10]; ? Besides, for example x is a pointer to char but just char not chars. But how come char *x = "possible";

enter image description here

I think above one should be right but, I have seen for char *name[] = { "no month","jan","feb" }; I am really confused.

enter image description here

like image 599
newbie Avatar asked Feb 10 '23 04:02

newbie


2 Answers

Your char *p[10] diagram shows an array where each element points to a character.
You could construct it like this:

char f = 'f';
char i = 'i';
char l1 = 'l';
char l2 = 'l';
char a1 = 'a';
char r1 = 'r';
char r2 = 'r';
char a2 = 'a';
char y = 'y';
char nul = '\0';
char *p[10] = { &f, &i, &l1, &l2, &a1, &r1, &r2, &a2, &y, &nul };

This is very different from the array

char p[10] = {'f', 'i', 'l', 'l', 'a', 'r', 'r', 'a', 'y', '\0'};

or

char p[10] = "fillarray";

which are arrays of characters, not pointers.

A pointer can equally well point to the first element of an array, as you've probably seen in constructions like

const char *p = "fillarray";

where p holds the address of the first element of an array defined by the literal.

This works because an array can decay into a pointer to its first element.

The same thing happens if you make an array of pointers:

/* Each element is a pointer to the first element of the corresponding string in the initialiser. */
const char *name[] = { "no month","jan","feb" };

You would get the same results with

const char* name[3];
name[0] = "no month";
name[1] = "jan";
name[2] = "feb";
like image 70
molbdnilo Avatar answered Feb 11 '23 23:02

molbdnilo


char c = 'a';

Here, c is a char, typically a single byte of ASCII encoded data.

char* ptr = &c;

ptr is a char pointer. In C, all it does is point to a memory location and doesn't make any guarantees about what is at that location. You could use a char* to pass a char to a function to allow the function to allow the function to make changes to that char (pass by reference).

A common C convention is for a char* to point to a memory location where several characters are stored in sequence followed by the null character \0. This convention is called a C string:

 char const* cstr = "hello";

cstr points to a block of memory 6 bytes long, ending with a null character. The data itself cannot be modified, though the pointer can be changed to point to something else.

An array of chars looks similar, but behaves slightly differently.

 char arr[] = "hello";

Here arr IS a memory block of 6 chars. Since arr represents the memory itself, it cannot be changed to point to another location. The data can be modified though.

Now,

char const* name[] = { "Jan", " Feb"..., "Dec"};

is an array of pointer to characters. name is a block of memory, each containing a pointer to a null-terminated string.

In the diagram, I think string* was accidentally used instead of char*. The difference between the left and the right, is not a technical difference really, but a difference in the way a char* is used. On the left each char* points to a single character, whereas in the one on the right, each char* points to a null-terminated block of characters.

like image 25
nishantjr Avatar answered Feb 11 '23 21:02

nishantjr