Answer the question
In order to leave comments, you need to log in
How to implement memory allocation (malloc) through a function?
I can't figure out how to implement memory allocation through a function. I passed the pointer, but what to do with it is not clear.
void Test(int *a){
*a = (int*)malloc(1 * sizeof(int));
}
int main(){
int *a = NULL;
setlocale(LC_ALL, "Russian");
//a = (int*)malloc(1 * sizeof(int));
Test(a);
if (a==NULL){
printf("\nМассива нет!");
} else printf ("\nМассив есть!");
free(a);
getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
To change the value of the passed element, you need to pass a pointer to it - in this case, pass a pointer to a pointer :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question