我收到以下錯誤以下行:爲什麼在這段代碼中有指針錯誤?
randmst.c:42: warning: assignment makes integer from pointer without a cast
randmst.c:43: error: incompatible types in assignment
randmst.c:44: warning: assignment makes integer from pointer without a cast
randmst.c:50: error: invalid type argument of ‘unary *’
我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function generates a random float in [0,1]
float rand_float();
//all info for a vertex
typedef struct{
int key;
int prev;
float loc;
} Vertex;
//using the pointer
typedef Vertex *VertexPointer;
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]);
//seed the psuedo-random number generator
srand(time(NULL));
//declare an array for the vertices
int nodes[numpoints];
//create the vertices in the array
int x;
for(x = 0; x < numpoints; x++){
//create the vertex
VertexPointer v;
v = (VertexPointer)malloc(sizeof(Vertex));
(*v).key = 100;
(*v).prev = NULL;
(*v).loc = rand_float;
nodes[x] = v;
}
//testing
int y;
for(y = 0; y < numpoints; y++){
printf("%f \n", (*nodes[y]).loc);
}
}
//generate a psuedo random float in [0,1]
float
rand_float(){
return (float)rand()/(RAND_MAX);
}
你能指出哪些線路出現錯誤嗎? – Barmar
'節點[x]'具有'int'類型,而'v'是某種類型的指針,不能'nodes [x] = v;'。 –
順便說一句,你不應該在C中拋出'malloc'的結果。http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Barmar