gpt4 book ai didi

使用类型为 "struct student"的数组参数进行 C 编程与类型为 "struct student *"的参数不兼容错误

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

嗨,我正在为 C 类制作这个程序,我已经绞尽脑汁,我来到这里,因为我不能去我的老师或辅导部门,因为当我不工作时他们不可用。 .

我在使用这两个函数时遇到问题

void examination_seating_init(int rowNum, int columnNum, struct examination_seating* t)
{
struct student* placeholder;
for (int row = 0; row < columnNum; row++) {
for (int col = 0; col < rowNum; col++) {
*placeholder = t->seating[row][col];
student_init_default(placeholder);
}
}
}

void examination_seating_to_string(struct examination_seating* t)
{
char seatingchart = "The current seating: \n --------------------\n";
struct student *temp;
for (int row = 0; row < sizeof t->seating; row++) {
for (int col = 0; col < sizeof t->seating[0]; col++) {
if (col == sizeof t->seating[0] - 1) {
*temp = t->seating[row][col];
student_to_string(temp);
seatingchart = ("%c %c \n", seatingchart, temp);
}
else {
*temp = t->seating[row][col];
student_to_string(temp);
seatingchart = ("%c %c ", seatingchart, temp);
}

}
}
printf("%c", seatingchart);
}

这两个都给了我一个未初始化的局部变量使用错误,我不确定是否有更好的方法来做我想做的事情,或者我具体做错了什么,任何帮助都会很棒我已经在这里包含了我的完整程序以供引用.

我也尝试过这个

{
for (int row = 0; row < columnNum; row++) {
for (int col = 0; col < rowNum; col++) {
student_init_default(t->seating[row][col]);
}
}
}

但这给了我一个“struct Student”类型的参数与“struct Student *”类型的参数不兼容错误


#pragma warning(disable : 4996)

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

struct student {
char last_name[30];
char first_name[30];
};
struct examination_seating {
struct student** seating;
};
void student_init_default(struct student* p)
{
int i;
for (i = 0; i < sizeof p; ++i) {
p->first_name[i] = '###';
p->last_name[i] = '###';
}
}
void student_init(struct student* p, char* info)
{
char* names;
names = strtok(info, "/");
while (names != NULL)
{
p = names;
names = strtok(NULL, "/");
}
}

void student_to_string(struct student* p)
{
char initials = "";
initials = ("%c.%c", p->first_name[0], p->last_name[0]);
return initials;
}

void examination_seating_init(int rowNum, int columnNum, struct examination_seating* t)
{
struct student* placeholder;
for (int row = 0; row < columnNum; row++) {
for (int col = 0; col < rowNum; col++) {
*placeholder = t->seating[row][col];
student_init_default(placeholder);
}
}
}

int assign_student_at(int row, int col, struct examination_seating* t, struct student* p)
{
if (strcmp(t->seating[row][col].first_name, "###"))
{
t->seating[row][col] = *p;
}
else
{
return 0;
}
}

int check_boundaries(int row, int col, struct examination_seating* t)
{
if (sizeof t->seating > 0 | t->seating < row)
if (sizeof t->seating[0] > 0 | t->seating[0] < col)
{
return 1;
}
else
{
return 0;

}
}

void examination_seating_to_string(struct examination_seating* t)
{
char seatingchart = "The current seating: \n --------------------\n";
struct student *temp;
for (int row = 0; row < sizeof t->seating; row++) {
for (int col = 0; col < sizeof t->seating[0]; col++) {
if (col == sizeof t->seating[0] - 1) {
*temp = t->seating[row][col];
student_to_string(temp);
seatingchart = ("%c %c \n", seatingchart, temp);
}
else {
*temp = t->seating[row][col];
student_to_string(temp);
seatingchart = ("%c %c ", seatingchart, temp);
}

}
}
printf("%c", seatingchart);
}

void main() {
struct examination_seating examination_seating;
struct student temp_student;
int row, col, rowNum, columnNum;
char student_info[30];
// Ask a user to enter a number of rows for a examination seating
printf("Please enter a number of rows for a examination seating.");
scanf("%d", &rowNum);
// Ask a user to enter a number of columns for a examination seating
printf("Please enter a number of columns for a examination seating.");
scanf("%d", &columnNum);
// examination_seating
examination_seating_init(rowNum, columnNum, &examination_seating);
printf("Please enter a student information or enter \"Q\" to quit.");
/*** reading a student's information ***/
scanf("%s", student_info);
/* we will read line by line **/
while (1 /* change this condition*/) {
printf("\nA student information is read.");
// printing information.
printf("%s", student_info);
// student
student_init(&temp_student, student_info);
// Ask a user to decide where to seat a student by asking
// for row and column of a seat
printf("Please enter a row number where the student wants to sit.");
scanf("%d", &row);
printf("Please enter a column number where the student wants to sit.");
scanf("%d", &col);
// Checking if the row number and column number are valid
// (exist in the examination that we created.)
if (check_boundaries(row, col, &examination_seating) == 0) {
printf("\nrow or column number is not valid.");
printf("A student %s %s is not assigned a seat.", temp_student.first_name, temp_student.last_name);
}
else {
// Assigning a seat for a student
if (assign_student_at(row, col, &examination_seating, &temp_student) == 1) {
printf("\nThe seat at row %d and column %d is assigned to the student", row, col);
student_to_string(&temp_student);
examination_seating_to_string(&examination_seating);
}
else {
printf("\nThe seat at row %d and column %d is taken.", row, col);
}
}
// Read the next studentInfo
printf("Please enter a student information or enter \"Q\" to quit.");
/*** reading a student's information ***/
scanf("%s", student_info);
}
}

最佳答案

您似乎对 c 中指针的工作方式存在误解。
我将从这里开始:https://computer.howstuffworks.com/c20.htm它持续了好几页(点击下一页)。
现在解决手头的问题,您必须分配您所指向的空间。
请参阅此处如何操作:https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
还有一些有关 c: https://www.programiz.com/c-programming/c-strings 中字符串的信息
我已经解决了您询问的问题(以及其他一些问题),但是代码中可能还有更多问题。
我建议您详细了解 c 中的指针、分配和字符串。
其一,char不是字符串,必须分配char *。如果它在函数中分配并返回(如在 Student_to_string 中),则应该动态分配它或在函数外部分配它(就像我所做的那样)。

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

struct student {
char last_name[30];
char first_name[30];
};
struct examination_seating {
struct student** seating;
};
void student_init_default(struct student* p)
{
strcpy(p->first_name, "###");
strcpy(p->last_name, "###");
}
void student_init(struct student* p, char* info)
{
int i=0;
int j=0;
char * current = p->first_name;
while (info[i] != 0)
{
if (info[i] == '/')
{
current[j]=0;
current = p->last_name;
j=0;
}
else {
current[j] = info[i];
j++;
}
i++;
}
current[j]=0;
}

void student_to_string(struct student* p, char * initials)
{
sprintf(initials, "%c.%c", p->first_name[0], p->last_name[0]);
}

void examination_seating_init(int rowNum, int columnNum, struct examination_seating* t)
{
t->seating = malloc(rowNum * sizeof(struct student *));
for (int row = 0; row < columnNum; row++) {
t->seating[row] = malloc(columnNum* sizeof(struct student));
for (int col = 0; col < rowNum; col++) {
student_init_default(&(t->seating[row][col]));
}
}
}

int assign_student_at(int row, int col, struct examination_seating* t, struct student* p)
{
if (!strcmp(t->seating[row][col].first_name, "###"))
{
strcpy(t->seating[row][col].first_name, p->first_name);
strcpy(t->seating[row][col].last_name, p->last_name);
return 1;
}
else
{
return 0;
}
}

int check_boundaries(int row, int rowNum, int col, int colNum)
{
if (row > 0 | row < rowNum)
if (sizeof col > 0 | col < colNum)
{
return 1;
}
else
{
return 0;

}
}

void examination_seating_to_string(struct examination_seating* t)
{
char seatingchart[] = "The current seating: \n --------------------\n";
struct student *temp;
char initials[4];
for (int row = 0; row < sizeof t->seating; row++) {
for (int col = 0; col < sizeof t->seating[0]; col++) {
if (col == sizeof t->seating[0] - 1) {
student_to_string(&t->seating[row][col], initials);
sprintf(seatingchart,"%s %s \n", seatingchart, initials);
}
else {
student_to_string(&t->seating[row][col], initials);
sprintf(seatingchart,"%s %s ", seatingchart, initials);
}

}
}
printf("%s", seatingchart);
}

void main() {
struct examination_seating examination_seating;
struct student temp_student;
int row, col, rowNum, columnNum;
char student_info[30];
char initials[4];
// Ask a user to enter a number of rows for a examination seating
printf("Please enter a number of rows for a examination seating.");
scanf("%d", &rowNum);
// Ask a user to enter a number of columns for a examination seating
printf("Please enter a number of columns for a examination seating.");
scanf("%d", &columnNum);
// examination_seating
examination_seating_init(rowNum, columnNum, &examination_seating);
printf("Please enter a student information or enter \"Q\" to quit.");
/*** reading a student's information ***/
scanf("%s", student_info);
/* we will read line by line **/
while (1 /* change this condition*/) {
printf("\nA student information is read.");
// printing information.
printf("%s", student_info);
// student
student_init(&temp_student, student_info);
// Ask a user to decide where to seat a student by asking
// for row and column of a seat
printf("Please enter a row number where the student wants to sit.");
scanf("%d", &row);
printf("Please enter a column number where the student wants to sit.");
scanf("%d", &col);
// Checking if the row number and column number are valid
// (exist in the examination that we created.)
if (check_boundaries(row, rowNum, col, columnNum) == 0) {
printf("\nrow or column number is not valid.");
printf("A student %s %s is not assigned a seat.", temp_student.first_name, temp_student.last_name);
}
else {
// Assigning a seat for a student
if (assign_student_at(row, col, &examination_seating, &temp_student) == 1) {
printf("\nThe seat at row %d and column %d is assigned to the student", row, col);
student_to_string(&temp_student, initials);
examination_seating_to_string(&examination_seating);
}
else {
printf("\nThe seat at row %d and column %d is taken.", row, col);
}
}
// Read the next studentInfo
printf("Please enter a student information or enter \"Q\" to quit.");
/*** reading a student's information ***/
scanf("%s", student_info);
}
}

关于使用类型为 "struct student"的数组参数进行 C 编程与类型为 "struct student *"的参数不兼容错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59915274/

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