user5183133
user5183133

Reputation:

C dereferencing a pointer to array?

I have some code that looks like this:

int *array[10];

printf("%p\n", array); // Prints an address
printf("%p\n", *array); // Prints a different address
printf("%d\n", **array); // Segmentation Fault 11
printf("%d\n", *array[0]); // Segmentation Fault 11
printf("%d\n", (*array)[0]); // Segmentation Fault 11

Why do I get a segmentation fault? Shouldn't it print the first value in the first array?

Upvotes: 0

Views: 15758

Answers (4)

ankit gupta
ankit gupta

Reputation: 9

#include <iostream>
        using namespace std;
        #include <math.h>
        #include <string.h> 

        int main() {


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

            int *p=arr;


            int intgerSize=sizeof(int);


            for(int k=0;k<5;k++)


            {  
                cout<<"arr ["<<k<<"] "<<*(p+(k*sizeof(int)/intgerSize));  
                cout<<"  "<<(p+(k*sizeof(int)/intgerSize));
                cout<<"  "<<p+k<<"\n"; 

            }`enter code here`

            return 0;
        }
OUTPUT:- 
arr [0] 1 0x7ffd180f5800  0x7ffd180f5800
arr [1] 2 0x7ffd180f5804  0x7ffd180f5804
arr [2] 3 0x7ffd180f5808  0x7ffd180f5808
arr [3] 4 0x7ffd180f580c  0x7ffd180f580c
arr [4] 5 0x7ffd180f5810  0x7ffd180f5810

Upvotes: 0

tdao
tdao

Reputation: 17678

int *array[10];

array is array[10] of pointer to int (note that [] has higher precedence than *). But none of these 10 pointers have been initialised to point to valid location.

Given that,

    printf("%d\n", array); // address of array itself (also address of first element)
    printf("%d\n", *array); // getting the value of the fisrt array element - UB/unitilialise (currently points to some random location)
    printf("%d\n", **array); // dereference that first pointer - UB/segfault as deferencing an initialised pointer
    printf("%d\n", *array[0]); // same as *array
    printf("%d\n", (*array)[0]); // same as **array

Upvotes: 1

Zrax
Zrax

Reputation: 1670

To take a closer look, understand the nature of the declaration:

int *array[10];

This declares an array of 10 pointers (currently uninitialized) to ints -- note that this is not the same as a pointer to an array of 10 ints, which would instead be declared int (*array)[10]. Even declared this way though, you'd still need to initialize the pointer with something.

printf("%d\n", array); // Prints an address

This prints an the address of the ages array (the array is converted to a pointer automatically by passing it to printf).

printf("%d\n", *array); // Prints a different address

This uses the same rules to convert the array to a pointer, and then dereferences that pointer. Therefore, you're printing the first value of the array (equivalent to printf("%d\n", ages[0])). However, what you're actually printing here is an address, not an integer (even if it is uninitialized).

printf("%d\n", **array); // Segmentation Fault 11
printf("%d\n", *array[0]); // Segmentation Fault 11
printf("%d\n", (*array)[0]); // Segmentation Fault 11

Each of these are now dereferencing the uninitialized pointer stored in array[0]. They do indeed be refer to an int, but the pointer to that int is whatever the compiler and/or your OS decided to put in there.


Example: Pointer to an array

An example of using a pointer to an array looks like this:

#include <stdio.h>

int main()
{
    int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int (*parr)[10] = &array;


    printf("%p\n", parr);
    printf("%p\n", *parr);
    printf("%d\n", **parr);
    printf("%d\n", *parr[0]);
    printf("%d\n", (*parr)[0]);
}

Output (addresses vary, obviously)

0x7fff5fbff990
0x7fff5fbff990
1
1
1

The last three all ultimately lead to the same element, but go about it in different ways.

Upvotes: 9

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33854

You get a segmentation fault because you are attempting to dereference an uninitialized pointer, which is Undefined Behavior.

This command (and all following):

**ages

Dereferences the array (which decays to a pointer), which you then dereference. You have an array of pointers, not a pointer to an array.

Upvotes: 1

Related Questions