使用指针而不是数组 [英] Using pointers instead of an array

查看:100
本文介绍了使用指针而不是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前,我使用数组编写了该程序,现在我需要使用指针而不是数组.我不确定该怎么做,因此任何提示都将不胜感激!谢谢! :D

I wrote this program a few weeks ago using arrays and now I need to use pointers instead of using arrays. I'm not exactly sure how to go about doing that so any tips would be appreciated! Thanks! :D

这是代码:

#include <stdio.h>

int showArray(int row);
int exchangeRow(int row1, int row2);

int x, y;
int array[10][10];
int j;
int k;
int inputrow;
int inputcolumn;
int scanrow;
int temp;
int row1;
int row2;

int main() {

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

    // Print out selected row
    printf("Type in a number for the corresponding row to be printed \n");
    scanf("%d", &inputrow);
    if(inputrow >= 0 && inputrow < 10) {
             for(j = 0; j < 10; j++) {
                   printf("%d ", array[inputrow][j]);
             }
    }
    printf("\n \n");

    //Print out selected column
    printf("Type in a number for the corresponding column to be printed \n");
    scanf("%d", &inputcolumn);
    if(inputcolumn >= 0 && inputcolumn < 10) {
             for(j = 0; j < 10; j++) {
                   printf("%d ", array[j][inputcolumn]);
             }
    }
    printf("\n \n");

    printf("Type in a number for the row that method showArray will print \n");
    scanf("%d", &scanrow);
    showArray(scanrow);
    printf("\n \n");

    printf("Type in two numbers for the rows that method exchangeRow will switch \n");
    scanf("%d %d", &row1, &row2);
    exchangeRow(row1, row2);
    printf("\n \n");

    system("PAUSE");
}

int showArray(int row) {
    for(j = 0; j < 10; j++) {
           printf("%d ", array[row][j]);
     }
}

int exchangeRow(int row1, int row2) {
    if(row1 >= 0 && row1 < 10 && row2 >= 0 && row2 < 10) {
           temp = row1;
           row1 = row2;
           row2 = temp;
           printf("The first row now holds the values: ");
           showArray(row1);
           printf("\n");
           printf("The second row now holds the values: ");
           showArray(row2);
    }
}

推荐答案

我认为您的意思是使用动态内存分配" ...

I take it you mean "using dynamic memory allocation"...

许多人动态地处理2D​​数组的方式是这样的:

The way a lot of people do 2D arrays dynamically is like this:

const size_t nrows = 10, ncols = 10;

int **array = malloc( nrows * sizeof(int*) );
for( i = 0; i < nrows; i++ ) {
    array[i] = malloc( ncols * sizeof(int) );
}

但是我讨厌这个.如果要执行生产代码,这可能会很慢.内存用完的情况也更难处理,而且无法保证阵列的局部性.另外,免费是丑陋的:

But I hate this. If you are doing production code, this can be very slow. It's also harder to handle the case where you run out of memory, and there's no guaranteed locality of your array. Plus, it's ugly to free:

for( i = 0; i < nrows; i++ ) free(array[i]);
free(array);

在内存中,您的静态array[10][10]是一个连续的块.所以你应该做同样的事情:

In memory, your static array[10][10] is one contiguous block. So you should do the same:

int **array = malloc( nrows * sizeof(int*) );
array[0] = malloc( nrows * ncols * sizeof(int) );
for( i = 1; i < nrows; i++ ) {
    array[i] = array[i-1] + ncols;
}

要释放它:

free(array[0]);
free(array);

我通常会更进一步,只进行一次内存分配,而不是两次.这样,我只有一个指针.但是我不会在这里做.您必须对对齐有一点了解,而代码则有些杂乱无章.这是通常不需要的优化.

I often take this a step further, and do a single memory allocation instead of two. That way I have just one pointer. But I won't do that here. You have to be a little conscious of alignment, and the code is a little messier. It's an optimization that usually you don't need.

希望有帮助.

这篇关于使用指针而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆