How to find memory address of a variable in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to find memory address of a variable in c

like if mark is an array .To find its memory location we can find it by printf("%d",mark); how to find it for a variable in c language

14th Feb 2017, 7:54 AM
P.Sree Ram Keerthan
P.Sree Ram Keerthan - avatar
5 Answers
+ 1
I'm guessing that you want to print the address of the array. Instead of %d which is uses for decimal numbers you would use %p for pointer addresses. It should look like this: printf("%p", mark); @VEdward From what I know, there isn't any std::cout in C rather than C++.
14th Feb 2017, 8:24 AM
R- Ry
R- Ry - avatar
+ 1
int x = 5; // created int variable and set to 5 int *ptr = &x; //created a pointer of int type, pointing to the address of variable x. printf("%p",ptr); // printing the address of variable x. // ptr holds the address of x, not the value of x which is 5. // if x was an array you would just use x because the name of an array is actually a pointer. printf("%p",x);
14th Feb 2017, 9:59 AM
Michael W
Michael W - avatar
0
by using ampersand (&) int number = 5; cout << &number ; this will give you memory location of number variable
14th Feb 2017, 8:08 AM
Pankaj Vaghela
Pankaj Vaghela - avatar
0
if mark is an array, then mark is a "shortcut" for &mark[0] so: mark mark + 0 &mark[0] are the same thing, the address of the first element.
14th Feb 2017, 8:25 AM
bem
bem - avatar
0
well @R-ry you can use whatever it has.. xD if it has printf() use printf() if put() then use put() or print() xD
14th Feb 2017, 8:40 AM
Pankaj Vaghela
Pankaj Vaghela - avatar