hannah
hannah

Reputation: 909

Why are there pointer errors in this code?

I am receiving the following errors for the following lines:

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 *’

My code:

#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);
    }

Upvotes: 0

Views: 73

Answers (3)

0xF1
0xF1

Reputation: 6116

You cannot assign integers to pointers and vice-versa without explicit typecast.

All the error statements are:

(*v).prev = NULL;      // 'prev' is 'int', 'NULL' is 'void*' type pointer
(*v).loc = rand_float; // 'loc' is 'float', 'rand_float' is 'float*' type pointer
nodes[x] = v;          // 'nodes[x]' is 'int', 'v' is 'struct Vertex *' type pointer

and

(*nodes[y]).loc        // 'nodes[y]' is already an integer and you are dereferencing it

To correct these errors, declare the variables, to which are assigning pointers, as pointer to correct type.

Example: loc should be declared as float (*loc)(); and int nodes[numpoints] should be declared as VertexPointer nodes[numpoints];

Upvotes: 0

Sakthi Kumar
Sakthi Kumar

Reputation: 3045

//declare an array for the vertices
        int nodes[numpoints];

and

44 nodes[x] = v;

but v is of type VertexPointer. The nodes array has to be an array of VertexPointer

//declare an array for the vertices
VertexPointer nodes[numpoints];

This would fix the error on line 50 as well. Also on other lines,

42           (*v).prev = NULL;

prev is an int, but u assign NULL which is a pointer. You can change prev to void * or NULL to 0

43            (*v).loc = rand_float;

rand_float is a function name, which decays to a pointer. You can change loc to void * or rand_float to rand_float() <-- see the difference here. rand_float is the pointer, but rand_float() is a function call which returns float

Upvotes: 4

Barmar
Barmar

Reputation: 780899

This is causing the error about unary *:

        printf("%f \n", (*nodes[y]).loc);

nodes[y] is an int, but * is used to dereference a pointer.

Upvotes: 1

Related Questions