Reputation: 909
Why am I getting the following errors?
randmst.c: In function ‘main’:
randmst.c:41: error: variable-sized object may not be initialized
randmst.c: In function ‘createGraph’:
randmst.c:84: warning: return from incompatible pointer type
randmst.c:84: warning: function returns address of local variable
createGraph()
creates an array of pointers (called VertexPointer
) to structs
. Once it's created, I'm having trouble passing it back to main()
.
int main(int argc, char **argv){
//command line arguments
int test = atoi(argv[1]);
int numpoints = atoi(argv[2]);
int numtrials = atoi(argv[3]);
int dimension = atoi(argv[4]);
//perform trials, put results in an array
int i;
int trials[dimension];
for(i = 0; i < numtrials; i++){
VertexPointer graph[numpoint= createGraph(numpoints, dimension);
//testing
int y;
int i;
for(y = 0; y < numpoints; y++){
for(i = 0; i < dimension; i++){
printf("%f \n", (*graph[y]).loc[i]);
}
printf("\n");
}
}
}
//an array of pointers holding the vertices of the graph
VertexPointer
createGraph(int numpoints, int dimension){
//seed the psuedo-random number generator
srand(time(NULL));
//declare an array for the vertices
VertexPointer graph[numpoints];
//create the vertices in the array
int x;
int z;
for(x = 0; x < numpoints; x++){
//create the vertex
VertexPointer v;
v = (VertexPointer)malloc(sizeof(Vertex));
(*v).key = 100;
//(*v).prev = 0;
//multiple dimensions
for(z=0; z < dimension; z++){
(*v).loc[z] = rand_float();
}
//put the pointer in the array
graph[x] = v;
}
return graph;
}
Upvotes: 0
Views: 132
Reputation: 282158
C does not allow you to return arrays. Even if it did, your code declares that createGraph
returns a VertexPointer
, not an array of them. malloc
an array of pointers, change your code to return VertexPointer *
, and use a pointer instead of an array in main
.
VertexPointer *
createGraph(int numpoints, int dimension) {
...
VertexPointer *graph = malloc(numpoints * sizeof(*graph));
...
return graph;
}
int main(int argc, char **argv) {
...
VertexPointer *graph = createGraph(numpoints, dimension);
...
}
Upvotes: 1