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.
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
.
Just add something like this:
globalArray = malloc(10);
before setArray()
invocation.
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. You need to allocate memory and store that address in globalArray
using malloc
, calloc
etc.
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.
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()
After the for
loop you need myArray[i] = '\\0';
Also you have char localArray[9] = "abcdefghi";
in your code.
Here localArray
is not a valid c string as it doesn't contain terminating '\\0'
character. 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
)
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:
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).
Edit
The reason your code is crashing is that you pass a NULL
value for the myArray
argument, which is an invalid pointer value. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.