0

Hi, I can't seem to get my program to work. I'm using qsort to sort some keyboard-input strings, but I get segmentation fault. I tried 3 versons, and none of them works. Could someone pinpoint the error?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<search.h>
int str_cmp(const void *prv,const void *vtor);
main()
{
      int m,j;
      char strings[10][10];
      scanf("%d",&m);
      for(j=0;j<m;j++)
       scanf("%s",&strings[j]);
      qsort(strings,m,sizeof(char *),str_cmp);

      return 0;
}
int str_cmp(const void *prv,const void *vtor)
{
    return (strcmp(*(const char **)prv, *(const char **)vtor));
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<search.h>
int str_cmp(const void *prv,const void *vtor);
main()
{
      int m,j;
      char *strings[10];
      scanf("%d",&m);
      for(j=0;j<m;j++)
       scanf("%s",&strings[j]);
      qsort(strings,m,sizeof(char *),str_cmp);

      return 0;
}
int str_cmp(const void *prv,const void *vtor)
{
    return (strcmp(*(const char **)prv, *(const char **)vtor));
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<search.h>
int str_cmp(const void *prv,const void *vtor);
main()
{
      int m,j;
      char strings[10][10];
      char **pok;
      *pok=*strings;
      scanf("%d",&m);
      for(j=0;j<m;j++)
       scanf("%s",&strings[j]);
      qsort((void *)pok,m,sizeof(char *),str_cmp);

      return 0;
}
int str_cmp(const void *prv,const void *vtor)
{
    return (strcmp(*(const char **)prv, *(const char **)vtor));
}
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by guardians
0

I think a segmentation fault occurs when you are trying to use a memory location that it is not allowed to. For example, trying to write to a read only memory location or maybe even trying to use a particular memory location that has been allocated to another memory location. You have to be careful with memory allocation in C. I've just recently asked a question int he same kind of subject area. Maybe, read this.. http://en.wikipedia.org/wiki/Segmentation_fault

I can't really pinpoint where this is happening at the moment, but I'm sure a more experienced member of the forum will be able to help soon.

Hope it helps.
Jonathon.

0

You using qsort to sort "strings of 10 chars", aren't you? So tou ought to pass to qsort proper size -- i.e. 10*sizeof(char). Also you wrote str_cmp rather unusual, I corrected it to be more simple. Try this one, it works:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int str_cmp(const void *prv,const void *vtor);
int main(void) {
  int m,j;
  char strings[10][10];
  scanf("%d", &m);
  
  // TODO: here may be assert that m < 10!
  
  for(j=0; j<m; j++)
    scanf("%s",&strings[j]);
  
  qsort(strings, m, 10 * sizeof(char), &str_cmp);

  printf("\nResults\n:");
  for(j=0; j<m; j++)
    printf("%s\n", strings[j]);    
  
  return 0;
}

int str_cmp(const void *prv, const void *vtor)
{
    return strcmp((const char*)prv, (const char*)vtor);
}
0

Also you wrote str_cmp rather unusual, I corrected it to be more simple.

Apparently, not only simpler - but also correct :) I still am not well versed with the pointers and strings analogy in C, maybe someone could explain why my code didn't work? I actually couldn't get the comparison part to work, so that is the same code I found on the internet as an example for a comparison function...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.