0

hello i am first question in stackoverflow! i am not good at english :)but i tried it!

int numarr[1] = { 11 }; int* numptr1 = numarr; printf("%d", *numptr1);

result: 11

int num = 11; int* numptr2 = # printf("%d", *numptr2);

result: 11


i found difference!

int* numptr1 = numarr; int* numptr2 = #

why variable uses "&" and why array not use "&" when i allocated? I dont know difference... please tell me! Thanks you!

sangfeel
  • 1
  • 1
  • 1
    You can also write `int* numptr1 = &numarr[0];` which looks just like `int* numptr2 = #`. – dxiv Mar 06 '21 at 03:44
  • Your English is fine, and it's not a bad question - it's just that it's almost the same as another question which has already been answered. Please do not let this discourage you. Welcome to Stack Overflow, and keep asking questions. :-) – Bob Jarvis - Слава Україні Mar 06 '21 at 03:47

1 Answers1

1

Pointer is a fundamental concept of C programming.

In computer science, a pointer is a programming language object that stores a memory address.

You can think of your computer's memory as a contiguous array of bytes. Each time that you make an innocent declaration and assignation such as int a = 5, this value is written into your computer's memory on 4 bytes (integer size). This value will be written at a specific memory address, the stack (fast access to memory) if no memory allocation, else it will be stored deeper in the heap. This address also has a value!

#include <stdio.h>

int main(void) {
    int a = 5;  // declaring an integer variable and assigning the value of 5
    int *ptr;   // declaring a pointer to integer
    int b;      // declaring an integer variable
    printf("ptr's value: %2d, ptr's address: %p\n\n", *ptr, ptr);

    ptr = &a;   // pointer ptr points to what is stored at the memory address of variable a
    b = a;      // b will take the value and not the address
    a = 42;     // b is still equal to 5, but ptr will return 42, which is the value now stored at a's location;
    printf("  a's value: %2d,   a's address: %p\n", a, &a);
    printf("ptr's value: %2d, ptr's address: %p\n", *ptr, ptr); // you will get the same as above, notice that you have to dereference the pointer with * to get the value, and using the pointer alone (ptr) will give you the memory address.
    printf("  b's value: %2d,   b's address: %p\n", b, &b);
    //printf("Size of ptr: %zu\n", sizeof(ptr)); // size of ptr in bytes, 8 on my system.
    return 0;
}

You will get this kind of outpu:

ptr's value:  1, ptr's address: 0x7ffd99493000

  a's value: 42,   a's address: 0x7ffd99492f08
ptr's value: 42, ptr's address: 0x7ffd99492f08  <-- they now match thanks to ptr = &a
  b's value:  5,   b's address: 0x7ffd99492f0c

NB: On the second printf you will get the value that you got for a, notice that you have to dereference the pointer with * to get the value, and using the pointer alone (ptr) will give you the memory address.


For your specific question about & What is an R value:

In many languages, notably the C family, l-values have storage addresses that are programmatically accessible to the running program (e.g., via some address-of operator like "&" in C/C++), meaning that they are variables or de-referenced references to a certain memory location

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81