3

As my teacher told me ,array declaration can't accept address as an assignment.

int a[]={1,2};
int b[2]=a;

This shows an error as invalid initializer. But i was playing with such things and found weird example as following.

int a[][3] = {1, 2, 3, 4, 5, 6};
int (*ptr)[3] = a;

This is also a declaration of an array whose address is kept with ptr pointer and this accept the address of 2 Dimensional array a,but this shows no error. Why,can somebody explain me in simple words? thanks in advance.

5
  • 2
    "No error" does not always mean "works as expected". You may not have all of the compiler warnings turned on that will alert you to potential pitfalls. Commented Jul 10, 2018 at 19:44
  • 1
    When a compiler does not give an error, that means the syntax is correct. It does not give you any hint about the programming logic (in most cases). Tipp: Enable all warnings (for gcc, add -Wall to your Makefile or compilation command). Commented Jul 10, 2018 at 19:46
  • 1
    But it worked on online compilers as well. Commented Jul 10, 2018 at 19:46
  • Does "work" mean "does what I want" or "does something"? Commented Jul 10, 2018 at 19:47
  • 1
    it dispalyed what i want.. Commented Jul 10, 2018 at 19:47

2 Answers 2

7

In your second example, ptr is not an array but a pointer to an array.

This pointer is initialized with a which is an array that, in this context, decays to a pointer to its first element. The type of a is int [6][3], i.e. an array of size 6, where each element is an array of type int [3]. So a pointer to an element of a has type int (*)[3], which matches the type of ptr.

Sign up to request clarification or add additional context in comments.

1 Comment

Okk @dbush ,this is declaration of a pointer but single pointer should accept address of one dimensional array rather than it's accepting address of two dimensional array.How?
2

Your teacher is correct - an array expression may not be the target of an assignment. However, what's happening in

int b[2] = a;

is that an initializer for an array in a declaration must be a brace-delimited sequence of values. The expression a is not a brace-delimited sequence, hence the error.

What your teacher is talking about is situations like

int b[N];
...
b = some_expression;

This is not allowed - you cannot assign to an array using the = operator.

You can assign to individual array elements (as long as those elements are not arrays themselves):

b[i] = some_value;
b[j] = a[i];

Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T" and the value of the expression will be the address of the first element of the array.

The declaration

int a[][3] = {1, 2, 3, 4, 5, 6};

creates a 2-element array of 3-element arrays of int. Graphically:

   +---+
a: | 1 | a[0][0]
   +---+
   | 2 | a[0][1]
   +---+
   | 3 | a[0][2]
   +---+
   | 4 | a[1][0]
   +---+
   | 5 | a[1][1]
   +---+
   | 6 | a[1][2]
   +---+

Thus, when the expression a appears in the line

int (*ptr)[3] = a;

it is converted from type "2-element array of 3-element array of int" to "pointer to 3-element array of int", or int (*)[3], and the value of the expression is the address of a[0][0]. Since you declare ptr as a pointer to a 3-element array of int, the initialization works, and ptr points to the first element of the array (a[0]):

   +---+
a: | 1 | a[0][0] <--- ptr
   +---+
   | 2 | a[0][1]
   +---+
   | 3 | a[0][2]
   +---+
   | 4 | a[1][0]
   +---+
   | 5 | a[1][1]
   +---+
   | 6 | a[1][2]
   +---+

Since ptr points to a 3-element array of int, ptr+1 will point to the next 3-element array of int (a[1]):

   +---+
a: | 1 | a[0][0] <--- ptr
   +---+
   | 2 | a[0][1]
   +---+
   | 3 | a[0][2]
   +---+
   | 4 | a[1][0] <--- ptr + 1
   +---+
   | 5 | a[1][1]
   +---+
   | 6 | a[1][2]
   +---+

Edit:

A handy chart for declarations involving arrays, functions, and pointers:

T *a[N];   // a is an array of pointers to T
T (*a)[N]; // a is a pointer to an array of T
T *f();    // f is a function returning a pointer to T
T (*f)();  // f is a pointer to a function returning T

Things can get even more complicated:

T (*f[N])(); // f is an array of pointers to functions returning T
T (*a())[N]; // a is a function returning a pointer to an array of T

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.