|
-
November 8th, 2009, 10:54 AM
#1
Segmentation fault
This question is probably very stupid but my C is very rusty...
Basically I have this piece of code:
int* test;
int N = 3;
size_t size = sizeof(int) * N;
int *t = (int*)malloc(size);
for(int i=0;i<N;i++)
{
t[i] = i;
}
test = t;
printf("%d,%d,%d \n",test[0],test[1],test[2]);
Now I want to refactor the allocation code into a method:
void arrayTest(int * t, int N)
{
size_t size = sizeof(int) * N;
t = (int*)malloc(size);
for(int i=0;i<N;i++)
{
t[i] = i;
}
}
int* test;
arrayTest(test,3);
printf("%d,%d,%d \n",test[0],test[1],test[2]);
But I always get a segmentation fault. I thought arrays would be call by reference. So the allocation should have an effect outside of the method.
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|