My question is in the context of C programming and NOT C++! I am trying to pass a pointer between multiple function. However the memory allocation should not be done by the caller. I tried a small example to simulate this. As can be seen, when the pointer points to a struct variable defined in main function, it is 'working' as expected. That is my function can manipulate the value in that memory address when the value of the address is passed. However when the function call returns and control passes on to main, why does the pointer get 'reinitialized'? Can the pointer somehow reflect the address that it was pointing to?
How can this be done?
Here is what I have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//example to pass a struct to void pointer and a void pointer to struct
//testing if memory allocation is done by callee and not caller
typedef struct mystructure{
int a;
char b;
unsigned char c[10];
}mystruct;
void func(void *var){
mystruct *s = var;
s->a = 100;
s->b = 'I';
strncpy(s->c,"test",5);
printf("In func\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
void voidOut(void *var){
mystruct *s = var;
printf("In voidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
//here is void pointer is both and 'in' and 'out' parameter
void memfunc(void *var){
mystruct *s = var;
s = (mystruct *)malloc(sizeof(mystruct));
s->a = 100;
s->b = 'I';
printf("In memfunc\n");
strncpy(s->c,"test",5);
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
//here is void pointer is an 'in' parameter
void memvoidOut(void *var){
mystruct *s = var;
printf("In memvoidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
int main(int argc, char *argv[]){
mystruct val;
func(&val);
voidOut(&val);
mystruct *ptr = NULL;
memfunc(ptr);
memvoidOut(ptr);
return 0;
}
UPDATE: Following the answers and comments, here is what I have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//example to pass a struct to void pointer and a void pointer to struct
//testing if allocation is done by callee and not caller
typedef struct mystructure{
int a;
char b;
unsigned char c[10];
}mystruct;
void func(void *var){
mystruct *s = var;
s->a = 100;
s->b = 'I';
strncpy(s->c,"test",5);
printf("In func\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
void voidOut(void *var){
mystruct *s = var;
printf("In voidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
//here is void pointer is both and 'in' and 'out' parameter
void memfunc(void **var){
mystruct *s = var;
s = (mystruct *)malloc(sizeof(mystruct));
s->a = 100;
s->b = 'I';
printf("In memfunc\n");
strncpy(s->c,"test",5);
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
//memcpy(var,s, sizeof(s));
}
//here is void pointer is an 'in' parameter
void memvoidOut(void **var){
mystruct *s = var;
printf("In memvoidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}
int main(int argc, char *argv[]){
mystruct val;
func(&val);
voidOut(&val);
mystruct *ptr = NULL;
memfunc(&ptr);
memvoidOut(&ptr);
return 0;
}
However my output is:
In func
s->a = 100
s->b = I
s->c = test
In voidOut
s->a = 100
s->b = I
s->c = test
In memfunc
s->a = 100
s->b = I
s->c = test
In memvoidOut
s->a = 0
s->b = d
s->c =
What am I missing? Shoud I define memory for the struct in memvoidOut?
void memfunc(void *var)tovoid *memFunc()....return s;