Correct Typedef

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Priya Mishra

    #1

    Correct Typedef

    What is wrong in the below code,
    I get the 2 error when i compile the prog... what is mean ??


    error C2275: structure: illegal use of this type as an expression
    error C2065: str: undeclared identifier


    typedef struct
    {
    char file;
    int a;
    int b;
    int c;



    } structure;


    int func(structure *Ob,
    char *fileName);

    int main(int argc, char **argv )
    {


    int error;
    structure *str;


    error = func(&str, "abc.txt");


    return error;
    }

    Thanks
    Priya

  • Suman

    #2
    Re: Correct Typedef

    Priya Mishra wrote:[color=blue]
    > What is wrong in the below code,
    > I get the 2 error when i compile the prog... what is mean ??
    >
    >
    > error C2275: structure: illegal use of this type as an expression
    > error C2065: str: undeclared identifier[/color]
    Does that mean you are using VC++? Use a C compiler, will you, please?[color=blue]
    >[/color]
    #include <stdio.h>[color=blue]
    >
    > typedef struct
    > {
    > char file;
    > int a;
    > int b;
    > int c;
    >
    >
    >
    > } structure;[/color]
    A struct without a tag, and typedef'ing is okay on C compilers.[color=blue]
    >
    >
    > int func(structure *Ob,
    > char *fileName);
    >
    > int main(int argc, char **argv )[/color]
    Don't need the argc & argv if you are not going to use them, so
    int main(void) works.[color=blue]
    > {
    >
    >
    > int error;
    > structure *str;[/color]
    IMHO, you are trying to retrieve some information
    about some file (assuming the function declaration is sane.)
    Surely you need some memory allocated here?
    Or, atleast a valid pointer.[color=blue]
    >
    >
    > error = func(&str, "abc.txt");[/color]
    err: func takes pointer to struct, str, is a pointer to pointer to
    struct.[color=blue]
    >
    >
    > return error;
    > }
    >[/color]

    Comment

    • Suman

      #3
      Re: Correct Typedef



      Priya Mishra wrote:[color=blue]
      > What is wrong in the below code,
      > I get the 2 error when i compile the prog... what is mean ??
      >
      >
      > error C2275: structure: illegal use of this type as an expression
      > error C2065: str: undeclared identifier[/color]
      FYI:
      On copy-pasting the code in MSVC++ 6
      I did not get such an error, what I got was some other error,
      in your passing a wrong argument to func. So maybe you can explain
      a bit more about whether this is the only piece of code you tried
      compiling or not...
      [snip]

      Comment

      • Suman

        #4
        Re: Correct Typedef



        Priya Mishra wrote:[color=blue]
        > What is wrong in the below code,
        > I get the 2 error when i compile the prog... what is mean ??
        >
        >
        > error C2275: structure: illegal use of this type as an expression
        > error C2065: str: undeclared identifier
        >
        >
        > typedef struct
        > {
        > char file;
        > int a;
        > int b;
        > int c;
        >
        >
        >
        > } structure;
        >
        >
        > int func(structure *Ob,
        > char *fileName);
        >
        > int main(int argc, char **argv )
        > {
        >
        >
        > int error;
        > structure *str;
        >
        >
        > error = func(&str, "abc.txt");
        >
        >
        > return error;
        > }
        >
        > Thanks
        > Priya[/color]

        If I am not too wrong, there was another thread with the exact same
        content,
        poster's name etc. Usenet is not instant coffee, give it some time
        before recreating
        a thread. It is possible that your thread gets lost, but that is a
        rarety. I have already
        replied to the other, and it is indeed irritating to find same content
        elsethread.

        Comment

        • Mehta Shailendrakumar

          #5
          Re: Correct Typedef

          change
          error = func(&str, "abc.txt");
          to
          error = func(str, "abc.txt");

          it should work


          "Priya Mishra" <mis.priya@gmai l.com> wrote in message
          news:1121243300 .894522.131940@ g47g2000cwa.goo glegroups.com.. .[color=blue]
          > What is wrong in the below code,
          > I get the 2 error when i compile the prog... what is mean ??
          >
          >
          > error C2275: structure: illegal use of this type as an expression
          > error C2065: str: undeclared identifier
          >
          >
          > typedef struct
          > {
          > char file;
          > int a;
          > int b;
          > int c;
          >
          >
          >
          > } structure;
          >[/color]



          Comment

          • Neeraj Gupta

            #6
            Re: Correct Typedef

            Hi,

            I could figure out two problems with the code:
            1. You were trying to make a fn call without the function definition.
            2. The fn call should be made with only "str" and NOT "&str"

            This snippet should work:
            #include <malloc.h>

            typedef struct
            {
            char file;
            int a;
            int b;
            int c;



            } structure;


            int func(structure *Ob,
            char *fileName)
            {
            return 0;
            }

            int main(int argc, char **argv )
            {


            int error;
            structure *str;
            str = (structure *)malloc(sizeof (structure));

            error = func(str, "abc.txt");


            return error;

            }

            Comment

            • vsnadagouda@gmail.com

              #7
              Re: Correct Typedef

              Hello Priya,

              str is a pointer to "structure" type. If you want a pointer to this
              poniter, then the receiving end should be a pointer to pointer to
              "structure" type.(structure **Ob)

              So your program looking like:

              typedef struct
              {
              char file;
              int a;
              int b;
              int c;



              } structure;


              int func(structure **Ob,
              char *fileName);

              int main(int argc, char **argv )
              {


              int error;
              structure *str;


              error = func(&str, "abc.txt");


              return error;



              }

              But, be aware of the uninitialized pointers, like 'str'.

              Cheers
              -Vallabha

              Comment

              • Seven Kast USA

                #8
                Re: Correct Typedef

                READ ANY ONE C BOOK .. .I T WILL EXPLAIN U ......DONT COME POST
                MESSAGE IN USER NET

                Comment

                • Ken

                  #9
                  Re: Correct Typedef

                  Hi,

                  All errors were due to the definition of structure. Try this:

                  typedef struct _structure
                  {
                  char file;
                  int a;
                  int b;
                  int c;
                  } structure;

                  Comment

                  • noblesantosh@yahoo.com

                    #10
                    Re: Correct Typedef

                    I just compiles this code on gcc and it works absolutly fine without
                    any errors, just few warning messeges. which compiler are you using?

                    Comment

                    • noblesantosh@yahoo.com

                      #11
                      Re: Correct Typedef

                      Priya, I don't know which compiler you are using to compile this code,
                      here I'm using gcc and it is not giving any errors only one warning
                      that is

                      17: warning: passing arg 1 of `func' from incompatible pointer type

                      The problem is in function call " error = func(&str, "abc.txt"); "
                      correct it to " error = func(str, "abc.txt"); " removing &.
                      Now it is working fine.

                      Santosh.

                      Comment

                      • Yohji

                        #12
                        Re: Correct Typedef



                        Priya Mishra wrote:[color=blue]
                        > What is wrong in the below code,
                        > I get the 2 error when i compile the prog... what is mean ??
                        >
                        >
                        > error C2275: structure: illegal use of this type as an expression
                        > error C2065: str: undeclared identifier
                        >
                        >
                        > typedef struct
                        > {
                        > char file;
                        > int a;
                        > int b;
                        > int c;
                        >
                        >
                        >
                        > } structure;
                        >
                        >
                        > int func(structure *Ob,
                        > char *fileName);
                        >
                        > int main(int argc, char **argv )
                        > {
                        >
                        >
                        > int error;
                        > structure *str;
                        >
                        >
                        > error = func(&str, "abc.txt");
                        >
                        >
                        > return error;
                        > }
                        >
                        > Thanks
                        > Priya[/color]

                        Er,wrong here:

                        error = func(&str, "abc.txt");

                        'str' is of structure * type,so '&str' is structure ** .But the
                        prototype of func is:

                        int func(structure *,
                        char *);

                        Comment

                        • Lawrence Kirby

                          #13
                          Re: Correct Typedef

                          On Wed, 13 Jul 2005 03:51:33 -0700, noblesantosh wrote:
                          [color=blue]
                          > I just compiles this code on gcc and it works absolutly fine without
                          > any errors, just few warning messeges. which compiler are you using?[/color]

                          Warning messages are diagnostics just as much as error messages. The
                          comnpiler warned you that there is something wrong with the code, whether
                          it decides to continue compiling or not isn't particularly significant.

                          Lawrence



                          Comment

                          • Lawrence Kirby

                            #14
                            Re: Correct Typedef

                            On Wed, 13 Jul 2005 01:44:11 -0700, Suman wrote:
                            [color=blue]
                            > Priya Mishra wrote:[color=green]
                            >> What is wrong in the below code,
                            >> I get the 2 error when i compile the prog... what is mean ??
                            >>
                            >>
                            >> error C2275: structure: illegal use of this type as an expression
                            >> error C2065: str: undeclared identifier[/color]
                            > Does that mean you are using VC++? Use a C compiler, will you, please?[/color]

                            VC++ is a C compiler if invoked correctly.
                            [color=blue]
                            > #include <stdio.h>[/color]

                            You might as well drop this because nothing in the code needs it.

                            Lawrence

                            Comment

                            • David Resnick

                              #15
                              Re: Correct Typedef



                              Priya Mishra wrote:[color=blue]
                              > What is wrong in the below code,
                              > I get the 2 error when i compile the prog... what is mean ??
                              >
                              >
                              > error C2275: structure: illegal use of this type as an expression
                              > error C2065: str: undeclared identifier
                              >
                              >
                              > typedef struct
                              > {
                              > char file;
                              > int a;
                              > int b;
                              > int c;
                              >
                              >
                              >
                              > } structure;
                              >
                              >
                              > int func(structure *Ob,
                              > char *fileName);
                              >
                              > int main(int argc, char **argv )
                              > {
                              >
                              >
                              > int error;
                              > structure *str;
                              >
                              >
                              > error = func(&str, "abc.txt");
                              >
                              >
                              > return error;
                              > }
                              >
                              > Thanks
                              > Priya[/color]

                              I'm wondering if your compiler uses structure as a keyword
                              or something odd. The only problem I see is that you have
                              a prototype for func taking a structure* and you are passing
                              it a structure**. Compiles fine for me (gcc) once that is
                              repaired and func is defined.

                              -David

                              Comment

                              Working...