Thread: help me with structures using functions

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Unhappy help me with structures using functions

    I am just learning to use functions in structures can any one please explain me the error in the following code and rectify it.

    Code:
    #include<stdio.h>
    
    struct shop{
    int price;
    }
    
    struct shop funcone(void)
    void functwo(struct shop var1)
    
    int main()
    {struct shop var2;
    var2=funcone()
    functwo(var2);
    return 0;
    
    }
    void functwo(struct shop var1)
    { printf("price=%d\n"var1.price); 
    }
    
    struct shop funcone(void)
    { int p;
    struct shop var3;
    printf("enter the price");
    scanf("%d" , &p);
    var3.price=p;
    return part;
    }
    Errors shown

    func_in_struct.c:7: error: two or more data types in declaration specifiers
    func_in_struct.c: In function ‘funcone’:
    func_in_struct.c:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘main’
    func_in_struct.c:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    func_in_struct.c:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    func_in_struct.c:27: error: expected ‘{’ at end of input

    I have tried to rectify but I find the code correct to my knowledge
    please guide me

    Thank You
    Rake

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by magnetpest2k7 View Post
    I am just learning to use functions in structures can any one please explain me the error in the following code and rectify it.

    Code:
    #include<stdio.h>
    
    struct shop{
    int price;
    }
    
    /* You can't declare a function as an instance of struct shop. 
    Any expression in C, needs to terminate with a semi-colon.
    */
    struct shop funcone(void)  
    void functwo(struct shop var1)
    
    int main()
    { struct shop var2;
       var2=funcone()
       functwo(var2);
       return 0;
    
    }
    void functwo(struct shop var1)
    { printf("price=%d\n"var1.price); //missing comma afterward
    }
    
    struct shop funcone(void)
    { int p;
      struct shop var3;
      printf("enter the price");
      scanf("%d" , &p);
      var3.price=p;
      return part; //part is not a declared variable
    }
    Errors shown

    func_in_struct.c:7: error: two or more data types in declaration specifiers
    func_in_struct.c: In function ‘funcone’:
    func_in_struct.c:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘main’
    func_in_struct.c:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    func_in_struct.c:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    func_in_struct.c:27: error: expected ‘{’ at end of input

    I have tried to rectify but I find the code correct to my knowledge
    please guide me

    Thank You
    Rake
    A few hints are in red.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    typedef

    does it ask you to enter a price and then prints price=... if yes then this is the correct code, enjoy build and run
    Code:
    #include<stdio.h>
    
    typedef struct shop
    {
        int price;
    }s;
    
    s funcone(void);
    void functwo(s);
    
    main()
    {
        s var2;
        var2=funcone();
        functwo(var2);
        return 0;
    
    }
    void functwo(s var1)
    {
        printf("price=%d\n", var1.price);
    }
    
    s funcone(void)
    {
         int p;
         s var3;
         printf("enter the price ");
         scanf("%d" , &p);
         var3.price=p;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions about structures passing through functions
    By nareik9394 in forum C Programming
    Replies: 1
    Last Post: 03-18-2009, 06:19 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  4. data Structures / Hash functions
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-11-2001, 03:46 PM
  5. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 01:54 PM