Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

Sorting strings in C

$
0
0

I was trying to sort c-strings using qsort(). Below is the comp function.

> int comp(const void *a,const void *b)
> {const char **ia=(const char **)a; 
> const char **ib=(const char **)b; 
> return strcmp(*ia,*ib); }

In main

char *b[6];
 for(int i=0;i<6;i++)
 b[i]=new char[6];
 for(int i=0;i<6;i++)
 strcpy(b[i],"");

The qsort function works in this case where the call given to qsort was

size_t strings_len=sizeof(b)/sizeof(char *);//since the array is of char*s
 qsort(b,strings_len,sizeof(char*),comp);

here

strings_len=sizeof(b)/sizeof(char*);

However, if in main() we have

char b[6][6];
for(int i=0;i<6;i++)
strcpy(b[i],"");

and then we call qsort() as given above a SIGSEGV occurs.Whats the difference in the two?Isn't b[i] a pointer to the c string?


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>