简体   繁体   English

在C中初始化2d数组

[英]Initializing a 2d array in C

Here is my code: 这是我的代码:

int main() { 
    int x, y; 
    int *xptr, *yptr; 
    int array[10][10]; 
    int j; 
    int k; 
    int z = 0; 

    for(j = 0; j < 10; j++) { 
        for(k = 0; k < 10; k++) { 
            array[j][k] = j * 10 + k; 
        } 
    } 

    xptr = &array[0][0]; 

    for(j = 0; j < 10; j++) { 
        for(k = 0; k < 10; k++) { 
            printf("array[%d][%d] = %d \n", j, k, *(xptr + j), (xptr + k)); 
        } 
    } 

    system("PAUSE"); 
}

I am trying to initialize a 2d array so that at [0][0] it equals 0 and at [9][9] it equals 99. With the way that it is now, [0][0-9] all equal 0 and then [1][0-9] all equal 1. How would I properly load this array in the fashion that I mentioned? 我正在尝试初始化2d数组,以便[0] [0]等于0,[9] [9]等于99。就现在而言,[0] [0-9]都等于0,然后[1] [0-9]都等于1。如何以我提到的方式正确加载此数组?

for(j = 0; j < 10; j++) {
      for(k = 0; k < 10; k++) {
            array[j][k] = j*10 + k;
      }
}

I'm assuming you've actually declared everything, but didn't include it in the example. 我假设您实际上已经声明了所有内容,但未在示例中包括它。 You simply want 你只是想要

array[j][k] = j*10 + k;

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

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