2
int  array[5];

Expressions such as

 array[3] gets converted to *(array+3)

Or in

void fun ( int *array[] );

*array[] gets converted to int **array 

I was wondering what does the array declaration

int array[5]; 

Get converted to? Is it

int *(array+5)

If yes, what does this even mean? And how does one interpret it and/or read it?

4
  • 1
    int array[5]; means an array of size 5 whereas int *(array+5) is not a legal variable name as it has the + symbol in it. Commented May 9, 2017 at 8:12
  • Context is everything. It's important to know where the array is declared. Please edit and make sure your code examples are complete. Commented May 9, 2017 at 8:15
  • The C language is case-sensitive today as well. Are you using MS Word as code editor or what? Commented May 9, 2017 at 8:18
  • I'm using the the stack overflow app sometime I just do the 4 space indent manually and sometimes I use the insert code option Commented May 9, 2017 at 8:25

2 Answers 2

5
  • array[i] gets converted to *(array+i)

    Correct, given that array[i] is part of an expression, then array "decays" into a pointer to its first element, which is why the above holds true.

  • Void fun ( Int *array[] );
    *array[] gets converted to Int **array

    Yes because of the rule of function parameter adjustment ("decay"), which is similar to array decay in expressions. The first item of that array is an int* so after decay you end up with a pointer to such a type, a int**.

    This is only true for functions with the specific format you posted, there is otherwise no relation between pointer-to-pointers and arrays.

  • I was wondering what does the array declaration

    Int array[5];
    Get converted to?

    Nothing, declarations don't get converted. It is an array of 5 integers.


To sum this up, you actually list 3 different cases.

  • When an array is used as part of an expression, it "decays" into a pointer to the first element.
  • When an array is used as part of a function parameter declaration, it "decays" too - it actually has its type replaced by the compiler at compile-time - into a pointer to the first element. C was deliberately designed this way, so that functions would work together with arrays used in expressions.
  • When an array is declared normally (not part of a parameter list), nothing happens except you get an array of the specified size.
Sign up to request clarification or add additional context in comments.

1 Comment

@M.M Yes well the dirty details is that it depends on the operators used. I'm guessing you have sizeof in mind. I think this simplification should be sufficient for beginners though.
0

I think you are confusing two things.

*(array+i)

cannot be used for declaration, only for accessing the memory location (array being the starting address and i the offset)

also, the following declaration will create an array of 5 integers onto the stack

int array[5]; 

You can access any element from the array with the other notation, because values are being pushed onto the stack. The following two yielding in the same result:

int a = *(array+3);
int b = array[3];
if (a == b) printf("Same value");
else printf("Not same value");

5 Comments

int array[5]; is not a initialization... Moreover the question is tagged c and answer should be coded using that specific language.
I was not trying to initialize anything I was interpreting *(array+i) as accessing a memory location. Also I'm not trying to access the declaration int array [5] although Int *(array+5) sort of looks that way. I was just wondering if Int *(array+5) had any similar meaning to the other pointer representation or if it's nonsensical to think of it that way
"also, the following initialization is executed on the stack" It doesn't get executed at all... furthermore the scope determines where it gets allocated. Stack variables do not get initialized, as opposed to variables with static storage duration. Also, this is tagged C not C++, so kindly edit your post.
@LPs oops, changed it to a c function instead of C++
@Lundin better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.