I'm having trouble picturing what this looks like in memory which is one of problems. Maybe someone can help me out.
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
typedef struct {
char* name;
int number;
} SomeStruct;
//function prototypes
char * getbuf(SomeStruct ***ptrs);
SomeStruct * GetNextSomeStruct(void);
int main()
{
// array of pointers to SomeStruct
SomeStruct **array_ptrs = NULL;
char * b, q, *r;
b=getbuf(&array_ptrs);
//I want to do something like this
//to fill the array_ptrs with data but I keep getting errors
//array_ptrs[2][1] = GetNextSomeStruct();
//I tried to test it like this but it crashes here too
array_ptrs[2][1].number = 8;
q = *b;
}
char * getbuf(SomeStruct ***ptrs)
{
char buff[8] ;
//int n = 5; //can also be determined by user at runtime
// allocate the memory for the array
int m;
*ptrs = (SomeStruct**)malloc( sizeof( SomeStruct* )* n );
for (m = 0; m < 1; m++)
{
(*ptrs)[m] = (SomeStruct*)malloc( sizeof( SomeStruct ) * n );
}
return (char *) buff;
}
// Return the next structure (to fill the
// abovementioned array with).
SomeSeq * GetNextSomeStruct()
{
//I don't really know what I should do here
//create some data and then return it to
//array_ptrs in main.
SomeStruct *p=NULL;
p = (SomeStruct*)malloc( sizeof( SomeStruct* ) );
return p ;
}