How to find memory location of the element in a array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to find memory location of the element in a array

Please anyone help me #include <iostream> using namespace std; int main() { int i; int arr[5]={1,2,3,4,5}; for(int i=0;i<=4;i++) cout<<arr[i]; return 0; }

3rd Jan 2022, 2:51 AM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
8 Answers
+ 4
A simple way, use the address of operator (&) For example, to output the address of arr[i], the code snippet will be similar to the below one: cout<<&(arr[i]); As an alternative way, we can use the property that the array identifier itself is an address and points to the first element. So you can get the address of the ith element by adding i to the base address(arr). The below code snippet produces the same output as the above one: cout<<arr+i;
3rd Jan 2022, 4:05 AM
Rishi
Rishi - avatar
+ 2
Yes it's ok thanks But I want find the number if that is in Cout that memory location for example arr[1] =2 where is 2
3rd Jan 2022, 4:11 AM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
+ 2
Sangeetha Santhiralingam what are you saying? I couldn't get you. My method gives you the address of the ith element.
3rd Jan 2022, 4:13 AM
Rishi
Rishi - avatar
+ 2
The question is it in value 2 in this array if that is in find that memory location
3rd Jan 2022, 4:15 AM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
3rd Jan 2022, 4:21 AM
Rishi
Rishi - avatar
+ 2
Use <stdlib.h> header file and then use the address operator (&) to get base address of the memory
3rd Jan 2022, 7:58 AM
N. Vimukthi Dilshan Fernando
+ 2
https://code.sololearn.com/cwGrnQA7dqha/?ref=app Finally I code it with simple method thank you your help
3rd Jan 2022, 9:08 AM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
3rd Jan 2022, 4:14 AM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar