behinddwalls
behinddwalls

Reputation: 618

Pointers manipulation in c

I have a doubt in pointers manipulation...

main()
{   int x=10;  // extra line added...
   char *p=x;

   printf("%d", &p)

   printf("%d", &p[0]);
   printf("%d" , &p[1]);
}

This code gives output

Address of 10.. 10 11

how are the last two outputs are coming.. Can anyone explain it to me ..?

Code changed.....

Upvotes: 1

Views: 2115

Answers (6)

Mahesh
Mahesh

Reputation: 34625

char *p=10; 

The code at present has undefined behavior. Pointer p is not pointing to any valid memory location to initialize 10 in that location.

With that said, both p, &p[0] yields the same memory location.

Upvotes: 0

Adarsh
Adarsh

Reputation: 78

Will explain two scenarios:

Scenario 1: char * p = x;

x == 10, p is a char pointer which is initialized with 10 (an address which user program can't access): p == 10

The value of p (ie, *p) will lead to segmentation fault (being invalid memory reference)

p[0] == *(p + 0) and &p[0] == (p+0) == p which is 10.

Hence printf("%p", &p[0]) will give you 10.

p[1] == *(p + 1) and &p[1] == (p+1)

Since, p is a character pointer, it gets increments by sizeof(char), ie 1

Hence printf("%p", &p[1]) will give you 10+1 = 11

Scenario 2: char * p = &x;

Here p is a char pointer pointing to integer x

Value of x = 10

Address of x = 1000 (assume)

Hence p = 1000

p[0] == *(p + 0) and &p[0] == (p+0) == p

Hence printf("%p", &p[0]) will give you 1000

p[1] == *(p + 1) and &p[1] == (p+1)

Since, p is a character pointer, it gets increments by sizeof(char), ie 1

ie &p[1] == 1000+1 == 1001

Upvotes: 2

Oki Sallata
Oki Sallata

Reputation: 374

   printf("%d", &p[0]);
   printf("%d" , &p[1]);

it will print out the address that p is pointered on.. because char *p=x; while x = 10;

then ,, &p[0] = 10, &p[1] = 11 &p[2] = 12 , and so on..

   printf("%d", &p);

I'm not so sure about this, but as i know it will print out the value contained in memory 10 in my case = 10000

   printf("%d", p);

it will print out the 10, which is the *p value;

cmiiw

Upvotes: 0

dpp
dpp

Reputation: 1758

p and &p[0] will be evaluated to same address (address of first element of array) which is 10.

So &p[0] will be evaluated to 10 and &p[1] to &p[0] + sizeof(char) which is 11

Your code will most likely segfault though when you dereference p (*p).

Following code will always print True.

main()
{  
    int* p; 
    printf("%s\n",p == &p[0] ? "True" : "False");
}

Upvotes: 1

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11267

Some errors in your code:

int x = 10;
char * p = &x; // error  1 - you was assigning int to pointer

printf("%p\n", &p); //  error 2 - you printing address of pointer (char **), 
                    //  so different format string is needed
printf("%p\n",  &p[0]);  // same error 2 - printing address of first byte in int x
printf("%p\n" , &p[1]);  // same error 2 - printing address of second byte in int x

Upvotes: 0

Alok Save
Alok Save

Reputation: 206518

This is Undefined Behavior.
The pointer needs to point to something valid before some value can be added to that location.

char a = 10;
char *p = &a;

Upvotes: 3

Related Questions