此问题在此处已有答案:
Initializing a pointer in a separate function in C(2个答案)
5年前关闭。
我有一个C程序,它提示用户输入行数和列数,以构造教室座位的2D数组。该程序在大小为2x2时工作正常,但当我输入3或更大的行数或列数时,它会终止,并出现以下错误
“有一个问题导致这个程序停止工作.”
下面是我的代码片段:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//declaring structure called student
struct student {
//members of the structure
char last_name[30];
char first_name[30];
};
//declaring structure called classroom_seating
struct classroom_seating {
struct student **seating ;
};
//Functions implementation for struct student.
void student_init_default(struct student *s) {
//initializing the data members to string "???"
strcpy(s->first_name, "???");
strcpy(s->last_name, "???");
}
void student_init(struct student *s, char *info) {
/* Use the strtok function to extract first name and
last name from the variable student, then assign
them to each instance variable of the student
structure*/
strcpy(s->first_name, strtok(info, "/"));
strcpy(s->last_name, strtok(NULL, "/"));
}
void student_to_string(struct student *s) {
//printing the initial character of the first name, a period, the initial character of the last name, and a
//period
printf("%c.%c.", s->first_name[0], s->last_name[0]);
}
//end of step1
//step2
//Functions implementation for struct classro classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){
//It instantiates a two-dimensional array of the size "rowNum" by "columnNum" specified by the
//parameters inside the struct a. Then it initializes each student element of this array using the student_init_default function
void classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){
a->seating=malloc((sizeof(a->seating[rowNum][columnNum])));
//initializing the counters
int i = 0;
int j = 0;
for (i = 0; i < rowNum; i++) {
for (j = 0; j < columnNum; j++) {
student_init_default(&a->seating[i][j]);
}
printf("\n");
}
}
int assign_student_at(int row, int col,struct classroom_seating *a,struct student *s) {
char str1[30];
strcpy(str1, a->seating[row][col].first_name);
if (strcmp(str1, "???"))
{
strcpy(a->seating[row][col].first_name,s->first_name);
strcpy(a->seating[row][col].last_name,s->last_name);
return 1;
}
else {
return 0;
}
}
int check_boundaries(int row, int col, struct classroom_seating *a) {
if (row < 0 || col < 0) {
return 0;
}
else if (row > sizeof(a->seating) || col > sizeof(a->seating[0])) {
return 0;
}
else {
return 1;
}
}
void classroom_seating_to_string(struct classroom_seating *a) {
printf("The Current Seating\n");
printf("-------------------\n");
int i = 0;
int j = 0;
for (i = 0; i < sizeof(a->seating); i++) {
for (j = 0; j < sizeof(a->seating[0]); j++) {
student_to_string(&a->seating[i][j]);
}
printf("\n");
}
}
//end of functions implementation.
//main function
int main(){
//variable declaration
struct student s;
struct classroom_seating a;
char info[30];
int rowNum,colNum,row,col;
//taking the inputs from the user,,,enter number of rows and columns that the array will have
printf("Please enter a number of rows for an classroom seating.");
scanf("%d",&rowNum);
printf("Please enter a number of columns for an classroom seating.");
scanf("%d",&colNum);
//
classroom_seating_init(rowNum,colNum,&a);//calling method to create a 2D array for class arrangement.
printf("Please enter a student information or enter \"Q\" to quit.");
scanf("%s",&info);
student_init(&s, info);
while (1) {
printf("\nA student information is read.\n");
// printing student information.
student_to_string(&s);
printf("\n");
// 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
if (check_boundaries(row, col, &a) == 0) {
printf("\nrow or column number is not valid.");
printf("A student %s %s is not assigned a seat.\n",s.first_name, s.last_name);
}
else {
// Assigning a seat for a student
if (assign_student_at(row, col, &a, &s)== 1) {
printf("\nThe seat at row %d and column %d is assigned to the student\n", row, col);
student_to_string(&s);
classroom_seating_to_string(&a);
}
else {
printf("\nThe seat at row %d and column %d is taken.\n", row, col);
}
}
// Read the next studentInfo
printf("Please enter a student information or enter \"Q\" to quit.");
// reading a student's information
scanf("%s", info);
}
return 0;
}
字符串
1条答案
按热度按时间djmepvbi1#
给定
字符串
然后
型
不是你想要的。直接的问题是你的结构定义不能模拟现实世界的座位问题。首先你需要填充总的座位容量。
您可以将定义更改为
型
那么
classroom_seating_init
可以是型
您可以选择在
struct student
中添加填充字段型