Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指针到指针?_C_Pointers - Fatal编程技术网

C 指针到指针?

C 指针到指针?,c,pointers,C,Pointers,我在学C,现在是指针 #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; int f1(int **w){ for (int i=0;i<2;i++){ for (int j=0;j<10;j++){ w[i][j]=10; printf("%d ",w[i][j]);

我在学C,现在是指针

#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int f1(int **w){
    for (int i=0;i<2;i++){
        for (int j=0;j<10;j++){
            w[i][j]=10;
            printf("%d ",w[i][j]);
        }
        printf("\n");
    }
    printf("----\n");
}

int main () {


    int **w = (int **) malloc(sizeof(int*)*2);
    for (int i=0;i<2;i++)
        w[i] = (int*)malloc(sizeof(int)*10);

    for (int i=0;i<2;i++){
        for (int j=0;j<10;j++){
            w[i][j]=i*10 + j;
            printf("%d ",w[i][j]);
        }
        printf("\n");
    }
    printf("---\n");
    f1(w);
        for (int i=0;i<2;i++){
        for (int j=0;j<10;j++){
            w[i][j]=i*10 + j;
            printf("%d ",w[i][j]);
        }
        printf("\n");
    }

    return 0;
}
我想知道,为什么数组值(见最后两行)不同于10?
我猜,没有发送正确的指针,但是,在这种情况下,数组的存储位置是10…10?,它是神奇地创建的吗


谢谢

看来这个程序正是按照你的程序做的

f1函数将所有值设置为10

 w[i][j]=10;
其他地方把它设置为

w[i][j]=i*10 + j;

这说明了最后一组中的整个输出范围。

输出一定是错误的

它应该是:

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19

使用
w[i][j]=i*10+j显式重新分配这些值在打印它们之前。。。什么不清楚?
for (int i=0;i<2;i++){
    for (int j=0;j<10;j++){
        w[i][j]=i*10 + j;
        printf("%d ",w[i][j]);
    }
    printf("\n");
}
0*10 + 0 = 0... 
0*10 + 9 = 9...
1*10 + 0 = 10...
1*10 + 9 = 19
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19