简体   繁体   中英

Pointer to pointer gives segmentation fault?

Here is the code

    int main
    {
    char s[]="prady";
    char **p;

    p=(char **)&s;

    printf("%u %u\n",p,*p);
    printf("%u %u\n",&s,s); 

    printf("%s\n",s);
    printf("%s\n",&s);
    printf("%u %u\n",s+1,&s+1);


    printf("%s\n",p);
    printf("%s\n",*p);
    }

o/p:

3217062327 1684107888
3217062327 3217062327
prady
prady
3217062328 3217062336
prady
Segmentation fault

My doubt as follows

  1. How both the address is same of s and &s?

  2. If both are same then how they show different when adding 1 to it?

  3. How I got segmentation fault in *p?

First, arrays are not pointers. Pointers are not arrays. Arrays decay into pointers.

1.How both the address is same of s and &s?

char s[]="prady";

   --------------------------
s: | p | r | a | d | y | \0 |
   --------------------------

The array s is a request for 6 characters to be set aside. In other words, at s there are 6 characters. 's` is a "thing", it doesn't point at anything, it just is.

char *ptr = "prady";

------         --------------------------
|*ptr|    -->  | p | r | a | d | y | \0 |
------         --------------------------

The pointer ptr requests a place which holds a pointer. The pointer can point at any char or any string literal (continuous chars).

Another way to think about this:

int b;   //this is integer type 
&b;      //this is the address of the int b, right?  

int c[]; //this is the array of ints 
&c;      //this would be the address of the array, right? 

So that's pretty understandable how about this:

*c;   //that's the first element in the array 

What does that line of code tell you? if I deference c, then I get an int. That means just plain c is an address. Since it's the start of the array it's the address of the array, thus:

c == &c;

2. If both are same then how they show different when adding 1 to it.

From my answer to #1 I assume you see why they're not the same. So why do you get different values? Look at the values you get:

s    = 0x3217062327 
s+1  = 0x3217062328  // It's 1 bigger, why? Because a char takes 1 byte, s holds chars
                     // so s (address of a char) + 1 (sizeof char) gives you one more than s
&a + 1 //This is adding 1 (sizeof array) which is bigger than the size of a char  

3. How I got segmentation fault in *p.

I think you can get this from my previous two answers... But:

  1. p is a pointer to a pointer to a character
  2. You set p to the address of the array (which remember, is the array itself)
  3. a deference of p is a pointer to a char (another address), however you can't do that to the array.

When you typecast you tell the compiler "I know better then you so just make these two work". When you segfault... it's because you didn't really know better.

In your case s isn't a pointer. It is an array!

a small change will fix a thing:

char *s = "prady";

1.How both the address is same of s and &s.

s is an array of characters. But, arrays are converted to pointers, save in a few cases: when they are used to initialize an array (eg: your char s[]="prady"; line), when they are the operand of the unary & operator (plenty of cases in your code), and when they are the operand of the sizeof operator.

2.If both are same then how they show different when adding 1 to it.

They are not the same.

2.How I got segmentation fault in *p.

p contains the address of "prady" . *p contains "prady" . Attempting to use "prady" as if it were the address of a string causes a segfault.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM