Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
Where am I going out of bounds?

My code takes in a series of words as command line arguments and should sort them using qsort. Right now I print out the original Args however when it goes to print the Sorted args I receive a segmentation error. New to C, all advice is appreciated.

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


int stringCmp(const void *str1, const void *str2); //Function prototype.

int main (int argc, char *argv[])
{
  int i;
  char **arr = malloc(argc * sizeof(char *));
  printf("Original Args:\n");
  for (i = 0; i < argc-1; i++){
    arr[i] = argv[i+1];
    printf("%s\n",arr[i]);
  }

  qsort(arr, argc, sizeof *arr, stringCmp);

  printf("\nSorted Args:\n");
  for (i = 0; i < argc; i++){
    printf("%s\n", arr[i]);
  }
  free (arr);
  return 0;
}

int stringCmp(const void *str1, const void *str2)
{
  strcmp(str1, str2);
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your comparison function isn't returning anything. Also, this function takes the address of the array elements, so what you're actually getting are char * const *, not void *

You want:

int stringCmp(const void *str1, const void *str2)
{
  const char * const *s1 = str1;
  const char * const *s2 = str2;
  return strcmp(*s1, *s2);
}

You're also not passing in the correct number of elements to qsort. It should be argc-1, not argc. The same going for printing the list:

  qsort(arr, argc-1, sizeof *arr, stringCmp);

  printf("\nSorted Args:\n");
  for (i = 0; i < argc-1; i++){
    printf("%s\n", arr[i]);
  }
over 4 years ago · Santiago Trujillo Relatório

0

For starters you are passing invalid number of elements

qsort(arr, argc, sizeof *arr, stringCmp);

You need to write

qsort(arr, argc - 1, sizeof *arr, stringCmp);

Secondly the function stringCmp returns nothing and uses incorrect arguments in the call of strcmp. It should look like

int stringCmp(const void *str1, const void *str2)
{
  return strcmp( *( const char ** )str1, *( const char ** )str2);
}
over 4 years ago · Santiago Trujillo Relatório

0

out by one here too

for (i = 0; i < argc; i++) {
    printf("%s\n", arr[i]);
}

should be

for (i = 0; i < argc - 1; i++) {
    printf("%s\n", arr[i]);
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda