简体   繁体   English

如何遍历并在C中复制双指针

[英]How to iterate through and copy double pointer in C

I've created a two dimensional integer array in C, initialised it with values, and then cast it to an int** (I have to do that because it's for homework). 我已经在C语言中创建了一个二维整数数组,并使用值对其进行了初始化,然后将其转换为int** (我必须这样做,因为它是用于家庭作业)。
I've managed to iterate through it and set all of the values to 0. However, when I iterate through it again and print the values it, the output is not all zeros. 我已经设法对其进行迭代并将所有值都设置为0。但是,当我再次对其进行迭代并打印其值时,输出结果并非为零。

Here is a minimum working example: 这是一个最小的工作示例:

#include <string.h>
#include <stdio.h>

#define ROWS    3
#define COLS    2

int main(void)
{
    /* Create array and convert to double pointer */
    int c[ROWS][COLS] = {{1,2},{3,5},{8,13}};
    int **ptr = (int **)c;

    /* Loop through entire array and print then double each value. */
    int *temp[ROWS];
    for(int i = 0; i<ROWS; i++){
        temp[i] = (int*)(ptr+i);
        for(int j = 0; j<COLS; j++){
            printf("Before setting: %i\n", temp[i][j]);
            temp[i][j] = temp[i][j]*2;
        }
    }

    /* Copy temp back into ptr */
    memcpy(ptr, temp, sizeof(ptr));

    /* Loop through array and print values */
    int *temp2[ROWS];
    for(int i = 0; i<ROWS; i++){
        temp2[i] = (int*)(ptr+i);
        for(int j = 0; j<COLS; j++){
            printf("After setting: %i\n", temp2[i][j]);
        }
    }

}

The issue is that the results are not what I would expect. 问题是结果不是我所期望的。 One time I ran it, this was the output: 一次运行它,这是输出:

Before setting: 1 设置前:1
Before setting: 2 设定前:2
Before setting: 3 设定前:3
Before setting: 5 设定前:5
Before setting: 8 设定前:8
Before setting: 13 设定前:13
After setting: -1193330832 设置后:-1193330832
After setting: 32764 设置后:32764
After setting: 6 设定后:6
After setting: 10 设定后:10
After setting: 16 设定后:16
After setting: 26 设定后:26

The value 32764 is the same every time the program is run, but the value -1193330832 changes each time (I assume it is the memory address of the array). 每次运行程序时,值32764都是相同的,但是值-1193330832每次-1193330832更改(我假设它是数组的内存地址)。

The output I was expecting is: 我期望的输出是:

Before setting: 1 设置前:1
Before setting: 2 设定前:2
Before setting: 3 设定前:3
Before setting: 5 设定前:5
Before setting: 8 设定前:8
Before setting: 13 设定前:13
After setting: 1 设置后:1
After setting: 4 设定后:4
After setting: 6 设定后:6
After setting: 10 设定后:10
After setting: 16 设定后:16
After setting: 26 设定后:26
because the values in the first loop have been doubled. 因为第一个循环中的值已加倍。

What have I done wrong? 我做错了什么? Why are the values changing and how should I actually go about fixing this? 为什么值会改变,我实际上应该如何解决呢?

(PS the homework doesn't involve finding a way to iterate through the double pointer, but I need to be able to do it to complete the actual task) (PS的作业不涉及寻找一种通过双指针进行迭代的方法,但是我需要能够做到这一点才能完成实际的任务)

int **ptr = (int **)c; isn't a valid pointer conversion, since you cannot use a pointer-to-pointer to point at a 2D array. 这不是有效的指针转换,因为您不能使用指针对指针指向2D数组。 Because it has nothing to do with 2D arrays. 因为它与2D阵列无关。

Instead you can use a pointer to a 2D array, int (*)[ROWS][COLS]; 相反,您可以使用指向2D数组的指针int (*)[ROWS][COLS]; . The most convenient is however to use a pointer to a 1D array and have it point at the first element of the 2D array: 但是,最方便的方法是使用指向1D数组的指针并将其指向2D数组的第一个元素:

int (*ptr)[COLS] = &c[0];

...

ptr[i][j] = ...;

Fixed example: 固定示例:

#include <string.h>
#include <stdio.h>

#define ROWS    3
#define COLS    2

int main(void)
{
    /* Create array and convert to double pointer */
    int c[ROWS][COLS] = {{1,2},{3,5},{8,13}};
    int (*ptr)[COLS] = &c[0];

    /* Loop through entire array and print then double each value. */
    for(int i = 0; i<ROWS; i++){
        for(int j = 0; j<COLS; j++){
            printf("Before setting: %i\n", ptr[i][j]);
            ptr[i][j] = ptr[i][j]*2;
        }
    }

    /* Loop through array and print values */
    for(int i = 0; i<ROWS; i++){
        for(int j = 0; j<COLS; j++){
            printf("After setting: %i\n", ptr[i][j]);
        }
    }
}

(A matter of style, but your order of ROWS and COLS is a bit weird, it is more common to do int[COLS][ROWS] then for(i=0; i<COLS; i++) ) (关于样式,但是您对ROWS和COLS的顺序有些奇怪,通常先进行int[COLS][ROWS]然后再for(i=0; i<COLS; i++)

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

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