在C中初始化2d数组

时间:2022-08-28 21:29:29

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?

我正在尝试初始化一个二维数组,以便在[0] [0]它等于0而在[9] [9]它等于99.按照现在的方式,[0] [0-9]全部相等0然后[1] [0-9]都等于1.我怎样才能以我提到的方式正确加载这个数组?

2 个解决方案

#1


0  

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

#2


0  

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;

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

#1


0  

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

#2


0  

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;

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