DIGIT-ARC read-only archive · snapshot: July 23, 2026

sorting problem??? help please...

4 posts · originally from https://geek.digit.in/community/threads/sorting-problem-help-please.87622/

J
jdesai18 Original post #1
May 12, 2008 at 5:08 PM
hey guys,

i am doin this program for sorting two-dimnsional aray in ascendin order, and it should accpt N inputs from user... whr N is 7

this is what i have done so far....

void sortLastNames(char name[][N], int N)
{
int count, count1;
char tempname[20];
for(count=0; count<N-1;count++)
{
for(count1=count+1;count1<N;count1++)
{
if(strcmp(name[count], name[count1])>0)
{
strcpy(tempname, name[count]);
strcpy(name[count], name[count1]);
strcpy(name[count1], tempname);
}
}
}
}

now problem is it runs smoothly if u enter input with upto 6 characters, if u enter more than that, it crashes....

please guys reply me back ASAP...
QwertyManiac
May 12, 2008 at 11:43 PM
...;count<N-1;...

If N==7, shouldn't you simply count upto N viz count<N such that it goes from 0 ... 6?
nightcrawler
May 13, 2008 at 11:10 AM
hey guys,
void sortLastNames(char name[][N], int N)

This is the reason for it. The above statement means that sortLastNames takes an array of Strings (or 2d array of char). The size of each string is N that is 7. Hence whenever a string greater than 7 is encountered strcmp and strcpy will cause problems.(They have overflow issues as they look for NULL terminating strings '\0').

Fixing this will solve ur problem.
M
May 13, 2008 at 2:25 PM
void sortLastNames(char name[][N], int N)
Gives me compile time error with gcc.