Problems with malloc() and typedef struct
-
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. -
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.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.
-
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.
-
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.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
-
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
-
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.
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