could anyone tell me what's wrong about the use of pointers and malloc. =)
in function alloc_memory the memory is allocated just fine, but when it returns, the pointers test and test->test are 0x00 =(((
in function alloc_memory the memory is allocated just fine, but when it returns, the pointers test and test->test are 0x00 =(((
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *value1;
char *value2;
}TEST2;
typedef struct
{
char *value;
TEST2 *test;
} TEST;
TEST *meu_test;
void alloc_memory(TEST *test)
{
test = (TEST *) calloc(1, sizeof(TEST));
test->value = (char *)calloc(100, 1);
test->test = (TEST2 *) calloc(1, sizeof(TEST2));
test->test->value1 = (char *)calloc(100,1);
test->test->value2 = (char *)calloc(100,1);
}
void set_values(TEST *test)
{
test->value = "this is value";
test->test->value1 = "this is value 1";
test->test->value2 = "this is value 2";
}
void show_values(TEST *test)
{
printf("TEST->value: %s\n", test->value);
printf("TEST->test->value1: %s\n", test->test->value1);
printf("TEST->test->value2: %s\n", test->test->value2);
}
int main(int argc, char *argv[])
{
alloc_memory(meu_test);
set_values(meu_test);
show_values(meu_test);
system("PAUSE");
return 0;
}