#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Problem with my basic program (qsort again)


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct testGrade {
    	char *student;
    	int grade;
    } testGrade
    
    int compare(const void *a,const void *b)
    {
    	testGrade *x = (testGrade *)a;
    	testGrade *y = (testGrade *)b;
    	return x->grade - y->grade;
    }
    
    int main()
    {
    	testGrade Avery = {"Avery",86};
    	testGrade Michael = {"Michael",74};
    	testGrade Barack = {"Barack",93};
    	testGrade Shelby = {"Shelby",97};
    	testGrade data[] = {Avery,Michael,Barack,Shelby};
    	qsort(data,4,sizeof(testGrade),compare);
    	printf("%d",data->grade);
    	getchar();
    	return 0;
    }
    I want it to just sort the four people according to what they got on their tests from smallest to largest.

    Thanks again. Don't worry, this won't take me as long as linked lists ;)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > printf("%d",data->grade);
    Perhaps you should print the whole array.
    Because all you're doing at the moment is printing data[0].grade.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Still doesn't work.

    Error(s):
    Code:
    1>question1.c(9): error C2054: expected '(' to follow 'testGrade'
    1>question1.c(10): error C2085: 'compare' : not in formal parameter list
    1>question1.c(10): error C2143: syntax error : missing ';' before '{'
    1>question1.c(22): error C2440: 'initializing' : cannot convert from 'testGrade' to 'char *'
    1>question1.c(22): error C2440: 'initializing' : cannot convert from 'testGrade' to 'int'
    1>question1.c(22): error C2440: 'initializing' : cannot convert from 'testGrade' to 'char *'
    1>question1.c(22): error C2440: 'initializing' : cannot convert from 'testGrade' to 'int'
    1>question1.c(23): error C2065: 'compare' : undeclared identifier
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Telling me the problem to this would be great as well.

    Code:
    #include<stdio.h>
    
    typedef struct x{
    	int value;
    } x;
    
    int main()
    {
    	x la = {55};
    	x ex = {56};
    	x data[2] = {la,ex};
    	printf("%d",data[0].value);
    	getchar();
    	return 0;
    }
    Error(s):
    Code:
    1>question1.c(11): error C2440: 'initializing' : cannot convert from 'x' to 'int'
    1>question1.c(11): error C2440: 'initializing' : cannot convert from 'x' to 'int'
    Helping me debug this program will probably help me debug the other program. It's not compiling for some reason. Wonder why... and sorry for the bad variables.
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    I tried your code with gcc, and didn't get the error.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    It could be as simple as a missing semicolon.

    Code:
    typedef struct testGrade {
    	char *student;
    	int grade;
    } testGrade

IMN logo majestic logo threadwatch logo seochat tools logo