-2

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.

I added more info.

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; 
}
cokedude
  • 379
  • 1
  • 11
  • 21
  • 1
    This code makes no sense and would not compile. Where is `main`? Where is the assignment? Where does `kk` come from? Those two casts hanging out there outside of a function are not legal. – Ed S. Oct 15 '14 at 05:35
  • 1
    You are casting to nowhere. Please post the whole expression, where the cast is used. – M Oehm Oct 15 '14 at 05:36
  • see this for how to typecast in c ,http://www.tutorialspoint.com/cprogramming/c_type_casting.htm – smali Oct 15 '14 at 05:37
  • It should be something like: double temp = (double) start.x; Also this should be inside main() {}; – vvvv Oct 15 '14 at 05:38
  • @EdS. I added the main. I still don't know how I am casting wrong. – cokedude Oct 15 '14 at 05:50
  • @MOehm How is this not casting `(double)start.x`? – cokedude Oct 15 '14 at 05:51
  • @ali786 I did check out that tutorial. It doesn't explain how to struct values which is what I need. – cokedude Oct 15 '14 at 05:52
  • What is `edges`? PLease post an example which will compile. – Ed S. Oct 15 '14 at 05:53
  • @cokedude: I haven't said that you are not casting. The syntax is casting syntax, but the whole expression must be used somewhere, e.g. `func((double) start.x)` or `x = (double) start.x`. Depending on what you want to do, you probably don't need a cast; implicit conversion might do. – M Oehm Oct 15 '14 at 06:01
  • what error you are getting the above snippet is giving no error. – smali Oct 15 '14 at 06:06
  • @cokedude: Ah, you have updated the question. In your case, you don't need the cast; it might even obscure useful warnings. (You do compile with warnings on, don't you.) When you store the `int start.x` in the `double x`, it is converted automatically. You example still isn't very sensible. If all your struct data are `int`, your local variables should probably also be `int`. Or is there a reason you want to use a `double` here? – M Oehm Oct 15 '14 at 06:07
  • @Claptrap Did you see my edit before you commented `x = (double)start.x;`? The first `x` is a double. The second `x` is a int in my struct. – cokedude Oct 15 '14 at 06:07
  • @MOehm I use the -Wall warnings but not the -Wextra warnings. They are to picky for me. Yes there is. I want to pass them to a function that needs doubles. When I did that it gave me an error that made no sense `error: invalid operands to binary & (have ‘double’ and ‘double’)`. I assume I am not properly casting. I tried moving my cast that didn't work either x = start.(double)x. That gave me two errors that made even less sense `expected identifier before ‘(’ token` and `too few arguments to function`. It definitely has parenthesis and the proper argument count. – cokedude Oct 15 '14 at 06:19
  • what exactly is the error message you get and in which line? If you keep on changing your question it makes it difficult to answer. not quite sure why you cast again when you call addition, you already have the double values in x and y – AndersK Oct 15 '14 at 07:13

1 Answers1

0

I tried running your code with a bit of changes here and there and it works for me, except i had to comment out "(double)edges.x1[kk]" as x1 is not there in any of the structures, plus I think you need to use "shapes" instead of "edges", I don't know if you have defined "edges" in other part of you code. Below is what I tried:

#include <stdio.h>
#include <string.h>
#include <math.h>
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];
};



void main()
{
    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;

    start.x = 123;
    (double)start.x;
    edges.x1[kk] = 143;
    (double)edges.x1[kk];
    printf("%d\t%d\n",start.x,edges.x1[kk]);
    z = (sqrt(start.x+edges.x1[kk]));
    printf("%f",z);

}
JammuPapa
  • 138
  • 1
  • 1
  • 10
  • There is a structure called edges. It's a structure of type LINE. And It Has a child Called x1. – ThisaruG Oct 15 '14 at 06:11
  • @JammuPapa Sorry I added the struct. – cokedude Oct 15 '14 at 06:21
  • It wasn't a part of this question when i answered that's why i wrote "I don't know if you have defined "edges" in other part of you code." – JammuPapa Oct 15 '14 at 06:21
  • @JammuPapa Do you see how I am casting wrong? Is there a special way you need to do it in functions? – cokedude Oct 15 '14 at 06:23
  • I have changed my code, It runs fine. and to know y there is %f please refer to http://stackoverflow.com/questions/9047747/convert-int-to-double – JammuPapa Oct 15 '14 at 06:58