LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2006, 11:41 AM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
C structures


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 ?
 
Old 03-03-2006, 11:47 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
semicolon on return
 
Old 03-03-2006, 11:51 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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; 
}/
This link might explain a bit better:
http://www.embedded.com/showArticle....icleID=9900748

'Hope that helps .. PSM

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()".

Last edited by paulsm4; 03-03-2006 at 11:57 AM.
 
Old 03-03-2006, 12:00 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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;
}
 
Old 03-03-2006, 12:04 PM   #5
mic
Member
 
Registered: Jun 2005
Posts: 44

Rep: Reputation: 16
Now that's different from your 1st post
Move this:
Code:
typedef struct{
   char text[5];
   int t;
   } stuff;
before main() and it should work.

Also, declare all local vars at the beggining of function. This is not required but increases readability and is common practice.
 
Old 03-03-2006, 12:14 PM   #6
mic
Member
 
Registered: Jun 2005
Posts: 44

Rep: Reputation: 16
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.
 
Old 03-03-2006, 12:23 PM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Mic ive done that and get the same error.


PSM

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.
 
Old 03-03-2006, 12:30 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Quote:
Originally Posted by mic
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
 
Old 03-04-2006, 12:41 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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 ;-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding structures eshwar_ind Programming 2 04-25-2005 09:18 AM
different results for sizeof structures linetnew Programming 7 04-18-2005 04:44 AM
Structures AMMullan Programming 6 02-18-2004 11:39 AM
Nested structures :S? alitrix Programming 11 11-15-2003 07:13 PM
C and arrayed structures.. miguetoo Programming 9 05-22-2003 06:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration