I wrote the following programme in C++
#include<iostream>
using namespace std ;
int main()
{ int a,i , *p;
string str[4] = {"one","two","three","four"};
p = &a ; // stores address of a into p
*p = 12 ; // puts integer 4 on the adress present in p i.e a = 4
cout<<"Value is:- "<<*(&a + 1)<<endl ; // shows value stored at next address
// working on array
int x= *(&str + 1) - str ; //&str is adress , how can we add integer ?
cout<<"line 1 is:-"<<(&str + 1)<<endl; // LINE 1
cout<<"line 2 is:-"<<(*(&str+1))<<endl; // LINE 2
cout<<"size is :-"<<x;
return 0 ;
}
Below is the output
I came to know that *(&str + 1) is the immediate address after last elements of the the array & also that str stores the address of firt element.
Below are few things which I am unable to understand :-
1.
cout<<"line 1 is:-"<<(&str + 1)<<endl; // LINE 1
cout<<"line 2 is:-"<<*(&str+1)<<endl; // LINE 2
Why both the lines are showing same output ? Line 2 is having "*" operator , so it must point to a particular value stored at the address,Just like *(&a + 1)
points to a integer value stored at an address next to &a
.
Why & how *(&str+1)
showing an address data type value ?
- In this line
int x= *(&str + 1) - str
How difference of two address is stored in a integer data type ?