gpt4 book ai didi

c - 在 C 中访问二维数组

转载 作者:行者123 更新时间:2023-11-30 20:20:26 25 4
gpt4 key购买 nike

您好,以下是我的代码,我需要 check_boundary 函数的帮助,因为我如何检索初始化的二维(座位)数组,以便我可以将它与新的行号和列号进行比较,目前我得到 0 0 行和列在那个函数中,如果我打印它。座位数组在 Auditorium_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));
}
}

最佳答案

您似乎遇到的问题的绝对数量通常反射(reflect)了一定比例的人口不读书。我只是想说,你似乎就是这些人中的一员,而且这对我们许多人来说都是显而易见的。您想看起来像这些人中的一员吗?

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

<小时/>
int total = sizeof(a->seating);

这到底是什么?好的,首先,sizeof 表达式是 size_t 类型,而不是 int 类型。当您不想编写诸如 check_boundaries 之类的负面处理代码时,这是有意义的。它为那些投资阅读一本书的人节省了很多很多的击键次数。

您需要修改您的structauditorium_seating(目前只不过是一个被误导的typedef)来存储该信息,如sizeof运算符的大部分工作在编译时而不是运行时完成,因此它无法检索malloc返回的对象的大小。 p>

您似乎在这方面遇到了一些困境,所以请允许我帮助您开始:

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

当您修改座位(即初始化或“向其添加元素”)时,您可能还需要修改row_sizecolumn_size 。继续对您的代码进行这些更改。仅此而已。

<小时/>
void main()

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

在这里只需使用int,并且...注意这个警告:C 不是那种当你做错事时必然会用错误消息来惩罚你的语言。错误在成为安全漏洞之前往往会被忽视很多年,heartbleed就是一个例子。

请,如果您还没有这样做,请让我们免于再次心碎,并给自己买本书来读!

<小时/>
/* we will read line by line **/

剧透警告!这不是the manual说!

s 匹配非空白字符的字节序列。”

如果您错过了这个小细节,我不会感到惊讶,因为那些不喜欢读书的人也喜欢不阅读一般内容。如果您是这些人中的一员,那么您就不应该成为一名程序员。相反,选择水果。

<小时/>
printf("\nA guest information is read.");

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com