ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im having a rather odd error and wondering if im trying to do something illegal.
I have a very simple program that defines 2 structures and then inputs code into each one. I dont get an error with the first one but i do when i use the structure definition again.
here is a snippet of what it looks like.
Code:
typedef struct {
char text[5];
char t;
} stuff;
int main()
{
stuff one;
<<adds some data to the structure elements>>
stuff two;
<<adds some data like the first one>>
return 0
}
when i attempt to compile i get error "Improper use of a typedef symbol in function main"
So is this allowed or do i need to allocate memory first with a malloc command to get it to work. Or am i just crazy and this is wrong ?
You didn't post the error message, but I suspect the (at least one) problem might be with your "typedef", and would go away if you did this:
Code:
struct stuff
{
char text[5];
char t;
}
stuff;
int
main(int argc, char *argv[])
{
struct stuff one;
<<adds some data to the structure elements>>
struct stuff two;
<<adds some data like the first one>>
return 0;
}/
PS:
Personally, I generally prefer to leave "struct's" as "struct" instead of making a typedef. I definitely encourage you to give the return value and arguments for "main()", even if you don't use them.
PPS:
No, if you declare the struct as a variable, then you don't need to call "malloc()".
On the original code it has one thats just a typeing error when i retyped it.
So its not whats causing the problem.
let me post the original code here
Code:
#include<stdio.h>
int main()
{
typedef struct{
char text[5];
int t;
} stuff;
char hold[5];
stuff tag;
sprintf(tag.text,"help");
tag.t=5;
stuff bete;
printf("%s","Please enter a word no more then 4 letters.\n");
scanf("%s",hold);
sscanf(hold,"%s",bete.text);
printf("%s","Please enter a number.\n");
scanf("%s",hold);
sscanf(hold,"%d",bete.t);
printf("info is %s %d\n",tag.text,tag.t);
printf("you typed %s %d\n",bete.text,bete.t);
return 0;
}
Funny, I tried your code and it compiles fine. Anyway, I've never seen somebody define a struct inside a function. Usually you put that in header file or at least before function definitions.
Also, correct this:
Code:
sscanf(hold,"%d",bete.t);
to
Code:
sscanf(hold,"%d",&bete.t);
You need to pass the address if you want the function to change a primitive (not array or struct, etc) variable.
i do have a return statement just forgot the ; when I typed it.
I personally wouldent use a typedef on a structure but im just refreshing my currenly learned skilles. I havent touched programming in about a month and i dont wanna forget stuff. but this error is perplexing.
Im wondering if this is due to the old compiler im trying.
Funny, I tried your code and it compiles fine. Anyway, I've never seen somebody define a struct inside a function. Usually you put that in header file or at least before function definitions.
Yea im thinking its the age of the compiler im using. As for my structure definiton i have a talent for doing wierd crap
Quote:
Originally Posted by mic
You need to pass the address if you want the function to change a primitive (not array or struct, etc) variable.
Yea my bad I knew i forgot something with that statement
If you didn't have the "typedef", you could declare and define the struct wherever you want (inside or outside the function), in any combination (separately, or all at once) you want ;-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.