Ok so these are my two functions:
Code:
int function1(struct pre_block *ppp){
int a;
int b;
int c;
int i;
a=2;
b=3;
c=a+b;
if ((ppp = (struct pre_block *)malloc(0)) == NULL) {
return EXIT_FAILURE;
}
if ((ppp->blk = (int *)malloc(0)) == NULL) {
return EXIT_FAILURE;
}
ppp->no_blk=0;
for(i=1;i<c;i++){
if ((ppp->blk = (int *)realloc(ppp->blk, sizeof(int) * (ppp->no_blk+2))) == NULL) {
return EXIT_FAILURE;
}
ppp->no_blk=ppp->no_blk+2;
}
printf("ppp->no_blk=%i", ppp->no_blk); /* this prints the correct value ppp->no_blk=8 */
return EXIT_SUCCESS;
}
int function2(struct pre_block *ppp){
int x=10;
int y;
y=(ppp->no_blk)+x;
printf("ppp->no_blk=%i", ppp->no_blk); /* this prints preblk->no_blk=0; */
return EXIT_SUCCESS;
}
I call both of them from the main script as:
Code:
...
int submain(int argc, char **argv) {
struct pre_block ppp;
if(function1(&ppp)!= EXIT_SUCCESS){
return EXIT_FAILURE;
}
printf("ppp.no_blk=%i",ppp.no_blk); /*prints 0*/
if(function1(&ppp)!= EXIT_SUCCESS){
return EXIT_FAILURE;
...
}
I get the value 0 when I print ppp->no_blk in the main function and in function2 although it should have the value 8.