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);
    }
4
  • Can you point out which lines are getting the errors? Commented Mar 6, 2014 at 6:00
  • 1
    nodes[x] has type int, while v is some type of pointer, you can't do nodes[x] = v;. Commented Mar 6, 2014 at 6:02
  • BTW, you shouldn't cast the result of malloc in C. stackoverflow.com/questions/605845/… Commented Mar 6, 2014 at 6: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 Commented Mar 6, 2014 at 6:08

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.