I am trying to cast an int value in a struct to a double. I am guessing I am screwing up the parenthesis somehow. I wasn't able to find anything useful in the forum or in google.

struct START
{
    int x; 
    int y;
    double heuristic;
};

struct SHAPES
{
    int x [100]; 
    int y [100];
    double heuristic [100];
    int has_visited [100];
    int num_points;
    int *link;
};

struct LINE
{
    int x1 [100];
    int y1 [100];
    int x2 [100];
    int y2 [100];
};

double addition(double x, double y)
{
    return x + y;
}

int main(int argc, char *argv[])
{
    int kk = 0;
    double x = 0;
    double y = 0;
    double z = 0;    
    struct START start;
    struct END end;
    struct SHAPES shapes[100];
    struct LINE edges;


    x = (double)start.x;
    y = (double)edges.x1[kk];
    z = addition((double)start.x, (double)edges.x1[kk])
   return 0; 
}

Recommended Answers

All 4 Replies

Your casts are fine; the problem is that you haven't initialized the values of start.x and edges.x1[0] yet, so they have whatever garbage was in them before the program was run (or zeroes, at best, if the memory is getting cleared by the system before use).

BTW, did you see my answer to your previous question? I think you'd be able to better structure your data if you followed my earlier advice.

Oh, and I just noticed that you dropped the semi-colon at the end of line 45.

Were you able to solve the issue?

Yes. I screwed up a comma in a pass to my function. Google wasn't giving me any useful results so I assumed it was a casting issue. I also couldn't find any examples of casting a struct value either

error: invalid operands to binary & (have ‘double’ and ‘double’)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.