0

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?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
user394334
  • 243
  • 1
  • 10

1 Answers1

0

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
0___________
  • 60,014
  • 4
  • 34
  • 74
  • any commant silent DV-ter? – 0___________ May 25 '22 at 08:10
  • I am not the down voter but I can see why someone would. Saying _explicitly_ that `int * = c` "works" (where `c` has been declared for example as `int c = 10;` ) sounds like you are _implicitly_ giving it the thumbs up, i.e. that it is completely okay to make that assignment. **It is not okay to make that statement**. It would be more accurate to say that with any compliant compiler, `int * = c` will compile, but with a warning: _"warning: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int';"_ or similar. – ryyker May 25 '22 at 12:19
  • I read the whole thing a couple of times. Most of it is fine, but you cannot just go around and make statements like your very first one and expect people to accept it. It is incorrect. Bolding it later just draws more attention to that fact. And the mitigations you add later do not change the meaning of that very first statement. It still does not work to assign an `int` to a `int *`. It is an _incompatible_ integer to pointer conversion. – ryyker May 25 '22 at 12:40
  • ...also the comment here: `int *x = array; //decays to pointer to first element` is incorrect. The symbol `array` did not _decay_ into a pointer, it just is a pointer, but also retains all of its other attributes, such as size information. (i.e. `sizeof(array)` can be used here if needed to provide the size of the array. It is however correct to say that _when an array is passed as a function argument, it decays from an array with address, size and type information in tact, to a simple pointer, keeping only its address and type. `sizeof(array)` as function argument returns sizeof pointer. – ryyker May 25 '22 at 12:54
  • @ryyker C standard: `An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.` **That assignment is 100% correct!**. Standard does not know anything about "incompatible conversions" – 0___________ May 25 '22 at 13:00
  • @ryyker compiler does not have to emit any warning. Simply admit that my statement is **right** and you are wrong. (for example "*** It is not okay to make that statement.***") – 0___________ May 25 '22 at 13:10
  • I don't disagree with the excerpt, and it makes the point even better that you should not say an assignment that results in an _"incompatible integer to pointer conversion warning"_, works. – ryyker May 25 '22 at 13:10