Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems with malloc() and typedef struct

Problems with malloc() and typedef struct

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manfr3d
    wrote on last edited by
    #1

    Hello guys, I've created a struct and defined it as a new data type. However, when I want to declare a variable of this type there are several errors: SMap: Illegal use of this type as expression mySMap: undeclared identifier #include #include #ifndef NULL #define NULL 0 #endif typedef struct { int width; int height; char* data; } SMap; SMap* createMap(int width, int height); int main(int argc, char* argv[]) { int width; int height; printf("Enter width: "); scanf("%i", &width); printf("Enter height: "); scanf("%i", &height); SMap* mySMap = (SMap*)malloc(sizeof(SMap)); // the errors refer to this line return 0; } SMap* createMap(int width, int height) { SMap* mymap = (SMap*) malloc(sizeof(SMap)); if (mymap == NULL) { return NULL; } mymap->width = width; mymap->height = height; mymap->data = (char*) malloc(width*height); if (mymap->data = NULL) { free(mymap); return NULL; } return mymap; } I know that the use of malloc() isn't necessary in C++ but I want to compile the code as C-code. Does anyone know why this doesn't work, maybe it's a basic thing I forgot about? Thanks and best wishes.

    I C 2 Replies Last reply
    0
    • M Manfr3d

      Hello guys, I've created a struct and defined it as a new data type. However, when I want to declare a variable of this type there are several errors: SMap: Illegal use of this type as expression mySMap: undeclared identifier #include #include #ifndef NULL #define NULL 0 #endif typedef struct { int width; int height; char* data; } SMap; SMap* createMap(int width, int height); int main(int argc, char* argv[]) { int width; int height; printf("Enter width: "); scanf("%i", &width); printf("Enter height: "); scanf("%i", &height); SMap* mySMap = (SMap*)malloc(sizeof(SMap)); // the errors refer to this line return 0; } SMap* createMap(int width, int height) { SMap* mymap = (SMap*) malloc(sizeof(SMap)); if (mymap == NULL) { return NULL; } mymap->width = width; mymap->height = height; mymap->data = (char*) malloc(width*height); if (mymap->data = NULL) { free(mymap); return NULL; } return mymap; } I know that the use of malloc() isn't necessary in C++ but I want to compile the code as C-code. Does anyone know why this doesn't work, maybe it's a basic thing I forgot about? Thanks and best wishes.

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      Do you get different errors if you change the extension of the from between C and CPP? Then you'll be using the different compilers. Also, for the initial typedef, try this:

      struct _SMap_
      {
      int width;
      int height;
      char* data;
      } SMap;

      then, in main:

      main (...)
      {
      int n;
      ...
      n = sizeof(struct _SMap_);
      n = sizeof(SMap);
      }

      and see if any happiness results. Iain.

      M 1 Reply Last reply
      0
      • I Iain Clarke Warrior Programmer

        Do you get different errors if you change the extension of the from between C and CPP? Then you'll be using the different compilers. Also, for the initial typedef, try this:

        struct _SMap_
        {
        int width;
        int height;
        char* data;
        } SMap;

        then, in main:

        main (...)
        {
        int n;
        ...
        n = sizeof(struct _SMap_);
        n = sizeof(SMap);
        }

        and see if any happiness results. Iain.

        M Offline
        M Offline
        Manfr3d
        wrote on last edited by
        #3

        I told VS to compile the project as C-code until now this was enough. As expected the results of the sizeof()-operations were the same (12Byte). However, this didn't change anything. I've no idea what's the problem.

        1 Reply Last reply
        0
        • M Manfr3d

          Hello guys, I've created a struct and defined it as a new data type. However, when I want to declare a variable of this type there are several errors: SMap: Illegal use of this type as expression mySMap: undeclared identifier #include #include #ifndef NULL #define NULL 0 #endif typedef struct { int width; int height; char* data; } SMap; SMap* createMap(int width, int height); int main(int argc, char* argv[]) { int width; int height; printf("Enter width: "); scanf("%i", &width); printf("Enter height: "); scanf("%i", &height); SMap* mySMap = (SMap*)malloc(sizeof(SMap)); // the errors refer to this line return 0; } SMap* createMap(int width, int height) { SMap* mymap = (SMap*) malloc(sizeof(SMap)); if (mymap == NULL) { return NULL; } mymap->width = width; mymap->height = height; mymap->data = (char*) malloc(width*height); if (mymap->data = NULL) { free(mymap); return NULL; } return mymap; } I know that the use of malloc() isn't necessary in C++ but I want to compile the code as C-code. Does anyone know why this doesn't work, maybe it's a basic thing I forgot about? Thanks and best wishes.

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          In C all variables must be defined at the start of the scope they belong to. Change main() to:

          int main(int argc, char* argv[])
          {
          int width;
          int height;
          SMap* mySMap;

          printf("Enter width: ");
          scanf("%i", &width);
          printf("Enter height: ");
          scanf("%i", &height);

          mySMap = (SMap*)malloc(sizeof(SMap));

          return 0;
          }

          ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

          M 1 Reply Last reply
          0
          • C cmk

            In C all variables must be defined at the start of the scope they belong to. Change main() to:

            int main(int argc, char* argv[])
            {
            int width;
            int height;
            SMap* mySMap;

            printf("Enter width: ");
            scanf("%i", &width);
            printf("Enter height: ");
            scanf("%i", &height);

            mySMap = (SMap*)malloc(sizeof(SMap));

            return 0;
            }

            ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

            M Offline
            M Offline
            Manfr3d
            wrote on last edited by
            #5

            Thanks, this was my problem. I knew that variables must be defined at the beginning of their scope when programming micro processors, didn't know that this is also important when writing Windows programs. Thanks a lot.

            C 1 Reply Last reply
            0
            • M Manfr3d

              Thanks, this was my problem. I knew that variables must be defined at the beginning of their scope when programming micro processors, didn't know that this is also important when writing Windows programs. Thanks a lot.

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #6

              It is a C language rule, platform/os doesn't matter.

              ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups