Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Whats the difference between char * and char []?

char difference
0
0 Posted

Whats the difference between char * and char []?

0
0

This questions usually presents code such as the following and asks what is different, or why it is not the same: char *p = “abcde”; // LINE A char q[] = “ABCDE”; Well, they are not the same. p is a pointer, a variable in its own right with its own memory, that is being initialized to point to the address of “abcd”, an unnamed string literal. If you could break it down into steps, you might have: char *p; p = “abcde”; whereupon p will be pointing at the location of where the a is. With q, it is actually the name for an array, specifically a 6 character array. It is as if it were declared as: char q[] = { ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘\0’ }; Here q does not point at the array of chars, it actually is the beginning location of the array of chars, 6 of them. With those things in mind, to be clear, note that p is of type char * and q is of type char [6]. In fact, the pointer can point at the array: p = q; // ok, p points at the location holding the A, aka &q[0] we can even write that: p = &q[0

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123