0

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);
    }
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
hannah
  • 889
  • 4
  • 13
  • 27
  • Can you point out which lines are getting the errors? – Barmar Mar 06 '14 at 06:00
  • 1
    `nodes[x]` has type `int`, while `v` is some type of pointer, you can't do `nodes[x] = v;`. – Yu Hao Mar 06 '14 at 06:02
  • BTW, you shouldn't cast the result of `malloc` in C. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Barmar Mar 06 '14 at 06:02
  • Yes.as barmar said, you should not use casting for malloc. Also I would suggest you to use a condition to check return value of malloc so that if the memory is not allocated, then such operation should not take place – user3256147 Mar 06 '14 at 06:08

3 Answers3

4
//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

Sakthi Kumar
  • 3,047
  • 15
  • 28
1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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];

0xF1
  • 6,046
  • 2
  • 27
  • 50