您好以下是我的代码,我需要在check_boundary函数中寻求帮助,因为我如何才能检索初始化的2d(就座)数组,以便可以将其与新的行和列号进行比较,目前我在该函数中获得0 0行和列如果我打印出来。座位数组在auditium_seating_init函数中实例化。

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


这个功能

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


这是我的主要

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

最佳答案

您似乎遇到的大量问题通常反映出一定比例的不读书的人口。我只是把它放在那儿,似乎您是其中的一员,对我们许多人来说都很明显。您想看起来像其中一个人吗?

希望你能证明我是错的,因为那些人​​甚至不应该在这里问他们是否不愿意阅读...



int total = sizeof(a->seating);


这到底是什么?好的,首先,sizeof表达式是size_t类型,而不是int类型。当您不想编写负处理代码(例如check_boundaries)时,这很有意义。这样可以节省那些花很多时间在读书上的人。

您需要修改您的struct auditorium_seating(目前仅是误导的typedef)来存储该信息,因为sizeof运算符在编译时而不是在运行时完成其大部分工作,因此它无法检索malloc返回的对象的大小。

您似乎对此感到两难,所以请允许我开始:

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


修改seating(即初始化或“向其中添加元素”)时,可能还需要修改row_sizecolumn_size。继续对您的代码进行这些更改。就这些。




void main()



在C中,应始终(几乎)声明main以返回int。如果不是,您应该知道为什么不是。在这种情况下,没有证据表明您确实知道为什么选择void,应该在哪里。

只需在此处使用int,然后...注意此警告:当您执行错误操作时,C并不是那种会通过错误消息来惩罚您的语言。在安全漏洞面前,错误常常被忽视很多年,而令人流血的例子就是其中之一。

请,如果您还没有这样做的话,请让我们免于再次伤心,并让自己读书。




/* we will read line by line **/



扰流板警报!这不是the manual所说的!

s匹配不是空格字符的字节序列。”

如果您错过了这么小的细节,也不会令我感到惊讶,因为那些不愿读书的人也更喜欢不读书。如果您是其中的一员,则不应成为程序员。采摘水果。




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



我们将'\n'放在最后是有原因的。当然,您需要阅读以找到该原因。嘿,您正在阅读此答案!为什么不读一本书,它可以告诉您所有这些事情(以及更多)?

'\n'在写入行缓冲流中时会导致隐式fflush。因此,如果要确保输出立即显示在终端上,则需要将'\n'放在末尾,或者在fflush(stdout);之后调用printf。我宁愿选择那些不太可怕的选择。

关于c - 在C中访问2d数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46315630/

10-11 22:44