-2

Hi following is my code i need help in the check_boundary function as how can i retrieve my initialized 2d (seating) array so that i can compare it with the new row and column number, currently i am getting 0 0 row and column in that function if i print that. the seating array is instantiated in the auditorium_seating_init function.

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

struct guest {
    char last_name[30];
    char first_name[30];
};
struct auditorium_seating {
    struct guest **seating;
};
void guest_init_default(struct guest *g)
{

    strcpy_s(g->first_name,sizeof(g->first_name), "???");
    strcpy_s(g->last_name, sizeof(g->last_name), "???");
}
void guest_init(struct guest *g, char *info)
{
    char *token,*token1;
        token1 = strtok_s(info, "/",&token);
        strcpy_s(g->first_name,sizeof(g->first_name), token1);
        token=strtok_s(NULL, "/", &token);
        strcpy_s(g->last_name, sizeof(g->last_name), token);

}
void guest_to_string(struct guest *g)
{
    printf("%c", g->first_name[0]);
    printf(".%c", g->last_name[0]);
}
void auditorium_seating_init(int rowNum, int columnNum, struct auditorium_seating *a)
{
    a->seating = malloc(rowNum * sizeof(a->seating));
    for (int i = 0; i < rowNum; i++) {
        a->seating[i] = malloc(columnNum * sizeof(**a->seating));
        for (int j = 0; j < columnNum; j++) {

            guest_init_default(&a->seating[i][j]);
        }
    }

}
int assign_guest_at(int row, int col, struct auditorium_seating *a, struct guest* g)
{

    if ((strcmp(a->seating[row][col].first_name , "???") == 1 ) && (strcmp(a->seating[row][col].last_name, "???") == 1))
    {
        a->seating[row][col] = *g;


        return 1;
    }
    else
    {
        return 0;
    }
}

This function

int check_boundaries(int row, int col, struct auditorium_seating *a)
{
    int total = sizeof(a->seating);
    //printf("total %d", sizeof(a->seating));
    //printf("col %d", sizeof(a->seating) / sizeof(a->seating[0]));
    //printf("row %d", sizeof(a->seating[0]) / sizeof(a->seating[0][0]));
    if (row >= 0 && col >= 0 && (col <= sizeof(a->seating[0])) && row <= (total / sizeof(a->seating[0]))) {
        //printf("col %p",sizeof(a->seating[0]));
        //printf("row %p", total/sizeof(a->seating[0]));
        return 1;
    }
    else
    {
        return 0;
    }
}

This is my main

void main() {
    struct auditorium_seating auditorium_seating;
    struct guest temp_guest;
    int row, col, rowNum, columnNum;
    char guest_info[30];
    // Ask a user to enter a number of rows for an auditorium seating
    printf("Please enter a number of rows for an auditorium seating.");
    scanf_s("%d", &rowNum);
    // Ask a user to enter a number of columns for an auditorium seating
    printf("Please enter a number of columns for an auditorium seating.");
    scanf_s("%d", &columnNum);
    /*** reading a guest's information ***/
    // auditorium_seating

    //scanf_s("%c", guest_info);
    auditorium_seating_init(rowNum, columnNum, &auditorium_seating);
    printf("Please enter a guest information or enter \"Q\" to quit.");
    /*** reading a guest's information ***/
    scanf_s("%s", guest_info, sizeof(guest_info));
    //scanf("%s", guest_info);
    /* we will read line by line **/

    while ( strcmp(guest_info,"Q")==1) {

        printf("\nA guest information is read.");
        // printing information.
        printf("%s", guest_info);
        // guest
        guest_init(&temp_guest, &guest_info);
        // Ask a user to decide where to seat a guest by asking
        // for row and column of a seat
        printf("Please enter a row number where the guest wants to sit.");
        scanf_s("%d", &row);
        printf("Please enter a column number where the guest wants to sit.");
        scanf_s("%d", &col);
        // Checking if the row number and column number are valid
        // (exist in the theatre that we created.)
        if (check_boundaries(row, col, &auditorium_seating) == 0) {
            printf("\nrow or column number is not valid.");
            printf("A guest %s %s is not assigned a seat.", temp_guest.first_name, temp_guest.last_name);
        }
        else {
            // Assigning a seat for a guest
            if (assign_guest_at(row, col, &auditorium_seating, &temp_guest) == 1) {
                printf("\nThe seat at row %d and column %d is assigned to the guest", row, col);
                guest_to_string(&temp_guest);
                auditorium_seating_to_string(&auditorium_seating);
            }
            else {
                printf("\nThe seat at row %d and column %d is taken.", row, col);
            }
        }
        // Read the next guestInfo
        printf("Please enter a guest information or enter \"Q\" to quit.");
        /*** reading a guest's information ***/
        scanf_s("%s", guest_info, sizeof(guest_info));
    }
}
  • 2
    Please read [How to create a minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) – Hans Petter Taugbøl Kragset Sep 20 '17 at 07:22
  • 1
    [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) --> You can't... means your `int total = sizeof(a->seating);` fails. – Andre Kampling Sep 20 '17 at 07:23
  • 3
    You asked yesterday about the same code you show now and I commented already: <> I don't want to offend you but if you won't do that you will have questions again and again. It is simply more effective to really learn the basic stuff before asking here. – Andre Kampling Sep 20 '17 at 07:32
  • That is a lot of unnecessary code. Please edit your question down to the minimum code needed to reproduce the question. –  Sep 20 '17 at 20:34

1 Answers1

1

The sheer number of problems you seem to be experiencing are generally reflective of a certain percentage of the population who doesn't read books. I'm just putting it out there, that it seems like you're one of those people, and that it's pretty obvious to many of us. Do you want to seem like one of those people?

Hopefully, you can prove me wrong, as those people shouldn't even bother to ask here if they're unwilling to read...


int total = sizeof(a->seating);

What even is this? Okay, for a start, a sizeof expression is a size_t type, not an int type. This makes sense when you don't want to have to write negative-handling code such as check_boundaries. It saves those who invest in reading a book many, many keystrokes.

You need to modify your struct auditorium_seating (which is currently nothing more than a misguided typedef) to store that information, as the sizeof operator does most of its work at compile time rather than runtime, so it can't retrieve the size of malloc-returned objects.

You seem to have some dilemma with regards to this, so allow me to get you started:

struct auditorium_seating {
    size_t row_size, column_size;    // and along with this comes more code throughout your project
    struct guest **seating;
};

int check_boundaries(size_t row, size_t column, struct auditorium_seatching *a) {
    return (row < a->row_size && column < a->column_size);
}

When you modify seating (i.e. initialise, or "add elements to it"), you might need to also modify row_size or column_size. Go forth and make these changes to your code. That is all.


void main()

In C, main should (almost) always be declared to return int. When it isn't, you should know why it isn't. In this case, there's no evidence to suggest that you do know why you chose void, where-as there should be.

Just use int, here, and... heed this warning: C is not the kind of language which necessarily punishes you with an error message when you do something wrong. Mistakes often go unnoticed for many years before they crop up as security vulnerabilities, heartbleed being one example.

Please, if you haven't done so already, save us from another heartbleed, and get yourself a book to read!


/* we will read line by line **/

Spoiler alert! That's not what the manual says!

"s Matches a sequence of bytes that are not white-space characters."

It wouldn't surprise me if you missed this small detail, as those who prefer not to read books also prefer not to read in general. If you're one of those people, you shouldn't be a programmer. Pick fruit, instead.


printf("\nA guest information is read.");

There is a reason we put the '\n' at the end. You would need to read to find that reason, of course. Hey, you're reading this answer! Why not read a book, which would tell you all about all of these things (and more)?

'\n' causes an implicit fflush when it's written to a line-buffered stream. Hence, if you want to ensure that output appears immediately on the terminal, you need to put the '\n' at the end, or call fflush(stdout); after printf... Your choice. I'd rather the less hideous options.

autistic
  • 1
  • 3
  • 35
  • 80
  • thanks for the help :D but the main was already given in the question (couldn't do anything about that). i resolved the array size matter by setting up global variables, and accessing them from there. – Deathknight921 Sep 21 '17 at 00:03
  • You're... helpless? I very highly doubt that. You could... stand up in class and say... "This guy is telling me it's wrong to use `void main`. Why is he saying that?" – autistic Sep 21 '17 at 07:26