简体   繁体   English

指针指针给出了分段错误?

[英]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: 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? 地址如何与s和&s相同?

  2. If both are same then how they show different when adding 1 to it? 如果两者相同,那么在向它添加1时它们的显示方式有何不同?

  3. How I got segmentation fault in *p? 我怎么在* 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. 数组s是要求留出6个字符的请求。 In other words, at s there are 6 characters. 换句话说,在s有6个字符。 's` is a "thing", it doesn't point at anything, it just is. 's`是一个“东西”,它没有任何意义,只是。

char *ptr = "prady";

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

The pointer ptr requests a place which holds a pointer. 指针ptr请求保存指针的位置。 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. 如果我尊重c,那么我得到一个int。 That means just plain c is an address. 这意味着普通c就是一个地址。 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. 从我对#1的回答,我假设你明白为什么他们不一样。 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 p是指向字符的指针
  2. You set p to the address of the array (which remember, is the array itself) 你将p设置为数组的地址(记住,是数组本身)
  3. a deference of p is a pointer to a char (another address), however you can't do that to the array. p的引用是指向char(另一个地址)的指针,但是你不能对数组执行此操作。

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. 在你的情况下,s不是指针。 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. 1.地址与s和&s相同。

s is an array of characters. s是一个字符数组。 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. 但是,数组被转换为指针,除了少数情况:当它们用于初始化数组时(例如:你的char s[]="prady"; line),当它们是一元&运算符的操作数时(很多)你的代码中的案例),以及它们是sizeof运算符的操作数。

2.If both are same then how they show different when adding 1 to it. 2.如果两者相同,则在向其添加1时它们的显示方式不同。

They are not the same. 她们不一样。

2.How I got segmentation fault in *p. 2.如何在* p中出现分段错误。

p contains the address of "prady" . p包含"prady"的地址。 *p contains "prady" . *p包含"prady" Attempting to use "prady" as if it were the address of a string causes a segfault. 尝试使用"prady"就好像它是字符串的地址一样会导致段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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