0

This function stops awsering, and I can't spot the prob, can someone try to find it please? It is suposed to give me the designed name and number from an array of strings.

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

int sameName();
char **getNumber();

char **getNumber (char *n[], char e[],int N){
    int a;
    for(a=0;a<N;a++){
        if (sameName(n[a],e))
        {
            return n[a];
        }
    }
    return "Not found!";
}

int sameName(char n[], char e[]){
    int a;
    for(a=0;e[a]!='\0';a++){
        if (n[a]!=e[a])
        {
            return 0;
        }
    }
    return 1;
}

int main (){
    char numbers [5] [100] ={{"Ash 031"},{"Bomberman 021"},{"Rango 120"},{"Gigo Senhas 017"},{"Marcoreano 135"}};
    char name [100];
    char a [100];
    scanf("%s",&a);
    strcpy(name,getNumber (numbers,a,5));
    printf("%s\n",name);
    return 0;
}
9
  • Have you actually debugged it? Commented Apr 26, 2016 at 22:44
  • with the gdb debugger? It gives em a sgementation fault on sameName Commented Apr 26, 2016 at 22:46
  • The prototype int sameName(); tells the compiler nothing about the function arguments, so it is useless as a forward reference. Commented Apr 26, 2016 at 22:48
  • should I just put it in a comment or erase it? Commented Apr 26, 2016 at 22:51
  • No, it needs the correct prototype: that is what it is for. sameName is called by getNumber, which precedes sameName implementation. So the dead prototype does not tell the compiler what the arguments should be. Commented Apr 26, 2016 at 22:55

1 Answer 1

1

You need to spicify the size of your two-dimension array when you pass it to your getNumber function. See http://c-faq.com/aryptr/pass2dary.html for details.

And for scanf you only need to pass a since it is a string.

Taking these plus fixing your prototypes as mentioned in the comment above, that gives :

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

int sameName(char n[], char e[]);
char *getNumber (char n[][100], char e[],int N);

char *getNumber (char n[][100], char e[],int N){
    int a;
    for(a=0;a<N;a++){
        if (sameName(n[a],e))
        {
            return n[a];
        }
    }
    return "Not found!";
}

int sameName(char n[], char e[]){
    int a;
    for(a=0;e[a]!='\0';a++){
        if (n[a]!=e[a])
        {
            return 0;
        }
    }
    return 1;
}

 int main (){
    char numbers [5] [100] ={{"Ash 031"},{"Bomberman 021"},{"Rango 120"},{"Gigo Senhas 017"},{"Marcoreano 135"}};
    char name [100];
    char a [100];
    scanf("%s",a);

    strcpy(name,getNumber (numbers,a,5));
    printf("%s\n",name);
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also, might I suggest returning (and checking for) NULL rather than "Not found!", which is a perfectly normal string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.