samedi 27 juin 2015

Segmentation Fault in C

My code is giving me a segmentation fault and I can't seem to find what I'm doing wrong:

#include <stdio.h>
#include <string.h>
char find(char name[], char allNames[][10], int length)
{
int i=0;
for (i = 0; i < length; i++) {
if (strcmp(allNames[i],name) == 1) {
printf("%i",i);
return *name;
}
}
return -1;
}

main(){
  char allNames[][10] = {"cat","dog","frog","log","bog"};
  char name[] = "log";
int length=5;
  printf("%s",find(name,allNames,length));

}

I'm really keen to understand all the mechanisms happening here and what I'm doing wrong for tomorrows exam. Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire