How to use memory address to dereference? [duplicate] - c

This question already has answers here:
Pointers in C: when to use the ampersand and the asterisk?
(10 answers)
Closed 14 days ago.
I create a pointer and get its memory address. How can i dereference the pointer?
#include <stdlib.h>
int main()
{
int b[] = {11,22,33,44,55};
int *a = b;
printf("memory:%p\n",a);
return(0);
}
suppose the memory address of b is 0x7fff24e69e60. How can i get the b[0] by the memory address?
How to use memory address to dereference in C language?

In C programming, to dereference a memory address, you use the * operator.
int x = 5;
int *ptr = &x;
printf("Value of x: %d\n", x);
printf("Value stored at address %p is: %d\n", ptr, *ptr);
Here, ptr is a pointer to an integer and is assigned the address of x using the & operator. The *ptr expression dereferences the pointer and accesses the value stored at the memory address ptr.

Related

Stack in x86_64 in C program on linux [duplicate]

This question already has answers here:
Order of local variable allocation on the stack
(10 answers)
Where in memory are my variables stored in C?
(9 answers)
Address of a string literal and array
(5 answers)
What is the difference between returning a char* and a char[] from a function? [duplicate]
(6 answers)
Closed 1 year ago.
I thought the stack in x86_64 would grow down hence from the max. address of the processes virtuell memory till the end of the stack size (https://www.google.com/url?sa=i&url=https%3A%2F%2Fen.wikibooks.org%2Fwiki%2FX86_Disassembly%2FThe_Stack&psig=AOvVaw3PNsHC2wRGe3XfCCsCi9Kd&ust=1631093147793000&source=images&cd=vfe&ved=0CAkQjRxqFwoTCNiH6c_F7PICFQAAAAAdAAAAABBY).
However in the following C programm the the first char of b, which I thought should be pushed on the stack after a and thereby should have a lower address, can be accessed by adding a positive offset instead of a negative one. Why is that?
I also don't understand why the addresses of the pointers to the stings a and b as well as the long long int c are stored ad much higher addresses. Whats in between the 0x7ff... and the 0x55f... addresses? A gap in the Stack?
#include <stdio.h>
int main() {
char *a = "abcdefg";
char *b = "mnopqrs";
printf("address a: %p\n", a);
printf("address &a: %p\n", &a);
printf("address b: %p\n", b);
printf("address &b: %p\n", &b);
printf("Char at addr %p: %c\n", a+8, *(a+8));
long long c = 0;
printf("address c: %p\n", &c);
return 0;
}
Output:
address a: 0x55fa2e1c8004
address &a: 0x7ffc148d29b0
address b: 0x55fa2e1c800c
address &b: 0x7ffc148d29b8
Char at addr 0x55fa2e1c800c: m
address c: 0x7ffc148d29c0

why looks different between array allocated pointer and variable allocated pointer in C LANGUAGE? [duplicate]

This question already has answers here:
Pointers - Difference between Array and Pointer
(2 answers)
Closed 1 year ago.
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 = &num;
printf("%d", *numptr2);
result: 11
i found difference!
int* numptr1 = numarr;
int* numptr2 = &num;
why variable uses "&" and why array not use "&" when i allocated?
I dont know difference... please tell me! Thanks you!
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

Pointer of pointer arithmetic?

I understand how the arithmetic of pointers works, but I have doubts about the one of the pointer of pointer:
If I have this code:
int x;
int* ptr = &x;
int** ptrptr = &ptr;
On a standard Windows system, if I do:
printf("%d",ptr);
ptr++;
printf("%d",ptr);
ptr will have a "+4" value than before, obviously because integer needs 4 bytes.
Now, if I do:
printf("%d",ptrptr);
ptrptr++;
printf("%d",ptrptr);
ptrptr will have a "+16" value than before, why? I apologize if this question has already been posted but I couldn't find out, thank you.
Code:
#include<stdio.h>
int main(void){
int x;
int* ptr = &x;
int** ptrptr = &ptr;
printf("Pointer: %p\nPointer of pointer: %p\n",(void*)ptr,(void*)ptrptr);
ptr++;
ptrptr++;
printf("Pointer: %p\nPointer of pointer: %p\n",(void*)ptr,(void*)ptrptr);
return 0;
}
Output:
Pointer: 000000000062FE44
Pointer of pointer: 000000000062FE38
Pointer: 000000000062FE48
Pointer of pointer: 000000000062FE40
The difference between 0x...FE40 and 0x...FE38 is 8, not 16, and 8 is the correct number of bytes of an address on a 64bit machine.
To hold an int (on your system) you need a box of 4 bytes. So to skip to the next item in memory you add 4 to the address of your initial int.
To hold a pointer_to_int (which is an address on your 64bit system) you need a box of 8 bytes.
A hint: the very numbers you are subtracting are 8-byte-long addresses:
0x00.00.00.00.00.62.FE.40 - 0x00.00.00.00.00.62.FE.38 = 0x08. The dots are only a visual aid.

pointer: *p and &p in C programming language [duplicate]

This question already has answers here:
C++ - *p vs &p vs p
(5 answers)
Closed 8 years ago.
Could you please tell me the difference between *p and &p in C programming language? cause I really have problem with this and I don't know whether *p or &p is ok!!!!
Just take
int a =10;
int *p = &a;
a is a variable which holds value 10. The address in which this value is stored is given by &a.
Now we have a pointer p , basically pointer points to some memory location and in this case it is pointing to memory location &a .
*p gives you 10
This is called dereferencing a pointer.
p = &a /* Gives address of variable a */
Now let's consider
&p
Pointer is a also a data-type and the location in which p is stored is given by &p
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:
type *var-name;
eg:
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Look at this program :
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_pointers.htm

What is the difference in C between &i and i if i is an array of integers? [duplicate]

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 9 years ago.
I tried a code to see what is the difference between &i and i if i is an array. My assumption was that i represents the starting address of the array, and I had no idea what will happen if I print &i as well.
The result was surprising (to me), as both i and &i was the starting address of the array.
#include<stdio.h>
int main()
{
char str[25] = "IndiaBIX";
int i[] = {1,2,3,4};
printf("%i\n", &i);
printf("%i\n", i);
return 0;
}
The result will be:
2686692
2686692
This is the same address.
But if I use a pointer to int:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int *) malloc(sizeof(int));
*j = 12;
printf("%i %i %i\n", *j,j,&j);
return 0;
}
The result will be:
12
5582744
2686748
Here I assume the first is the value of the memory area pointed to by j,the second is the address of the memory area pointed to by j, the third is the memory address of the pointer itself. If I print i and &i, why does &i not mean the memory address of the pointer i?
int i[] = {1,2,3,4};
The difference is their type, i has a type of integer array, &i has a type of a pointer of an integer array.
Yeah, both i and &i leads to print the same answer but they are not exactly same,
-> i represents the address of the first element in an array named i.
-> &i represents the address of the whole array(though values of both are same, their types are different)
For more info, please refer this [link]http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html
int ar[10];
ip = ar; /* address of first element */
ip = &ar[0]; /* address of first element */
ar10i = &ar; /* address of whole array */

Resources