-3

I found this piece of code:

char *array[4]={"aa","bb","cc","dd"};
char *(*ptr)[4]=&array;

In comment it says:

ptr: It is pointer to array of string of size 4

What is then by definition a variable array?

And what would be in this case?

char array[4]={"aa","bb","cc","dd"};

since I found this:

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p = balance;

that says:

An array name is a constant pointer to the first element of the array, Thus, the above program fragment assigns p as the address of the first element of balance.

Okay, I think got this. So to try to answer my first question, is then variable array a constant pointer to the first character of the first element of the array, namely 'a'? I say this due to the * sign in front of the variable name.

Second question answer would just be: array is the constant pointer to the first element of the array.

Apparently, still having problems with pointers and arrays in C...

iBug
  • 35,554
  • 7
  • 89
  • 134
Krcevina
  • 131
  • 4
  • 13

2 Answers2

2

No. Array is not a constant pointer in C. It is dead wrong.

Array decays into pointer to first element in most cases. That doesn't mean it is a pointer.

Constant thing is coming in your mind because array is non-modifiable lvalue. So you can't use it as one where it is being modified.

From §6.3.2.1p3

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

This explains what happened - balance in that statement is converted into pointer to the first element. The first element is double and a pointer to it is double* and that pointer contains address of balance[0].

This paragraph also points out why the &array is char* (*)[] because array used as operand to & address of operator is a case where array is not converted to pointer. That is why the first statement is legal. (Note that char *(*ptr)[4] is a pointer to an array of 4 char*-s. Here the address of array is assigned to ptr).

from §6.3.2.1p1

...A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const- qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const- qualified type.

Whatever the source be, maybe it tried to show you this idea that arrays are not modifiable lvalues so statements like balance++ in C is illegal given that balance is an array name.

Without going awry or confusing the correct way to describe what array would be from standard §6.2.5p20

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T , the array type is sometimes called ''array of T ''. The construction of an array type from an element type is called ''array type derivation''

Also note one thing, when you used char a[]={"aa","bb"}; - this is wrong in the sense that string literals are char arrays which decays into pointer - so it is an array of char*-s not array of char.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

No. Arrays are not pointers. There's something called "array-to-pointer conversion" (decaying), but it only happens on the first level, and does not make arrays and pointers the same thing.

In your code snippet:

char array[4]={"aa","bb","cc","dd"};

array can be uses as (decays to) a pointer to another pointer to the first character of the string, or in other words, the first element in the array. There's no "the first character of" the first element, since that's the second level, and there are other matters.

This can be comprehended in another way: balance can be converted to a pointer to balance[0], so we have balance == &(balance[0]). But it can never happen that array = &(&(array[0][0])) because the address-of operator returns an rvalue, or anyway a value that cannot be taken address again, so array is not a pointer to array[0][0].

Since you're using string literals, which are effectively constant pointers to characters, the content of array looks like:

           array
          +----------+
array --> | array[0] |  --> "aa"
          +----------+
          | array[1] |  --> "bb"
          +----------+
          | array[2] |  --> "cc"
          +----------+
          | array[3] |  --> "dd"
          +----------+

Therefore, array is not a pointer to any character, it may be converted to (decay to) a pointer to another pointer of char, but it won't ever be converted to a pointer to the first character, 'a'.

iBug
  • 35,554
  • 7
  • 89
  • 134