Thread: Help in passing a strcture in function

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Help in passing a strcture in function

    Hello All,
    I want to pass a structure through a function. I am new in C programming. Help me.

    I am getting an error as bad function type.

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    it would be better if you posted your program but here is an overview of how to pass a structure as an argument :

    #include<stdio.h>


    Code:
    struct node
    {
           int item; 
    };
    
    void func1(struct node a);
    
    
    int main()
    {
        struct node b;
        b.item = 1;
        func1(b);
        return 0;
    }
    
    void func1(struct node b)
    {
         printf("%d", b.item);
    }
    just make sure that a copy of the structure is passed in this case, if you want to change values in a structure inside a function you need to pass a pointer to that structure.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    my car makes a clink-clack sound when i'm driving it. how do i fix it?


    EDIT: oh, sorry. i thought the thread title said "Crappy Questions"


    EDIT 2: how did ya manage to 'sneak one in' on me gemini?...2 minutes apart... how wierd
    Last edited by misplaced; 05-04-2005 at 03:30 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    please post your code so that we can find where u are going rough. any way here is sample code to how pass struct to the function

    Code:
    #include<stdio.h>
    /* skeleton of struct student or the abstract datatype*/
    struct student
    {
        int studno;
        char name[20];
    };
    /* fucniotn to print details of each student where it takes struct student as a parameter*/
    void printdetails(struct student tempst)
    {
        printf("Student Id %d\n", tempst.studno);
        printf("name %s",tempst.name);
    }
    /*fucniotn to clear the input buffer*/
    void clr_buf()
    {
        char ch;
        while((ch=getchar())!='\n' && ch!=EOF);
    }
    
     
    int main()
    {
        struct student s1;   //    instance of type struct student
        
        printf("Enter Stduent id\n");
        scanf("%d",&s1.studno);   
        
        clr_buf();    //  clear the input buffer
        printf("Enter name\n");
        fgets(s1.name,20,stdin);
        
        printdetails(s1);  // call printdetails by sending the instance 
        
        getchar();
    }
    s.s.harish

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    hahahahahhahah I wonder myself

    a better reply than mine though

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    3

    Hello

    Hello ,
    I will type the code

    #include<stdio.h>

    Code:
     typedef struct
     {
       int id;
       char info[100];
       int salary;
     } linklist;
    
      void addComission(linklist*,int);
    
     main()
     {
        int ids,salaries;
        char tempname[100];
    
        linklist l1;
    
        printf("Enter the id of the person ");
        scanf("%d",&ids);
        printf("Enter the name of the person ");    
        scanf("%s",tempname);    
        printf("Enter the salary of the person");
        scanf("%d",&salaries);
       
        
        l1.id=ids;
        strcpy(l1.info, tempname);    
        l1.salary=salaries;           
    
        if(l1.salary > 3000)
        {
           addComission(&l1,200);
           printf("Id is %d\n" , l1.id);
           printf("Name is %s\n",l1.info);
           printf("Salary is %d\n",l1.salary);     
        }
        else{
           printf("Id is %d\n" , l1.id);
           printf("Name is %s\n",l1.info);
           printf("Salary is %d\n",l1.salary);     
        }
        getchar();
     }
     
     void addComission(linklist *l1, int bonus)
     {
       int salarys;
       salarys = l1->salary;
       salarys = salarys + bonus;
       
     }.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    3
    Hey with your code I got the following error.

    c:\program files\miracle c\structure.c: line 92: illegal dereference
    'printdetails(s1)'
    aborting compile

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    4

    where is the problem

    btw what is the problem you have in your code.
    It worked fine when i compile and i dont get any compilation errors

    In your code you missed including string.h file and in addCommision() you omitted l1->salary = salarys

    rest and all is ok...

    regards
    Ravi

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    program works fine just change this

    add string.h file and your addCommision() should look like this if I am correct :

    Code:
    void addComission(linklist *l1, int bonus)
     {
       int salarys;
       salarys = l1->salary;
       l1->salary = salarys + bonus;
       
     }
    value assignment in C is from right to left not from left to right

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    use fgets() to read string rather than scanf()

    find out the reason fgets in FAQ


    s.s.harish

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > c:\program files\miracle c\structure.c: line 92: illegal dereference
    Oh no, not another one
    Miracle-C is a piece of ........ - just get rid of it now!

    That compiler needs taking off the net so badly, it's such a piece of crap it's not true (and not funny either).

    Do yourself a HUGE favour and get a real compiler (not some toy), like for example DEV-C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Passing a function to a function
    By lend0g in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2003, 10:16 PM