trouble with structure pointers

Member
Posts: 185
Joined: 2005.02
Post: #1
Hi. Just another one of those threads where I ask, "why won't this work?" and one of you shows me how stupid I am. I have been trying figure it out myself but have gotten nowhere after 2 weeks worth of free time. So, today's question pertains to structure pointers.

Ok,
Code:
struct seat {
        int seatNum;
        int Taken;
        char last[NMAX];
        char first[NMAX];
};
int main (void)
{
    
    struct seat seats[SMAX];
    char choice;
    for (int count = 0; count < SMAX; count++)
    {
        seats[count].seatNum = count + 1;
        seats[count].Taken = 0;
    }
    showMenu();
    while ((choice = getChoice()) != 'q')
    {
        switch (choice)
        {
            case 'a' : showEmpty(seats); break;
            case 'b' : listEmpty(seats); break;
            case 'c' : Assign(&seats, seats); trashRest(); break;
            case 'd' : showInfo(seats); break;
        }
        showMenu();
    }        
    return 0;
}            

void Assign(struct seat (* Pointer)[SMAX], struct seat seats[SMAX])
{
    int seatToAssign;
    char lastTemp[NMAX];
    char firstTemp[NMAX];
    listEmpty(seats);
    puts("Which seat would you like to assign?(enter 0 to abort)");
    seatToAssign = getInteger();
    if (seatToAssign == 0)
        return;
    while(checkIfEmpty(seatToAssign, seats) == 0)
    {
        puts("I'm sorry, that seat is already taken.");
        seatToAssign = getInteger();
        if (seatToAssign == 0)
            return;
    }
    puts("Please enter the last name:");
    fgets(lastTemp, NMAX, stdin);
    puts("And the first name:");
    fgets(firstTemp, NMAX, stdin);
    printf("assign %s %s to seat %d?(y or n)", firstTemp, lastTemp, seatToAssign);
    if (getchar() == 'y')
    {
        for (int count = 0; count < SMAX; count++)
        {
            if (seats[count].seatNum == seatToAssign)
            {
                Pointer[count]->Taken = 1;
                for (int index = 0; index < NMAX && lastTemp[index] != '\n'; index++)
                {
                    Pointer[count]->last[index] = lastTemp[index];
                }    
                for (int index = 0; index < NMAX && firstTemp[index] != '\n'; index++)
                    Pointer[count]->first[index] = firstTemp[index];
                return;
            }    
        }
    }
}

main() and Assign() don't appear to be comunicating properly. The only time It works correctly (modifies structure through pointers) is when I try to assign someone to seat #1. Why?

I am going to feel so stupid when this is answered.....
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #2
First, some nitpicking Wink I would rewrite your struct as:
Code:
struct Seat {
    int number;
    int taken;
    char lastName[MAX_NAME_LENGTH];
    char firstName[MAX_NAME_LENGTH];
};
typedef struct Seat Seat;

Namely, structs/clasess start with uppercase, variables start with lowercase, do not repeat the class name in the name of a member variable and use descriptive names, even if they are a bit longer. That makes it easy for other people to follow your code.

Second, why does Assign() take two parameters? In any case, remember that an array is already a pointer, so you don't need to do:
Code:
Assign(&seats, ...)
...
Assign(struct seat (* Pointer)[SMAX],...)
...
Pointer[count]->...

Just do a plain:
Code:
Assign(seats, ...)
...
Assign(struct seat Pointer[SMAX],...)
...
Pointer[count]....
You'll be modifying the original. (Of course, "Pointer" is not the best possible name for a variable Wink )
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #3
PowerMacX Wrote:Second, why does Assign() take two parameters?

because checkIfEmpy() takes a struct seat as an arguement not a pointer, and I was too lazy to go back and rewrite it. Smile

EDIT nevermind I get what you are saying. Thanks for the feedback!

EDIT#2 Yes! Ichanged it to take one arguement like you said and it works. Finally! Thank you very much!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  c pointers and memory allocation NelsonMandella 15 7,401 Feb 27, 2010 12:59 AM
Last Post: Kerome
  C++ pointers (multiple uses inside a class) problems :( wyrmmage 14 5,897 Jun 2, 2007 10:08 AM
Last Post: akb825
  Problems With Arrays of Pointers Nick 8 3,873 Sep 27, 2005 04:53 PM
Last Post: Zekaric
  pointers to structs Atomical 13 4,871 Jul 17, 2005 10:55 AM
Last Post: Steven
  dereferencing void pointers Duane 15 7,548 May 4, 2005 05:15 PM
Last Post: Puzzler183