c - Passing a pointer to multiple functions and memory allocation in first function -


my question in context of c programming , not c++! trying pass pointer between multiple function. memory allocation should not done caller. tried small example simulate this. can seen, when pointer points struct variable defined in main function, 'working' expected. function can manipulate value in memory address when value of address passed. when function call returns , control passes on main, why pointer 'reinitialized'? can pointer somehow reflect address pointing to?

how can done?

here have:

#include <stdio.h> #include <stdlib.h> #include <string.h>  //example pass struct void pointer , void pointer struct //testing if memory allocation done callee , 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 void pointer both , 'in' , '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 void pointer '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 answers , comments, here have:

#include <stdio.h> #include <stdlib.h> #include <string.h>  //example pass struct void pointer , void pointer struct //testing if allocation done callee , 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 void pointer both , 'in' , '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 void pointer '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 output is:

in func s->a = 100 s->b = s->c = test in voidout s->a = 100 s->b = s->c = test in memfunc s->a = 100 s->b = s->c = test in memvoidout s->a = 0 s->b = d s->c =  

what missing? shoud define memory struct in memvoidout?

you should change signature of function allocates new memory:

void memfunc(void *var) 

to

void memfunc(void **var) 

and call main

memfunc(&ptr) 

explanation:

your memfunc function, makes copy of passed pointer ptr. initially, assigns pointer allocated space (via malloc), then, after function returns, pointer still original 1 passed memfunc (that null) because ptr pointer's copy reclaimed.

now if change proposed, address of pointer passed, not face "copy pointer" issue


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -