passing struct to function help

This is a discussion on passing struct to function help within the C Programming forums, part of the General Programming Boards category; Im trying to pass elements of a structure to a function (call by reference) This code suppose to work like ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    22

    passing struct to function help

    Im trying to pass elements of a structure to a function (call by reference)

    This code suppose to work like this:
    Enter students name and Points
    then calculate points to grade in a function.



    Code:
    #include<stdio.h>
    
    
    void calculate (struct student *, struct student *);
    
    int main (void)
    {
    	
    	struct student
    	{
    		char name[30];
    		float points;
    		int grade;
    	}stud[3];
    
    	
    	int i;
    	for (i=1; i<=3; i++)
    	{
    		printf("%d.name->", i);  scanf( "%s", &stud[i].name );
    		printf("points->");  scanf( "%f", &stud[i].points);
    		calculate ( &stud[i], &stud[i] );
    	}
    	return 0;
    }
    
    void calculate ( struct student *nname, struct student *pnt )
    {
        int grade;
        grade = ( (*pnt).points < 50 ? 5  :  4-(  (int)(*pnt).points - 50 / 15 )  );
    	printf("\n%sīs grade is  %d", (*nname).name, grade);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > for (i=1; i<=3; i++)
    An array of 3 elements is indexed using for (i=0; i<3; i++)

    > grade = ( (*pnt).points < 50 ? 5 : 4-( (int)(*pnt).points - 50 / 15 ) );
    1. Use pnt->points in place of (*pnt).
    It's less error prone.
    2. Use if ( ) else to make your meaning clearer than the ?: operator

    Also you only need to pass the parameter once to access all members of a structure
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    okay, I made some changes to make it more readable
    my compiler gives me several errors and warnings
    like unknown field etc
    Code:
    #include<stdio.h>
    
    
    void calcu (struct student* );
    
    int main (void)
    {
    
    	struct student
    	{
    		char name[30];
    		float points;
    		int grade;
    	}stud[3];
    
    
    	int i;
    	for (i=0; i<3; i++)
    	{
    		printf("%d.name->", i);  scanf( "%s", &stud[i].name );
    		printf("points->");  scanf( "%f", &stud[i].points);
    		calcu ( &stud[i] );
    	}
    	return 0;
    }
    
    void calcu ( struct student *ptrstudent )        /* result: 5->"F"  4->"D"  3->"C"  2->"B" 1->"A" */
    {
        int result;
    	if (ptrstudent->points < 50)
    	{
    		result=5;
    		printf("\n%s failed with a %d in his C test", ptrstudent->name, result);
        }
    	else
    	{
    		result= ( (int)ptrstudent->points - 50 / 15 );
        	printf("\n%s got a %d in his C test", ptrstudent->name, result);
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by staticalloc
    okay, I made some changes to make it more readable
    my compiler gives me several errors and warnings
    like unknown field etc
    Your struct student definition is made within the main function. As such, its scope is limited to the main function. Move the definition of that struct outside of main.

    Code:
    #include<stdio.h>
    
    struct student
    {
        char name[30];
        float points;
        int grade;
    };
    
    void calcu (struct student* );
    
    int main (void)
    {
    
        struct student stud[3];
    
        int i;
        for (i=0; i<3; i++)
        {
            printf("%d.name->", i);  scanf( "%s", &stud[i].name );
            printf("points->");  scanf( "%f", &stud[i].points);
            calcu ( &stud[i] );
        }
        return 0;
    }
    
    void calcu ( struct student *ptrstudent )        /* result: 5->"F"  4->"D"  3->"C"  2->"B" 1->"A" */
    {
        int result;
        if (ptrstudent->points < 50)
        {
            result=5;
            printf("\n%s failed with a %d in his C test", ptrstudent->name, result);
        }
        else
        {
            result= ( (int)ptrstudent->points - 50 / 15 );
            printf("\n%s got a %d in his C test", ptrstudent->name, result);
        }
    }
    I did not check for other problems, except for the missing } marked above (typo?)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    22
    THANKS! I changed the code as you said and it works.


    Code:
    #include<stdio.h>
    
    struct student
    	{
    		char name[30];
    		float points;
    		int grade;
    	};
    
    void calcu (struct student* );
    
    int main (void)
    {
    
    	struct student stud[3];
    
    
    	int i;
    	for (i=0; i<3; i++)
    	{
    		printf("\n%d.name->", i);  scanf( "%s", &stud[i].name );
    		printf("points->");  scanf( "%f", &stud[i].points);
    		calcu ( &stud[i] );
    	}
    	return 0;
    }
    
    void calcu ( struct student *ptrstudent )
                                                 /* result: 5->"F"  4->"D"  3->"C"  2->"B" 1->"A" */
    {
        int result;
    	if (ptrstudent->points < 50)
    	{
    		result=5;
    		printf("\n%s failed with a %d in his C test",ptrstudent->name, result);
        }
    	else
    	{
    		result= (4- ( (int)ptrstudent->points - 50 )/ 15 );
        	printf("\n%s got a %d in his C test", ptrstudent->name, result);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Acessing a struct after passing to a function
    By bomberto in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 04:29 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21