I'm tryin to search an array of structures for name. I'm comparing a char[] to a struct member with is a char[]

my struct

struct NBA_Team
{
    char* name;
    char conference[8];
    char division[10];
    unsigned short wins = 0;
    unsigned short losses = 0;
    float pct;
};

My search code

void searchAndTally(NBA_Team teams, char team[], bool WL)
{

    int low = 0;
    int high = numTeams;
    int med = (high + low)/2;
    bool found = 0;

    if(strcmp(teams[0].name, team) == 0)
    {
        if(WL == 1)
        {
            teams[0].wins += 1;
        }
        else if(WL == 0)
        {
            teams[0].losses += 1;
        }
    }
    if(strcmp(teams[numTeams - 1].name, team) == 0)
    {
        if(WL == 1)
        {
            teams[numTeams - 1].wins += 1;
            return;
        }
        else if(WL == 0)
        {
            teams[numTeams - 1].losses += 1;
            return;
        }
    }

    while(!found && low <= high)
    {
        med = (high + low)/2;


        if(strcmp(teams[med].name, team) == 0)
        {
            found = true;
            if(WL == 1)
            {
                teams[med].wins += 1;
                return;
            }
            else if(WL == 0)
            {
                teams[med].losses += 1;
                return;
            }

        }
        if(strcmp(teams[med].name, team) > 0)
        {
            high = med - 1;
        }
        else
        {
            low = med + 1;
        }
    }
}

The problem is with this line:

 if(strcmp(teams[0].name, team) == 0)

It balks evertime is sees it. I think it may be because there's a pointer inthe struct declaration, but I've made similar comparisons and they worked, this is really stumping me.

Recommended Answers

All 3 Replies

I think it may be because there's a pointer inthe struct declaration,

Nope, it has nothing to do with that pointer, unless there is no memory allocated to the pointer.

The problem looks like Teams is not an array but a single instance of the structure. If you want Teams to be an array then

void searchAndTally(NBA_Team teams[], char team[], bool WL)

or

void searchAndTally(NBA_Team* teams, char team[], bool WL)

Never mind - God I why do I always figures this crap out right after I post?!

So stupid. Forgot the brackets in the function header.

"Never mind - God I why do I always figures this crap out right after I post?"

Probably because we all figure out our errors right after we ask someone about them - having a bit of time to rethink what we did... Welcome to the Homer Simpson Society "Doh!". :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.