1

I thought that if I create a pointer to array type, I will be able to print all of its elements. I wrote a program like this,

#include <stdio.h>

int main()
{


    int arr[] = {4,2,3};
    //here arr is a (*arr)[3] type
    int (*p)[3], i;
    p = &arr;
    for(i = 0 ; i < 3 ; i++)
            printf("%d ", *(p)[i]);
    printf("\n");
    return 0;
}

output : 4 0 -673566907

My understanding is p will point to an array of 3 elements. p+1 will point to another array of 3 elements.

arr=x   [ 1      2     3 ]
 y       [x]  [x+1] [x+2]

p=y(address of arr)
*p=x
**p = 1
*(*p+1) = 2
*(*p+2) = 3

I can print array like above. Does it mean p is actually double pointer?

P.S Is it correct way to read

  1. *(p)[1] should be read as "p is an array of 1 pointer to"

  2. (*p)[1] should be read as "p is a pointer to an array of 1 element"

  3. when we print

printf("%d ", (*p)[1]); //here it should be read as p is a pointer to the second element"

5 Answers 5

2

You're dereferencing your pointer p in the printf statement incorrectly. Your code is acting as if p is an array of pointers to int instead of a pointer to an array of int. Instead of

printf("%d ", *(p)[i])

you want

printf("%d ", (*p)[i])

because you have to first turn p into the int[3] and then index into it.

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

Comments

1

Change the Line

printf("%d ", *(p)[i]);

to

printf("%d ", (*p)[i]);

It will access the pointer-to-array first, due to referencing (*p), and then iterate through the content of the array with the []-operator.

Consider the second iteration in the for-loop from your code which equals to:

printf("%d ", *(p + 1));

Trying to access the memory location p+1 with the * leads to undefined behavior as others have already pointed out.

4 Comments

My understanding is p will point to an array of 3 elements. p+1 will point to another array of 3 elements....so it does not quite match your statement here.
@acidlategamer yes. the parenthese is redundant in this case
While printing if I am using (*p)[1], then I am pointing to address of the second element or value, or getting only the address of the second value?
The expression (*p) gives you the memory address where the array arr is located. You then apply the [ ]operator to it. Thus, (*p)[1] is equal to arr[1].
1

I will post my understanding from this thread,

pointer to array type

int arr[4] = {2,4,1,7};
int (*p)[4];//here p is a pointer, pointing to an array of 4 elements
p = &arr;

now if i want to access elements of 0th array

*p = *(p+0); //first element address pointed by p

*p+1 = *(p+0)+1;//second element addres pointed by p

and so on

now

**p = *(*(p+0)) ;//dreference of the 0th element of 0th array. equals to (*p)[0] expression.

*(*p+1) = *((*p+0)+1);//dereference of the 1st element of 0th array. equals to (*p)[1] expression

*(p)[1] = *(p+0)[1] =*(p+(1))= *(p+1) = next array.

Comments

0

No, for any value other than 0 for i

for(i = 0 ; i < 3 ; i++)
        printf("%d ", *(p)[i]);

causes undefined behavior as you try to access invalid memory.

Therefore, the is no scope of p being a pointer-to-pointer, it is a pointer-to-array, and as they say, arrays are not pointers and vice-versa.

You can, however, take a pointer to the starting of the array and then loop over till you have valid elements in the array, that will be allowed.

Comments

0

I didn't understand your code well, but I edited it in this way and it worked. hope it can help you.

#include <stdio.h>
int main()
{
    int arr[] = {4, 2, 3};
    int *p[3];
    int i = 0;
    p[0] = arr;

    for (int i = 0; i < 3; i ++)
        printf("%d\n",*(p[0] + i) );

    return 0;
}

Comments

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.