0
#include<stdio.h>
main(){

int b=90;
int*  a;
a=&b;
//pointer arith metic
printf("Address a is %d\n",a);
printf("size of integer is %d bytes\n",sizeof(int));
printf("Address a is %d\n",*a);
printf("Address a+1 is %d\n",a+1);
printf("value  of a+1 is %d\n",*(a+1));

char *ip;
ip=(char*)&b;
printf("Address ip is %d\n",ip);
}

Output of the Program :

Address a is 1495857868
size of integer is 4 bytes
Address a is 90
Address a+1 is 1495857872
value  of a+1 is 1495857868
Address ip is 1495857868
Address ip is 90

1.there is always 4 byte gap between the address of the a+1 position and and

2.The output for the value at *(a+1) and the address of variable b when the pointer converts to char becomes equal 3.Though the pointer value converts into char it shows full value of the variable

ip=(char*)&b;
printf("Address ip is %d\n",*ip);

the output:Address ip is 90

4
  • 2
    *(a+1) leads to undefined behavior... Commented Apr 20, 2016 at 7:17
  • 1
    what's your issue? Commented Apr 20, 2016 at 7:18
  • Print a pointer's value itself like this printf("Address a is %p\n",(void*)a); as opposed to the value it is pointing to. Commented Apr 20, 2016 at 7:25
  • Consider editing your question to better format it Commented Apr 20, 2016 at 8:20

3 Answers 3

1

This:

a + 1

when a has type int * will print the address incremented by the size of one int. This is known as pointer arithmetic and is one of C's most core features when it comes to pointers.

Sign up to request clarification or add additional context in comments.

Comments

1

1.there is always 4 byte gap between the address of the a+1 position

Gap bytes is determine by type of pointer: char pointer--> gap bytes are 1 byte, int pointer --> gap bytes are 4 bytes ...

In this case: variable a is int pointer --> Gap bytes are 4 bytes

==>Offset of Address (a+1) and (a) is 4 byte (1495857872 - 1495857868 = 4)

  1. The output for the value at *(a+1) and the address of variable b when the pointer converts to char becomes equal

Value at address (a+1) is can not predic, it base on your system.

  1. I run in my PC, result is:

    Address b is: 2665524

    value of a+1 is: 2665720

  2. If you change your code a little, Add (a+1) = 5; before: //pointer arith metic*

Then run --> result become:

Address b is:         2665524
value  of a+1 is:     5

3.Though the pointer value converts into char it shows full value of the variable

It show full value of the variable because value of b is 90, this value only need 1 byte to store in memory, so when convert it to char (1 byte in memory) you saw that value after convert to char equal with int value.

if you asign b > 255, ex: int b=290;

Then run --> result become:

Value a is:         290
value ip is:        34

Comments

0

You may change :

printf("Address a is %d\n",a);

to

printf("Address a is %p\n",a); //%p is specifically designed for pointers.

Till

printf("Address a+1 is %d\n",a+1);

your code should be fine. But the next statement is problematic

printf("value  of a+1 is %d\n",*(a+1));

As a is a pointer a+1 will advance a by the number of bytes int occupies in your system. Then, by doing *(a+1) you're trying to access a memory location which is not assigned for a.(Remember a is meant only to store the address of one integer when you do a=&b;).

When you're try to de-reference a memory location which you have no right to, the behavior is undefined as per C standards.

3.Though the pointer value converts into char it shows full value of the variable

ip=(char*)&b;

The statement Though the pointer value converts into char is wrong.It is just that you're treating the contents in address &b as characters. Then you have:

printf("Address ip is %d\n",*ip); 
// Remember you're printing the value not address. So change the print message

Remember that the any data is stored as bits ie zeroes or ones and it is the format specifier that you choose which determines how it is displayed. Here you are printing the contents in the &b as an integer as you have mentioned in %d in printf so you get 90 as the result. Fortunately for you the number 90 is small enough to be contained in one byte.

Suppose you changed the printf to :

printf("Value pointed to by ip is %c\n",*ip); // %d to %c

You should have got

Value pointed to by ip is Z

Here 90 is interpreted as an ASCII code which corresponds to letter Z.

3 Comments

printf("Address a+1 is %d\n",a+1); when I used %p i get the address as this **Address p is 0x7fff137fdfa4 ** and when I print :ip=(char*)&b; printf("Address ip is %d\n",*ip); the output:valueof ip is 90.
When you use %p you get the result in hexadecimal format. You can differentiate hex numbers by the prefix 0x
how the 3rd parts happen in my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.