简体   繁体   中英

What am I doing wrong with this array in C?

I need to know how many times does "Maria" appears in this array, but when I run it, it says that it appears 51 times, and I think its only like 8

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

int main(int argc, char *argv[])
{
    int g;
    int i;
    const char * nombres[40] = {"Sandra Marisol","Juan Luis","Perez Luis","Carlitos","Maria","Mariana", "Carlota","Anthony",
                        "Fernando Jan","Alfonso Roche","Julieta Zacatenco","Maria de los Angeles","Laura Jessica",
                        "Andrea Maria","Jose Maria","Andres Molina","Aline Derrant","Paquito","Luisa","Ana Maria",
                        "Caleb","Luis Fernando","Mario Alberto","Paula Monica","Otoniel","Elias Primero","Maurico Enrique",
                        "Anastasia Maria","Maria Juana","Juana de Arco","Aria Montgomery""Hanna Maria","Magdalena","David Green",
                        "Florian Drake","Edward Jones","Joakin Broder","Paar","Alicia Torres","Juan Pablo"};
    for(i = 0; i>40; i++)
    {
        printf("%s\n", nombres[40]);
        if(nombres[i] == "Maria")
        g++;
    }
    if(g>0){
            printf("El nombre de Maria aparece %d veces.", g);
        }
        else {
            printf("El nombre de Maria NO aparece");
        }

    system("pause");
    return 0;

}

if(nombres[i] == "Maria")

You can't compare strings like this. You need to use strstr() to look for substrings in a string.

Also, printf("%s\\n", nombres[40]); probably should have i instead of 40 . And your comparison in the middle of your for loop above this is backwards.

And... probably more else wrong, but that is enough for me.

There are lots of error..... Let's see how much I could solve..xd

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

    int main(int argc, char *argv[])
    {
        int g=0;
        int i;
        const char * nombres[40] = {"Sandra Marisol","Juan Luis","Perez Luis","Carlitos","Maria","Mariana", "Carlota","Anthony",
                            "Fernando Jan","Alfonso Roche","Julieta Zacatenco","Maria de los Angeles","Laura Jessica",
                            "Andrea Maria","Jose Maria","Andres Molina","Aline Derrant","Paquito","Luisa","Ana Maria",
                            "Caleb","Luis Fernando","Mario Alberto","Paula Monica","Otoniel","Elias Primero","Maurico Enrique",
                            "Anastasia Maria","Maria Juana","Juana de Arco","Aria Montgomery""Hanna Maria","Magdalena","David Green",
                            "Florian Drake","Edward Jones","Joakin Broder","Paar","Alicia Torres","Juan Pablo"};
        for(i = 0; i<40; i++)
        {
            printf("%s\n", nombres[i]);
            if(!strcmp(nombres[i],"Maria"))
            g++;
        }
        if(g>0){
                printf("El nombre de Maria aparece %d veces.", g);
            }
            else {
                printf("El nombre de Maria NO aparece");
            }

        return 0;

    }

Here the counter variable g was not initialized. It should be initialized to 0 otherwize it contains some garbage value.then in for loop the condition was wrong the loop was not being executed because u write I>40 here i is initialized by 0 so condition get false and loop don't run.then printf in the loop contains nombres[40] which gives a nullpointer because you last name is hombres[39].then in if condition you can't just compare string like other variable you have to use lib function which is strcmp which means string compare. In following conditions

strcmp(s1,s2);

**

*if s1==s2 then it returns 0.
If s1>s2 then it returns 1
If s1<s2 it returns -1 

So I wrote !strcmp(); So if string matches it will return 0 and '!' Converts it to 1. Hope this works.kudos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM