2
char str[100];
char *q;

what is the difference between q and str....aren't they both char*
Mind that I am talking about str, not str[100]

6 Answers6

3
char str[100];

str is the the name of an array object. It is not in any real sense a pointer.

But like any expression of array type, it is, in most contexts, implicitly converted to the address of (or equivalently, a pointer to) the array's first element. This implicit conversion yields a pointer value; there is no pointer object, so there's nothing that can be modified. (The resulting pointer value is of type char*, not const char* or char *const.)

The applicable exceptions here are when str is the operand of a unary & or sizeof operator.

char *q;

q is the name of a pointer object, and like nearly any non-const object it can be modified.

See section 6 of the comp.lang.c FAQ.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

q is an uninitialized pointer, while str points to somewhere 100 bytes are allocated.

Also, you can change where q points to. But you cannot change the address of str later.

timrau
  • 22,578
  • 4
  • 51
  • 64
1

By convention the name of an array is a constant which its value is the address of first element of it.

In most cases when str is used in an expression or a statement it's implicitly converted to The address of the first element of the array. And one of the exceptions is with the sizeof operator. In this case (sizeof(str)), str is the array itself and it is not converted to an address and that's why sizeof(str) results 100.

rullof
  • 7,124
  • 6
  • 27
  • 36
  • Then explain the result of `sizeof str`. And no, the pointer value to which `str` may be implicitly converted is not `const`. – Keith Thompson Jan 23 '14 at 17:27
  • At a minimum, it would be `char *const`, i.e., a constant pointer to non constant `char`. What you have is a non-constant pointer to a constant char. – brianmearns Jan 23 '14 at 17:35
  • @sh1ftst0rm: No, it's just `char*`. The declaration of `str` does not create a pointer object, so no `const` is needed to avoid modifying any pointer object. Similarly, the value of `2+2` is of type `int`, not `const int`. – Keith Thompson Jan 23 '14 at 17:38
  • @KeithThompson Is It clear now? – rullof Jan 23 '14 at 17:39
  • Clear, but incorrect. There is no `const`, and an array name is not *always* converted to a pointer value. Think about what your answer implies about the result of `sizeof str`. – Keith Thompson Jan 23 '14 at 17:40
  • @KeithThompson How is that? – rullof Jan 23 '14 at 17:41
  • That's still incorrect; see my previous comment. – Keith Thompson Jan 23 '14 at 17:42
  • @KeithThompson: Agreed, and I think you explained it perfectly in your answer. But at the very least, if you're going to think in terms of `const`s and pointers, then it is the pointer that is "constant", not the value that it points to. I understand there's no actual pointer here to be `const`, but `char *const` is a closer approximation than `const char*`. – brianmearns Jan 23 '14 at 17:45
  • @sh1ftst0rm: `const` and "constant" are two distinct concepts, in spite of their similar names. `const` really means read-only, and it refers to *objects*. Given an array object, there is no pointer object for `const` to refer to. "Constant" refers to an expression that can be evaluated at compile time. Certainly `const char*` is incorrect, since you can modify the `char` elements. But `char *const` (const pointer to char) is still incorrect. The type of the expression, after the implicit conversion, is simply `char*`. – Keith Thompson Jan 23 '14 at 18:37
1

They are not the same.

Array vs. pointer questions already exist in great multitude on StackOverflow. See this, this, and this.

Community
  • 1
  • 1
kbshimmyo
  • 578
  • 4
  • 13
0

In term of accessing the data in the string, yes, str can be thought of as a char*, or more accurately, a char *const because you cannot assign a different address to str.

Strictly speaking, they are different types, which is why you can't assign a new value to str. Think of it like the compiler: every variable is just a label for a particular address that it will allocate for you. str, represent an address of the start of a buffer made up of 100 chars, but q is the address of an address, which in turn is the address of a char.

You can change the value of a variable, but you can't change (at run time) where a variable is placed (i.e., its address).

brianmearns
  • 9,581
  • 10
  • 52
  • 79
0

In that example str is of type char[100], whereas q is a pointer to a char. It could point to any single array element of type char, but not to an array of type char.

Kylelem62
  • 569
  • 4
  • 17
  • `q` cannot point to an array of `char`. It can point to an element of an array of `char`; you can then use pointer arithmetic to access other elements of the array. – Keith Thompson Jan 23 '14 at 17:25