Ash
Ash

Reputation: 98

function of arrays of strings in c

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;
}

Upvotes: 0

Views: 45

Answers (1)

Ouss4
Ouss4

Reputation: 479

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;
}

Upvotes: 1

Related Questions