LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

illegal use of type name

Hi, C++ guy trying to write C code for LabWindows/CVI, having some trouble with struct declaration/usage. The following code compiles and runs fine: (I'm aware there are existing Point typedefs, this is a contrived example of a problem I'm having with my struct usage in general)
#include <formatio.h>
#include <ansi_c.h>

typedef struct {
    int x;
    int y;
} Point2D;

int display(Point2D p);

int main(int argc, char *argv[])
{
    Point2D a;
    a.x = 1;
    a.y = 0;
    
    display(a);
   
    return 0;
}

int display(Point2D p)
{
    char msg[64];
    Fmt (msg, "%s<%s%i%s%i", "x= ", p.x, ", y= ", p.y);
    printf(msg);
    return 0;
}

The following code gives a compile error: "20, 5 Illegal use of type name 'Point2D'."
#include <formatio.h>
#include <ansi_c.h>

typedef struct {
    int x;
    int y;
} Point2D;

int display(Point2D p);

int main(int argc, char *argv[])
{
    Point2D a;
    a.x = 1;
    a.y = 0;
    
    display(a); 
   
    Point2D b;
    b.x = 2;
    b.y = 0;
    display(b);
   
    return 0;
}

int display(Point2D p)
{
    char msg[64];
    Fmt (msg, "%s<%s%i%s%i", "x= ", p.x, ", y= ", p.y);
    printf(msg);
    return 0;
}

Why can't I instance another Point2D called b? Thanks!
0 Kudos
Message 1 of 5
(5,525 Views)

Move the declaration for b under the declaration for a.  C does not allow you declare variables in the middle of your code segment like you can in C++.

int main(int argc, char *argv[])
{
    Point2D a;
    Point2D b;

    a.x = 1;
    a.y = 0;
    
    display(a);  
    
    b.x = 2;

...

 should work

Message Edited by mvr on 06-14-2007 01:18 PM

0 Kudos
Message 2 of 5
(5,526 Views)
Awesome, thanks!
0 Kudos
Message 3 of 5
(5,520 Views)

For what it is worth you can define a variable in a local code block that has a scope only within that block.

Int func(void)

{

int a;

a=0;

printf("a=%d\r\n", a);

{  // local block

   int b;

   b=20;

   printf("b=%d\r\n", b);

}

return a;

}

Can be useful when doing things in a more C++ like style. 

OT: I have no idea what causes the double spacing of lines in reply messages.

 

Message Edited by mvr on 06-14-2007 01:26 PM

0 Kudos
Message 4 of 5
(5,521 Views)
Ah I see, thanks. I'll just get into the habit of declaring everything together at the top of my functions.
0 Kudos
Message 5 of 5
(5,513 Views)