Thread: Structures by reference

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Structures by reference

    As usual I'm trapped at something and making slow progress, so here I am again. My problem is that I'm trying to sort and print structures by reference, I think the sorting part is done correctly but the printing is when I see the crash happening.
    Structures by reference-referstruct-jpg

    Code:
    void printing(struct student *vStudnt, int *sort, char *order){
    	int pass;
    	int x;
    	
    	switch(*sort)
    	{
    		case 1:
    			if(*order = 'a' || 'A')
    			{
    				for(pass = 0; pass < 500; pass++)
    				{
    					for(x = 0; x < 500; x++)
    					{
    						if(sorting->lastName[x] > sorting->lastName[x+1])
    						{
    							temp = sorting[x];
    							sorting[x] = sorting[x+1];
    							sorting[x+1] = temp;
    						}
    					}
    				}
    			}
    			else
    			{
    				for(pass = 0; pass < 500; pass++)
    				{
    					for(x = 0; x < 500; x++)
    					{
    						if(sorting->lastName[x] < sorting->lastName[x+1])
    						{
    							temp = sorting[x];
    							sorting[x] = sorting[x+1];
    							sorting[x+1] = temp;
    						}
    					}
    				}
    			}
    			printf("\n%s%s%0d%s%s\n", "Last Name", "First Name", "Age", "Gender", "Class");
    			for(x = 0; x < 500; x++)
    			{
    				printf("%d\n", x);
    				*vStudnt = sorting[x];
    				printf("%s%s%d%s%s\n", vStudnt->lastName, vStudnt->firstName, vStudnt->age, vStudnt->gender, vStudnt->Class);
    			}
    			break;
    PS: I'm very very bad with pointer.

    so can any point out my error?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You do not seem to have defined the variable named sorting. Furthermore, the only use for vStudnt is to copy sorting[x], which means that vStudnt was completely unnecessary to begin with since you could have written sorting[x].lastName instead of vStudnt->lastName.

    By the way *order = 'a' || 'A' is probably a mistake. You might have wanted: *order == 'a' || *order == 'A'
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't see where "temp" or "sorting[]" are declared.

    Code:
    for(pass = 0; pass < 500; pass++)
    {
        for(x = 0; x < 500; x++)
        {
            if(sorting->lastName[x] > sorting->lastName[x+1])
            {
                temp = sorting[x];
                sorting[x] = sorting[x+1];
                sorting[x+1] = temp;
            }
        }
    }
    I would suggest not using "magic numbers".

    Assuming the "sorting" array is designed to hold that number (500) of elements, you're overrunning the bounds of the array during the last iteration with [x+1] (lines 8 and 9 above).

    Bubble Sort and Modified Bubble Sort - Cprogramming.com
    Last edited by Matticus; 01-15-2014 at 12:17 PM.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    Both temp and sorting were declared at the top of my program or was I suppose to do it again in the function?
    Code:
    struct student{
    	char fLoc;
    	int stdNum;
    	char firstName[15];
    	char lastName[10];
    	int age;
    	char gender;
    	char address[70];
    	char Class[10];
    	// struct *mInfo;
    }sorting[500], temp;
    Ohh and LaserLight, IDK why but when I'm comparing chars in IF statement the == doesn't make it compile.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BIGDENIRO
    Both temp and sorting were declared at the top of my program or was I suppose to do it again in the function?
    temp, being presumably "temporary", should be declared in the function where it is used. sorting should probably be declared in the main function (and you probably should give it a better name like students), and then you pass a pointer to its first element around to functions that need to access it.

    Quote Originally Posted by BIGDENIRO
    Ohh and LaserLight, IDK why but when I'm comparing chars in IF statement the == doesn't make it compile.
    What did you write and what is the compile error?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Both temp and sorting were declared at the top of my program or was I suppose to do it again in the function?
    Those are called "global variables", and they too should be avoided. variables should ideally be declared within the scope they are needed (in this case, your function).

    Code:
    char lastName[10];
    But your loop iterates from "lastname[0]" to "lastname[499]". That is a problem.

    Ohh and LaserLight, IDK why but when I'm comparing chars in IF statement the == doesn't make it compile.
    That doesn't make sense, you must have been doing something else wrong. Can you create a simple program that illustrates the problem you were seeing?

    EDIT: laserlight keeps beating me by a minute

  7. #7
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    my head is running water, all other programs have == and compiled............ but thanks for pointing it out and for the tips

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and reference
    By BIGDENIRO in forum C Programming
    Replies: 3
    Last Post: 12-02-2013, 02:29 PM
  2. Passing structures by reference and then by value
    By Chinchila in forum C Programming
    Replies: 3
    Last Post: 12-01-2012, 09:11 AM
  3. C++ Data Structures Reference
    By neolyn in forum C++ Programming
    Replies: 6
    Last Post: 12-04-2004, 12:46 PM
  4. 2 structures to reference each other
    By Kinasz in forum C Programming
    Replies: 3
    Last Post: 04-07-2004, 06:21 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM