more questions on the ANSI standard...

[b][red]This message was edited by Gregry2 at 2006-1-20 4:3:37[/red][/b][hr]
Man, I think I need a good reference...

empty unions/structs are legal I know in C++, however, they aren't in ANSI C, are they? Only in C99, correct? Grrr....

EDIT: I need a function that can be like strtod, but for FLOATs only...see, I have a union type that is a union of a long, two pointers, and a float. All make the size of the union to simply be 4 on the x86.

Well, I have to go through alot, but basically, I'd need to reduce the percision to a `single' as they call it. I just need the checking for the function like strtod() to put ERANGE on errno not when its outside the range of a double, but a float.

banah.
{2}rIng


Comments

  • : empty unions/structs are legal I know in C++, however, they aren't in ANSI C, are they? Only in C99, correct? Grrr....

    [blue]It is unspecified behavior in all C versions, including C99.
    [/blue]
  • No, I understand your post, its just my compiler.

    See, I did this test.
    [code]
    typedef struct lamer{
    int a;
    union{
    long b;
    char* l;
    };
    }Lamer;

    #include
    #include

    int main(){
    Lamer a;
    a.a=3;
    a.b=7;
    a.l="hello";
    printf("%d,%d,%s>>>",a.a, a.b, a.l);
    system("pause");
    }
    [/code]

    It compiles correctly. BTW, I use Mingw, with dev-C++, and I'm calling gcc from the commandline. Now, when I call it with

    gcc -o sin sin.c

    it compiles. When I add the -ansi flag however, which obviously forces the ansi standard, it complains first that the union within Lamer doesn't declare anything, then says `b' and `l' in the next lines aren't members of Lamer.

    I guess I could understand that, but when calling with -std=c99, which's purpose is obvious, it gives the same errors.

    Also, gcc, by file ext, can instead call g++, the cpp compiler. So, i tried it with g++, and it worked. However, when I forced the language on the gcc with the arg "-x c", it compiled again, with no errors.

    So now, I'm wondering, what standard is my compiler following, its own? I know it gives a few extensions to the language under normal conditions, so is this part of it? I'm sure a few here have dev-c++, so just tell me, please.

    thanx
    {2}rIng
  • : No, I understand your post, its just my compiler.
    :
    : See, I did this test.
    : [code]
    : typedef struct lamer{
    : int a;
    : union{
    : long b;
    : char* l;
    : };
    : }Lamer;
    :
    : #include
    : #include
    :
    : int main(){
    : Lamer a;
    : a.a=3;
    : a.b=7;
    : a.l="hello";
    : printf("%d,%d,%s>>>",a.a, a.b, a.l);
    : system("pause");
    : }
    : [/code]
    :
    : It compiles correctly. BTW, I use Mingw, with dev-C++, and I'm calling gcc from the commandline. Now, when I call it with
    :
    : gcc -o sin sin.c
    :
    : it compiles. When I add the -ansi flag however, which obviously forces the ansi standard, it complains first that the union within Lamer doesn't declare anything, then says `b' and `l' in the next lines aren't members of Lamer.
    :
    : I guess I could understand that, but when calling with -std=c99, which's purpose is obvious, it gives the same errors.
    :
    : Also, gcc, by file ext, can instead call g++, the cpp compiler. So, i tried it with g++, and it worked. However, when I forced the language on the gcc with the arg "-x c", it compiled again, with no errors.
    :
    : So now, I'm wondering, what standard is my compiler following, its own? I know it gives a few extensions to the language under normal conditions, so is this part of it? I'm sure a few here have dev-c++, so just tell me, please.
    :
    : thanx
    : {2}rIng
    :


    [blue]I think I misunderstood what you asked for. Empty struct/union is this:

    struct myStruct{};


    What you are talking about is anonymous declaration of struct/union.
    I'm not sure whether it is part of the C standard or not. It should be part of the C++ standard.

    And yes, gcc/g++ follows it's own standard, but the -ansi switch should probably take care of that.
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories