0

I have been using java for long time but for some reason I need to use C (ANSI C not C++) to write a simple code. I need to pass the pointer from outside to a function, allocate some memory to the pointer and assign some values also before the function return. I have my code like

#include <stdio.h>
#include <stdlib.h>
void test(int *a)
{
 int n=3;
 // I have to call another function t determine the size of the array
 n = estimatesize(); // n >=3
 // I tried fix size n=10 also
 a = (int*)malloc(n*sizeof(int));
 a[0] = 1;
 a[1] = 2;
 a[2] = 3;
}
void main(void)
{
 int *s=NULL;
 test(s);
 printf("%d %d %d", s[0], s[1], s[2]);
}

I don't know why the code crashes. I thought at the beginning it is estimatesize() return wrong number but even I fix n to 10, the error still there. So I cannot pass a pointer to a function for memory allocation? If so, how can I dynamically create memory inside a function and pass it out? I know it may be a safe problem in this way but I just want to know if it is possible and how to do that. Thanks.

asked Nov 4, 2013 at 6:23

1 Answer 1

2

There are two solutions to this: Either return the pointer from the function, or pass the argument by reference.

For the first one, you simply don't take any arguments, instead you return the pointer:

int *test(void)
{
 int *a = malloc(...);
 ...
 return a;
}
int main(void)
{
 int *s = test();
 ...
}

For the second one, you need to pass the address of the pointer, in other words a pointer to the pointer, using the address-of operator &:

void test(int **a)
{
 *a = malloc(sizeof(int) * 3);
 for (int i = 0; i < 3; ++i)
 (*a)[i] = i;
}
int main(void)
{
 int *s;
 test(&s);
 ...
}

The reason it doesn't work now, is because the pointer (s in main) is passed by copying it. So the function test have a local copy, whose scope is only in the test function. Any changes to a in the test function will be lost once the function returns. And as s is copied for the argument, that means that s in main never actually changes value, it's still NULL after the function call.

answered Nov 4, 2013 at 6:24
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply. I get your idea but I tried your code, it doesn't work. Again, it seems that you are implementing it in C++ but I need ANSI C and only have ANSI C compiler.
@user1285419 No, both solutions are pure C, there is no C++ in it.
@user1285419 Arrays not withstanding, remember this mantra: C is a by-value parameter language. If you want to pass a parameter by-address, then passing an address is exactly what you need to do: pass the address of the target variable and declare the parameter to be a formal pointer-to-type. And the "type" makes no difference. Whatever the target variable type is, a pointer to that type is the way the parameter should be declared ( in Joachim's code above, the type is int*). (and +1 on the answer, btw).
I think the syntax you define i in the loop only works in c++. In ANSI C, we need to define all variables before the code. Anyway, by fixing that, I have it run in now. Thanks a lot :)
@user1285419 Declaring variables in a for loop like that has been standardized for a very long time (since the C99 standard). What compiler and what version of it are you using?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.