0

char * ptr="coding";

Here ptr is a character pointer which is pointing to the first character of the string enclosed in " ", that is ptr is pointing to address of 'c'.

  1. In pointers--> * Operator is used to getting the values that are stored on the address pointed by the pointer variable.

So here

On accessing *ptr --> we get the value which is stored in the address pointed by ptr, as ptr is pointing to address of 'c', so on writing this code

 char * ptr="coding";
 cout<< *ptr;

we get the output: c

on accessing *++ptr --> ++ptr increments the location by 1 , so on accessing *++ptr, we get the value which is stored in next loaction.So on writing this code

char * ptr="coding";
     cout<< *++ptr;

we get the output: o

& operator is used for getting the address that is being pointed by pointer variable, here as ptr is pointing to 'c' so on writing this code :

 const char* str = "Hello"; 

 cout << &str ;

we get the output: 0x7fff11d19cb0

Now when we write code:

 char * ptr="coding";
         cout<< ptr;

We get the output: coding

How is it printing a whole string, as ptr is only pointing to the address of the first character 'c'. Where does ptr pointing to? As ptr is a character pointer.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Kunal
  • 1
  • 2

0 Answers0