-3

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

enter image description here

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 ?

  1. In this line

int x= *(&str + 1) - str

How difference of two address is stored in a integer data type ?

Brijesh
  • 35
  • 6
  • 4
    `*(&a + 1)` deferencing an address outside the object invokes UB – phuclv Apr 13 '22 at 16:46
  • What is the topic of the chapter in your C++ textbook where this sample code is from? These fundamentals of pointer arithmetic should be well explained in every textbook, is there something in yours' explanation that's unclear to you? Pointer arithmetic is a comparably weighty topic, that needs to be fully explained; however Stackoverflow isn't really a replacement for a textbook. Or, if you can [edit] your question, and just one, specific question about the shown code, then a concise answer must be possible. But the entire explanation of everything that happens here is just too much. – Sam Varshavchik Apr 13 '22 at 16:50
  • Note: `cout<<"line 2 is:-"<<*(&str[0]+1)< – ShadowRanger Apr 13 '22 at 17:04

1 Answers1

1

In line 1 ,How are we able to add 1 (int type) to &str which is an address data type ?

This is called pointer arithmetic. Pointers are iterators for arrays. If you have an random access iterator (such as a pointer) to an element of a range (an array in this case), and add an integer i to the iterator, the result is an iterator to the ith successive sibling of the previously pointed range element.

Now, &str is not a pointer to an element of an array, but rather a pointer to a singular object (which happens to be an array object). It's well defined to perform pointer arithmetic by adding 1 to such pointer as if it was element of an array containing a single element. The result of such addition is a pointer past the end of the object. Hence, &str and &str+1 can be used as a pair of iterators to a half-open range containing that single object. Indirecting through an iterator past the end of a range will result in undefined behaviour.

Why both the lines are showing same output ?

On the second line, in expression *(&str+1), you indirect through a pointer beyond the pointed object, and the behaviour of the program is undefind.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Useful link for explaining why `*(&str + 1)` does what it does, and why it's not the same as `*(str + 1)` or `*(&str[0] + 1)`: [Address of an array](https://stackoverflow.com/q/8412694/364696) – ShadowRanger Apr 13 '22 at 17:06
  • `*(&str +1 )` stores the address next to that of array. What I wanted to know is if `*(&a + 1)` shows an vales stored at that address , then why even `*(&str +1 )` is not behaving in same manner ? – Brijesh Apr 13 '22 at 18:55
  • @Brijesh The behaviour of the program is undefined. – eerorika Apr 13 '22 at 19:03