+ 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; }
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;
+ 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
+ 2
Sangeetha Santhiralingam what are you saying? I couldn't get you. My method gives you the address of the ith element.
+ 2
The question is it in value 2 in this array if that is in find that memory location
+ 2
Sangeetha Santhiralingam
Are you saying like this?
https://code.sololearn.com/cdPHBQp0RL50/?ref=app
+ 2
Use <stdlib.h> header file and then use the address operator (&) to get base address of the memory
+ 2
https://code.sololearn.com/cwGrnQA7dqha/?ref=app
Finally I code it with simple method thank you your help