Tuesday, July 14, 2009

Help in C pointers?

#include"stdio.h"


#include"conio.h"


void main()


{


void bubblesort(char *R[], int a); //function declaration//


char *restname[20][30]; //Variable in main//


bubblesort(restname[30], 20)//Function bubblesort in main//





void bubblesort(char *R[], int a)//function bubble sort//


{


int i, j;


char *hold;


for(i= 0; i %26lt; a; i++)


{


for(j=0; j%26lt;a-1; j++)


{


if(R[j] %26lt; R[j+1])


{


hold = R[j];


R[j] = R[j+1];


R[j+1] = hold;}


}


}


for(i=0; i%26lt;a; i++)


{


printf("%s", R[i]);


printf("\n");


}


} //end function//





}


}





help with the declaration part

Help in C pointers?
I see several potential problems...





(1) The function declaration for bubblesort goes above the main, not in it.





(2) The function itself appears within the main function. C doesn't allow nested functions.





(3) That function has as its first parameter an array of pointers to char. Fine, we sort arrays of pointers all the time. But then the sort algorithm sorts by the value of the pointer itself, not the value of the char indicated by that pointer. Sorting the pointers in this manner makes little sense.
Reply:first any function declartion and definition should be out of main scope


not inside the main,the only thing inside the main is the fucntion call :)





secondly your array restname is empty ? why ? what are you sorting ?





good luck :)





#include"stdio.h"


#include"conio.h"








void bubblesort(char *R[], int a)//function bubble sort//


{


int i, j;


char *hold;


hold = new char;





for(i= 0; i %26lt; a; i++)


{


for(j=0; j%26lt;a-1; j++)


{


if(R[j] %26lt; R[j+1])


{


hold = R[j];


R[j] = R[j+1];


R[j+1] = hold;}


}


}


for(i=0; i%26lt;a; i++)


{


printf("%s", R[i]);


printf("\n");


}


} //end function//


void main()


{





char *restname[20][30]; //Variable in main//








bubblesort(restname[30], 20);//Function bubblesort in main//














}


No comments:

Post a Comment