-1

As per multiple sources, a pointer p points to a value when it is dereferenced. Thus, we may say that a pointer contains an address as it's value, and when the dereference operator (*) is used, the value at the address is returned.

A pointer may be assigned a value as follows:

int a = 90; int *p = &a;

if we assign a pointer it's value as follows:

int *p; *p = 60;

60 is alloted to p and causes undefined behavior upon dereferencing since 60 is not a valid address. (As per the answer to this question).

However, for the following code:

    int a = 90;
    int *p = &a;

    printf ("p is %d \n",*p);
    printf ("a is %d \n", a);
    printf ("address is %p \n",p);

    *p = 100;

    printf ("p is %d \n",*p);
    printf ("a is %d \n", a);   
    printf ("address is %p \n",p);

The following output is recieved :

p is 90
a is 90
address is 0028FED8

p is 100
a is 100 address is 0028FED8

ie, the expression *p = 100 changes the value at a, and not the value contained by p.

HOW ??????

Community
  • 1
  • 1
Fabulous
  • 735
  • 1
  • 10
  • 28

4 Answers4

2

*p = &a doesn't even compile. p is a pointer to int. It currently has an undefined value, therefore assigning anything to *p is undefined behaviour and would most likely crash. However, even if p did point to an int, you could only assign an int to *p, &a is a pointer to int, not an int, so this doesn't compile.

In your second example, *p = 60, the value of p is undefined, so you are trying to store 60 to an undefined location in memory. Instant crash. p isn't modified by this, so your explanation is wrong. p is not set to 60. You can't set p to an int. You can only set it to a pointer to int.

Correct:

p = &a; 
*p = 60;
gnasher729
  • 51,477
  • 5
  • 75
  • 98
1

You had asked:

ie, the expression *p = 100 changes the value at a, and not the value contained by p.

You can read the comment section for explanation of each line of C code and I'm not using exact address locations but using arbitrary ones for demonstration purposes:

int *p;       // Stack variable pointer to integer type w/ p's address being 4 bytes      @  0x00000000
int a = 90;   // Stack integer variable `a` and initializing it to the value of 90 located @  0x00000040 
*p = &a;      // Dereferencing the pointer `p` to be equal to the address of `a` ... One would think
              // that the address value of `a` 0x00000040 in hex would be stored into `a` which
              // has the value of 64 in decimal, however this is not always the case and this should be 
              // undefined behavior, but can still compile and run depending on the compiler and architecture. 
              // It may run or crash or not even compile or build at all. Most compilers should throw an error.

*p = 100;    // 'p' is located at 0x00000000 and contains the value 0x00000040 and by dereferencing it
             // it will assign the value of 100 to the stack address location of 0x00000040. Thus this
             // changes the value of `a` to 100

             // These two statements are in a sense equivalent  
*p = 100;    a = 100;
             // If one was to assign the address of `a` to `p` as such:
 p = &a;

EDIT

            // Therefor the statement `*p=100` will only work if the statement
            // `p=&a` is defined and evaluated beforehand.

EDIT Now as for the question based on the Title: "what does *p contain?" with the op's original code provided *p actually contains garbage or what ever was assigned to it upon declaration.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 1
    > the address value of `a` which is in hex would be stored into `a` – Fabulous Nov 26 '16 at 16:44
  • @simrandhamija well correct but that line of code is undefined behavior; and I did initially state that the address value would be stored as hex but it's equivalent value in decimal is 64 since the variable type is an int. But this is not what happens. – Francis Cugler Nov 26 '16 at 16:47
  • don't you mean " the address value of `a` which is in hex would be stored into `p` " ? – Fabulous Nov 26 '16 at 17:19
  • @simrandhamija yes, that is what would logically happen if the statement was valid, but to some novices they might think that it would store the actual value as its decimal conversion, but it doesn't. – Francis Cugler Nov 26 '16 at 18:48
  • @simrandhamija take the sentence from the comment section and omit the last part `with the value of 64 which is decimal, ...` and just read the first part of the sentence by itself. `One would think that the address value of 'a' (0x0...) which is in hex would be stored into 'a'...` is the same thing as what you replied back to me in your first and initial comment. – Francis Cugler Nov 26 '16 at 18:51
1

the code you wrote at the begining:

int *p;
int a = 90;
*p = &a;

is not valid, The asterisk (*) in line 1 indicate that it is a pointer, it is not the dereference operator as in line 3.

the following code:

int a = 90;
int *p = &a;

is equivalent to:

int a = 90;
int *p;
p = &a;

(p) is a pointer , and now is pointing at address of (a)

*p = 100;

so, you just assign a value to a, a = 100 . and you are printing the same value from the same address.

0

As per multiple sources, a pointer p points to a value when it is dereferenced.

Not quite. A pointer points to an object. Dereferecing a pointer produces that object. Using an object in a context where a value is needed produces the stored value.

int *p = &a;

The object that p now points to is a.

*p = 100;

Dereferencing p produces the pointed-to object, namely a. Since this is not a context where the stored value is needed, a's value isn't read, it remains the object a which is assigned the value 100.

Or, simply put, *p means a, therefore *p = 100 means a = 100.

  • 1
    correct me if I am wrong, but isn't C a procedural language? there are no objects in C since it is not an OOP language. – Fabulous Nov 26 '16 at 16:02
  • 1
    @simrandhamija No, I did get my terminology right, but I can understand it's confusing. What C calls an object is not the same thing as in OOP. What C calls an object is simply a block of memory, typically a variable, or something pointed to by the result of functions such as `malloc`. –  Nov 26 '16 at 16:05