简体   繁体   中英

Why can we only initialize a pointer with an array?

I have this code:

#include <stdio.h>

int main(void) {
    int b[] = {1,2,3};
    int *a = b;          //works
    
    
    int c = 1;
    int *d = c;          //doesn't work
}

Why is it that it works to initialize the pointer with an array but not an int?

int *d = c; it does work . It is simply converting the integer number held in c into a pointer. The compiler will issue the warning (unless you cast explicitly).

You can use it, for example, to read from a particular address of memory.

unsigned read_from_memory_address(unsigned address)
{
    unsigned *x = (unsigned *)address;
    return *x;
}

Of course, how this function will behave (or what type address should have) is implementation defined.

Pointers keep references to objects. If you want pointer to reference an integer variable you need to supply the reference (address) of this variable.

int *x = &c;

Arrays in expressions decay to pointer to first element of the array. That is why you do not need to use & in front of the array (you can, but the pointer will have a different type - in this case pointer to array).

int array[3];
int *x = array; //decays to pointer to first element
int (*y)[3] = &array; //pointer to array of 3 `int` elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM