[英]Passing Array as a function argument and setting elements values
So the solution to this problem I think is fairly simple but I must be missing something. 因此,我认为该问题的解决方案非常简单,但我必须缺少一些东西。 All I want to do is pass an array to a function and then set the arrays elements values in the function to then use in the global scope.
我要做的就是将数组传递给函数,然后在函数中设置数组元素值,然后在全局范围内使用。
int setArray(char* myArray) {
char localArray[9] = "abcdefghi";
for (int i = 0; i < 10; i++){
myArray[i] = localArray[i];
printf("myArray: %c\n", myArray[i]);
}
return(0);
};
int main(int argc, char* argv[])
{
char* globalArray = NULL;
setArray(globalArray);
printf("globalArray: %s\n", globalArray);
return(0);
};
The above code just crashes. 上面的代码崩溃了。 at one point I had it working where I could set the globalArray in the function and print out its values but when I printed out the values in main() it was null.
有一次,我可以在函数中设置globalArray并打印出它的值,但是当我在main()中打印出值时,它为null。
I believe the issue lies with not setting up the pointer correctly and passing it, but I am not sure 我认为问题在于无法正确设置指针并传递它,但是我不确定
any help would be greatly appreciated. 任何帮助将不胜感激。 thanks.
谢谢。
It crashes because you don't allocate memory for globalArray
. 它崩溃是因为您没有为
globalArray
分配内存。
Just add something like this: 只需添加如下内容:
globalArray = malloc(10);
before setArray()
invocation. 在
setArray()
调用之前。
Also, as it's already mentioned, your copy function is incorrect because you forgot about null-terminator when copying. 另外,如前所述,您的复制功能不正确,因为您在复制时忘记了空终止符。
The pointer globalArray
doesn't point to a valid memory location. 指针
globalArray
没有指向有效的内存位置。 You need to allocate memory and store that address in globalArray
using malloc
, calloc
etc. 您需要分配内存,并使用
malloc
, calloc
等将该地址存储在globalArray
。
You can allocate memory in main()
or in setArray()
function as dynamic memory allocation uses heap which is accessible to all the functions even after setArray
has exited. 您可以在
main()
或setArray()
函数中分配内存,因为动态内存分配使用堆,即使setArray
退出后,所有函数也可以访问该堆。
So correct code could be: 因此正确的代码可能是:
char* globalArray = malloc(10 * sizeof(char));
if(!globalArray)
{
printf("Memory could not be allocated!\n");
exit(1);
}
setArray(globalArray);
...
Also You haven't copied \\0
byte to myArray
in function setArray()
您还没有在函数
setArray()
中将\\0
字节复制到myArray
中
After the for
loop you need myArray[i] = '\\0';
在
for
循环之后for
您需要myArray[i] = '\\0';
Also you have char localArray[9] = "abcdefghi";
你也有
char localArray[9] = "abcdefghi";
in your code. 在您的代码中。
Here localArray
is not a valid c string as it doesn't contain terminating '\\0'
character. 这里的
localArray
不是有效的c字符串,因为它不包含结尾的'\\0'
字符。 Though it doesn't poise any problem for now but if you happen to print/alter localArray
as string then it'll lead to invalid memory access ( SegFault
) 尽管它暂时不会出现任何问题,但是如果您碰巧将
localArray
打印/更改为字符串,那么它将导致无效的内存访问( SegFault
)
So to avoid that use 所以避免使用
`char localArray[] = "abcdefghi";`
instead of 代替
`char localArray[9] = "abcdefghi";`
You need to either allocate memory for globalArray
to point to, or you need to declare it as an array rather than a pointer: 您需要为
globalArray
指向分配内存,或者需要将其声明为数组而不是指针:
char *globalArray = malloc( SOME_SIZE );
if ( globalArray )
{
setArray( globalArray );
...
}
free( globalArray );
or 要么
char globalArray[SOME_SIZE];
setArray( globalArray );
where SOME_SIZE
is large enough to at least hold the contents of localArray
in the setArray
function plus a trailing string terminator (0-valued byte). 其中
SOME_SIZE
大到足以至少将setArray
函数中localArray
的内容以及尾随字符串终止符(0值字节)保存起来。
Edit 编辑
The reason your code is crashing is that you pass a NULL
value for the myArray
argument, which is an invalid pointer value. 代码崩溃的原因是您为
myArray
参数传递了一个NULL
值,这是一个无效的指针值。 Attempting to dereference an invalid pointer value (in this case, through using the []
subscript operator) leads to undefined behavior ; 尝试取消引用无效的指针值(在这种情况下,通过使用
[]
下标运算符)会导致未定义的行为 ; in your case, it causes your code to crash. 在您的情况下,它将导致您的代码崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.