Let's say I declare a structure as it follows:
typedef struct {
int *numbers;
int size; // Size of numbers once treated as array
} sstruct;
And I create in main()
a pointer to structure (in order to later pass it by reference) using sstruct *example;
.
Then I have a function, call it allocpositions()
, in which I pretend to allocate memory positions for the *p
pointer contained in *example
.
If I would want to allocate positions to *example
, it would be enought to pass the direction &example
to the function, that would receive it as **a
and then do something like a = (sstruct **)malloc(N*sizeof(sstruct *))
, but I don't see how I can allocate directly to *p
inside the function.
And once allocated, would I still be able to refer the elements in *p
as example->p[index]
inside allocpositions()
?
I'd appreciate any help!
edit
Example code illustrating what I try to achieve:
typedef struct {
int *numbers;
int size; // size of numbers once treated as array
} ssm;
main() {
ssm *hello;
f_alloc(&hello);
}
void f_alloc(ssm **a) {
// Here I want to allocate memory for hello->p
// Then I need to access the positions of hello->p that I just allocated
}
Code with comments:
EDIT
I think this is what you are requiring:
Then a function to set/get
I leave doing the free bit to the reader.
EDIT 2
As I am in a good mood.
you can make it more fault tolerant to check that
malloc/realloc
have worked