我问了一个问题: 使用C通过引用传递数组 。 我意识到我的问题是在C中使用指针星。最后事实证明,这个方法适用于我的程序:
#include <stdio.h> void FillArray(int** myArray) { *myArray = (int*) malloc(sizeof(int) * 2); (*myArray)[0] = 1; (*myArray)[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }到目前为止,每件事情都很好。 然后,我修改了FillArray()函数,如下所示,以获得更好的代码可读性:
#include <stdio.h> void FillArray(int** myArray) { int* temp = (*myArray); temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }现在,我在printf行遇到以下运行时错误:
Trial.exe中0x773115de处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。
尽管我不是C专家,但修改它似乎是合法的。 但是,显然它不起作用。 语法有点混乱吗? 我在这里想念一下吗?
谢谢你的有用答案,
SAIT。
I asked the question: Pass array by reference using C. I realized my problem was the usage of the pointer star in C. And eventually it turned out that, this method works fine for my program:
#include <stdio.h> void FillArray(int** myArray) { *myArray = (int*) malloc(sizeof(int) * 2); (*myArray)[0] = 1; (*myArray)[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }Everyting was fine up to that point. Then, I modified the FillArray() function like the following for a better code readability:
#include <stdio.h> void FillArray(int** myArray) { int* temp = (*myArray); temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }Now, I get the following run-time error at the printf line:
Unhandled exception at 0x773115de in Trial.exe: 0xC0000005: Access violation reading location 0x00000000.
Even though I'm not an C expert, it seemed legitimate to do this modifying. However, apparently it doesn't work. Isn't the syntax a little bit confusing? Do I miss something here?
Thanks for your helpful answers,
Sait.
最满意答案
temp获取myArray地址的副本,但是你为temp分配了一些malloc ed内存,所以原来的赋值是没有意义的,没有持久的效果。 然后修改malloc ed内存,但这根本不会改变myArray 。 要在main更改myArray ,您必须分配
*myArray = temp;在FillArray结束时。
void FillArray(int** myArray) { int* temp; temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; *myArray = temp; }做你想要的。
temp gets a copy of the address of myArray, but then you assign some malloced memory to temp, so the original assignment was pointless and had no lasting effect. You then modify the malloced memory, but that doesn't change myArray at all. To change myArray in main, you have to assign
*myArray = temp;at the end of FillArray.
void FillArray(int** myArray) { int* temp; temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; *myArray = temp; }does what you intend.
C中指针星的用法(The usage of pointer star in C)我问了一个问题: 使用C通过引用传递数组 。 我意识到我的问题是在C中使用指针星。最后事实证明,这个方法适用于我的程序:
#include <stdio.h> void FillArray(int** myArray) { *myArray = (int*) malloc(sizeof(int) * 2); (*myArray)[0] = 1; (*myArray)[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }到目前为止,每件事情都很好。 然后,我修改了FillArray()函数,如下所示,以获得更好的代码可读性:
#include <stdio.h> void FillArray(int** myArray) { int* temp = (*myArray); temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }现在,我在printf行遇到以下运行时错误:
Trial.exe中0x773115de处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。
尽管我不是C专家,但修改它似乎是合法的。 但是,显然它不起作用。 语法有点混乱吗? 我在这里想念一下吗?
谢谢你的有用答案,
SAIT。
I asked the question: Pass array by reference using C. I realized my problem was the usage of the pointer star in C. And eventually it turned out that, this method works fine for my program:
#include <stdio.h> void FillArray(int** myArray) { *myArray = (int*) malloc(sizeof(int) * 2); (*myArray)[0] = 1; (*myArray)[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }Everyting was fine up to that point. Then, I modified the FillArray() function like the following for a better code readability:
#include <stdio.h> void FillArray(int** myArray) { int* temp = (*myArray); temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; } int main() { int* myArray = NULL; FillArray(& myArray); printf("%d", myArray[0]); return 0; }Now, I get the following run-time error at the printf line:
Unhandled exception at 0x773115de in Trial.exe: 0xC0000005: Access violation reading location 0x00000000.
Even though I'm not an C expert, it seemed legitimate to do this modifying. However, apparently it doesn't work. Isn't the syntax a little bit confusing? Do I miss something here?
Thanks for your helpful answers,
Sait.
最满意答案
temp获取myArray地址的副本,但是你为temp分配了一些malloc ed内存,所以原来的赋值是没有意义的,没有持久的效果。 然后修改malloc ed内存,但这根本不会改变myArray 。 要在main更改myArray ,您必须分配
*myArray = temp;在FillArray结束时。
void FillArray(int** myArray) { int* temp; temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; *myArray = temp; }做你想要的。
temp gets a copy of the address of myArray, but then you assign some malloced memory to temp, so the original assignment was pointless and had no lasting effect. You then modify the malloced memory, but that doesn't change myArray at all. To change myArray in main, you have to assign
*myArray = temp;at the end of FillArray.
void FillArray(int** myArray) { int* temp; temp = (int*) malloc(sizeof(int) * 2); temp[0] = 1; temp[1] = 2; *myArray = temp; }does what you intend.
发布评论