小编Nar*_*esh的帖子

内存地址有多少可能还有另一个内存地址?

我已将整数变量的地址存储在指针中,然后将该先前的地址存储到另一个指针中.我无法理解它是如何工作的.

#include <iostream>

using namespace std;

#include <stdio.h>

int main ()
{
    int  var;
    int  *ptr;
    int  **pptr;

    var = 30;

    /* take the address of var */
    ptr = &var;

   /* take the address of ptr using address of operator & */
    pptr = &ptr;

    /* take the value using pptr */
    printf("Value of var = %d\n", var );
    printf("Value available at ptr = %d\n", ptr );
    printf("Value available at pptr = %d\n", pptr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers pointer-to-pointer

0
推荐指数
1
解决办法
47
查看次数

为什么它会给出错误"分段错误"?

我想在不同的内存位置输入两个字符串,但在第一次输入后,它显示错误"分段错误(核心转储").我没有弄清楚这段代码有什么问题.

#include <stdio.h>
#include<iostream>
using namespace std;

int main()
{
    char *str;
    int i;
    scanf("%s",str+0);
    scanf("%s",str+1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我只接受一个输入时它工作正常.

#include <stdio.h>
#include<iostream>
using namespace std;

int main()
{
    char *str;
    int i;
    scanf("%s",str+0);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么?

c segmentation-fault char-pointer

-4
推荐指数
1
解决办法
90
查看次数