修改传递给C中的函数的2D char数组

时间:2021-04-25 21:34:26

Lacking money ATM so I'm offering $0.25 via paypal to the first person to point out what I did wrong in this code snippet -- I hope this doesn't violate the site rules or insult anybody.

缺钱ATM所以我通过paypal向第一个人提供0.25美元,以指出我在这段代码中做错了什么 - 我希望这不违反网站规则或侮辱任何人。

I want to modify an multi-dimensional array in a function. It gets modified while in the function, but when scope returns to the main function the array is unchanged.

我想修改一个函数中的多维数组。它在函数中被修改,但是当scope返回main函数时,数组不变。

The function headers cannot be modified. Thanks for helping me out.

无法修改函数头。谢谢你的协助。

void getAlignment(char*s1, char*s2, char*s3, char*aligned[])
{
    /***********************
    Code here which assigns
    char**tmp to "different" "words"
    ***********************/

printf("tmp in getAlignment function\n");
printf("%s %s\n", tmp[0], tmp[1]); // prints "different words", as expected
    aligned = tmp;
}

int main(void)
{
    // skipped some code

    char** aligned = (char**)malloc(sizeof(char*)*2);
    aligned[0] = "should";
    aligned[1] = "change";

    printf("%s %s\n", aligned[0], aligned[1]); // prints "should change", as expected
    getAlignment(s1, s2, transcript, aligned); // how do i change aligned during this call?
    printf("%s %s\n", aligned[0], aligned[1]); // prints "should change"

   return 0;
}

2 个解决方案

#1


1  

When you write inside getAlignment:

在getAlignment中写入时:

aligned = (char**) malloc(2*sizeof(char*));

you are making the pointer GetAlignment::aligned point to some new memory. It no longer points to the memory that main::aligned points to. When you operate on this new memory, it has no effect on the memory that main::aligned was and is pointing to.

你正在使指针GetAlignment :: aligned指向一些新的内存。它不再指向main :: aligned指向的内存。当你对这个新内存进行操作时,它对main :: aligned所指向的内存没有影响。

(Note - the :: is not C syntax, but my meaning is to disambiguate your two variables which are both called aligned in their local scope, despite the fact that they are two separate variables).

(注意 - ::不是C语法,但我的意思是消除两个变量的歧义,这两个变量在本地范围内都被称为对齐,尽管它们是两个独立的变量)。

If your intent is that the code in getAlignmentmodifies the memory being pointed to by main::aligned then just remove the above line.

如果你的意图是getAlignment中的代码修改main :: aligned指向的内存,那么只需删除上面的行。

If your intent is that getAlignment be able to allocate new memory, and main::aligned be switched to use that new memory, then you have to pass main::aligned by reference (i.e. add an extra level of indirection in the function call). And don't forget to free() the previously-allocated memory.

如果你的意图是getAlignment能够分配新内存,并且main :: aligned被切换为使用那个新内存,那么你必须通过引用传递main :: aligned(即在函数调用中添加额外的间接级别) 。并且不要忘记释放()以前分配的内存。

BTW don't cast malloc.

BTW不会投射malloc。

#2


0  

You don't need to allocate 2D array again by aligned = (char**) malloc(2*sizeof(char*)); in function getAlignment because you already allocated it in main.

您不需要通过aligned =(char **)malloc(2 * sizeof(char *))再次分配2D数组;在函数getAlignment中,因为你已经在main中分配了它。

You require allocate each element of array like

你需要分配数组的每个元素

int alignedStrLen = strlen(s3);
aligned[0] = malloc((alignedStrLen+1)*sizeof(char));
aligned[0][alignedStrLen] = '\0';
aligned[1] = malloc((alignedStrLen+1)*sizeof(char));
aligned[1][alignedStrLen] = '\0';

And and you should free the memory allocated by malloc.

而且你应该释放malloc分配的内存。

#1


1  

When you write inside getAlignment:

在getAlignment中写入时:

aligned = (char**) malloc(2*sizeof(char*));

you are making the pointer GetAlignment::aligned point to some new memory. It no longer points to the memory that main::aligned points to. When you operate on this new memory, it has no effect on the memory that main::aligned was and is pointing to.

你正在使指针GetAlignment :: aligned指向一些新的内存。它不再指向main :: aligned指向的内存。当你对这个新内存进行操作时,它对main :: aligned所指向的内存没有影响。

(Note - the :: is not C syntax, but my meaning is to disambiguate your two variables which are both called aligned in their local scope, despite the fact that they are two separate variables).

(注意 - ::不是C语法,但我的意思是消除两个变量的歧义,这两个变量在本地范围内都被称为对齐,尽管它们是两个独立的变量)。

If your intent is that the code in getAlignmentmodifies the memory being pointed to by main::aligned then just remove the above line.

如果你的意图是getAlignment中的代码修改main :: aligned指向的内存,那么只需删除上面的行。

If your intent is that getAlignment be able to allocate new memory, and main::aligned be switched to use that new memory, then you have to pass main::aligned by reference (i.e. add an extra level of indirection in the function call). And don't forget to free() the previously-allocated memory.

如果你的意图是getAlignment能够分配新内存,并且main :: aligned被切换为使用那个新内存,那么你必须通过引用传递main :: aligned(即在函数调用中添加额外的间接级别) 。并且不要忘记释放()以前分配的内存。

BTW don't cast malloc.

BTW不会投射malloc。

#2


0  

You don't need to allocate 2D array again by aligned = (char**) malloc(2*sizeof(char*)); in function getAlignment because you already allocated it in main.

您不需要通过aligned =(char **)malloc(2 * sizeof(char *))再次分配2D数组;在函数getAlignment中,因为你已经在main中分配了它。

You require allocate each element of array like

你需要分配数组的每个元素

int alignedStrLen = strlen(s3);
aligned[0] = malloc((alignedStrLen+1)*sizeof(char));
aligned[0][alignedStrLen] = '\0';
aligned[1] = malloc((alignedStrLen+1)*sizeof(char));
aligned[1][alignedStrLen] = '\0';

And and you should free the memory allocated by malloc.

而且你应该释放malloc分配的内存。