Dynamic array of pointers

I'm having trouble picturing what this looks like in memory which is one of problems. Maybe someone can help me out.\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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 ;
	  }
	 


 
Last edited on
Maybe try a vector of pointers?

Vectors are, if i'm not mistaken, a container defined by the Standard template library that works like an array.

Are Vecotrs C or C++? The code must be in straight C.
ah, templates are an attribute I'm pretty sure is not supported in C, so that won't work...


Try and find the source code for the vector template class, and reverse engineer it to create your own vector class. If you need any help with that just ask me, I've done stuff like it before as a test of my own skills.

I might note, the first line of your file looks to me like a c++ style header...
Last edited on
Topic archived. No new replies allowed.