-3

int main(){

    int number = 30;
    int* nPointer = NULL;

    nPointer = &number;

    printf("number:\n");
    printf("Value of number: %d \n", number);
    printf("Value of &number: %d \n", &number);
    printf("Address of number: %x \n", number);
    printf("Address of &number: %x \n\n", &number);

    printf("nPointer:\n");

    printf("Value of nPointer: %d\n", nPointer);
    printf("Value of &nPointer: %d \n", &nPointer);
    printf("Value of *nPointer: %d \n", *nPointer);

    printf("Address of nPointer: %x\n", nPointer);
    printf("Address of &nPointer: %x \n", &nPointer);
    printf("Address of *nPointer: %x \n", *nPointer);

    return 0;


}
Newbie to programming!
  1. I don't understand the difference between the output values of &number and nPointer. What do they mean?
  2. The address of *p and x?
  3. The difference between *nPointer and nPointer and &nPointer?

Overall, how are these connected? Obviously they are but how and why?

BPJ
  • 1
  • 2

3 Answers3

3

Most of your code causes undefined behaviour. With the printf function you must give the format specifier that corresponds to the type of the argument. It is not automatically converted for you.

Also most of your text description do not match the code.

Here is your code with undefined behaviour fixed, and the commentary fixed:

int main()
{
    int number = 30;
    int* nPointer = &number;

    printf("number:\n");
    printf("Value of number (base 10): %d \n", number);
    printf("Value of &number: %p \n", (void *) &number);
    printf("Value of number (base 16): %x \n", number);

    printf("nPointer:\n");

    printf("Value of nPointer: %p\n", (void *) nPointer);
    printf("Value of &nPointer: %p \n", (void *) &nPointer);
    printf("Value of *nPointer (base 10): %d \n", *nPointer);

    // last section redundant as it prints the same values but with wrong commentary

    return 0;
}

The void * is necessary because there are different sorts of pointer, and %p only knows about void *. (On common systems you may get away without it because common systems use the same format for all pointers).

"Value of &x" means the same thing as "address of x". The value of a pointer is the address of the object it is pointing to.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
0

variables ( like int nPointer) are stored in memory. And memory is a collection of memory addresses.

&number means address of variable 'number' in memory.

'nPointer' is an integer pointer because you declared it as 'int* nPointer'

An integer pointer will store address of an 'integer' variable.

So when you do, ' nPointer = &number' , nPointer will store the address of variable 'number'.

Adress of *p is the value stored in p.

*nPointer is read as : value at address stored in nPointer.

nPointer is a normal integer variable.

&nPointer is read as : address of variable nPointer

sps
  • 2,720
  • 2
  • 19
  • 38
0

Assume the following declarations and initializations:

int number = 30;
int *nPointer = &number;

then the following are true:

*nPointer ==  number == 30
 nPointer == &number == address of the number variable
&nPointer            == address of the nPointer variable
John Bode
  • 119,563
  • 19
  • 122
  • 198