Okay so I solved it by doing this:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int **test();
void printData(int**);
typedef struct
{
int **x;
}PRACTICE;
int main()
{
int i;
// int *x;
PRACTICE k;
// x = &i;
k.x = test();
printData(k.x);
}
int **test()
{
int *p;
int **y;
int i;
i = 0;
y = (int**) calloc(10,sizeof(int*));
for(i = 0; i < 10; i++)
*(y + i) = (int*)calloc(5,sizeof(int));
*(*(y + 7)+2) = 10;
// y = &p;
printf("%d\n", *(*(y + 7) + 2));
return y;
}
void printData(int**t)
{
printf("%d\n", *(*(t + 7) + 2));
}
But what if I have more varibles in struct that I need to allocate memory for. for example:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int **test();
void printData(int**);
typedef struct
{
int **x;
int **o;
}PRACTICE;
int main()
{
int i;
// int *x;
PRACTICE k;
// x = &i;
k.x = test();
printData(k.x);
}
int **test()
{
int *p;
int **y;
int i;
i = 0;
y = (int**) calloc(10,sizeof(int*));
for(i = 0; i < 10; i++)
*(y + i) = (int*)calloc(5,sizeof(int));
*(*(y + 7)+2) = 10;
// y = &p;
printf("%d\n", *(*(y + 7) + 2));
return y;
}
void printData(int**t)
{
printf("%d\n", *(*(t + 7) + 2));
}
Is there a better way to allocate memory for a struct without having to set it equal to a function everytime?
Thank you!