- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我正在为 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/
我有一个为 Firefox 3.6 编写的附加组件,现在我正在将其升级到 Firefox 4.0,同时尝试使其与 3.6 兼容。有没有人有尝试这样做的经验,或者关于如何在代码变得太意大利面条式的情况下
我已经安装了 Cassandra 2.0.1 并想在我的应用程序中使用 Astyanax Java API。我在维基上看到了 Cassandra 兼容性表,上面写着 Astyanax 使用 Netfl
是否可以使纯粹在 VBScript(无 COM 对象)中实现的自定义容器类与 For Each 语句一起使用?如果是这样,我必须公开哪些方法? 最佳答案 简而言之,没有 为什么?创建一个可枚举的集合类
我这里的代码很少 int b=3; b=b >> 1; System.out.println(b); 它工作得很好,但是当我将变量 b 更改为 byte、short、float、double 时它包含
我们有一个 Java 客户端,它使用 corba 调用多个第三方系统。这些是实现同一组接口(interface)的不同系统。我们获得了使用这些接口(interface)的库(jar 文件)。例如,这些
我知道从技术上讲 HTML5 是一个“实时规范”,但我想知道它是否符合在类名中添加尾随空格的规定。我没有在规范中看到任何对这种情况的引用,但我的一个队友说它是无效的。也许我错过了什么? 修剪这些空间会
我在 Linux x86-64 上用 C 语言编程。我正在使用一个库,它通过原始 clone 创建多个线程系统调用而不是使用 pthread_create .这些线程运行库内部的低级代码。 我想钩住这
我希望用汇编程序编写一个可启动程序,能够发送和接收网络数据包。我不想使用任何库,我想自己创建它(并在这样做的同时学习)。不幸的是,我无法找到有关最低级别的网卡通信(发送原始套接字)的任何信息。我相信有
是否有除 fixed scoping 之外没有任何更改的 CoffeeScript 分支,以便它在很大程度上与 CoffeeScript 兼容(如果代码没有外部变量赋值则完全兼容)?我会考虑使用可接受
这个问题已经有答案了: Why is BiConsumer allowed to be assigned with a function that only accepts a single para
我的 Java 应用程序需要一个高性能主内存数据库 1] 请建议数据库 -符合 JDBC -独立(即平面文件) -支持内存表 -高性能 -B-TREE索引 2] JAVA中是否有任何技术可以在程序运行
我通常会找到一些以char*作为参数的函数,但是我听说在C++中更推荐std::string。如何将std::string对象与以char* s为参数的函数一起使用?到目前为止,我已经知道了c_str
我正在移植我的一个旧 javascript 文件以与 requireJS 兼容。这是以前代码的样子。 // effect.js (function(exports){ // shorthand
在今天更新我的 SDK 之前,我有工作代码(为了将来引用,请查看问题询问日期)。 .getMap 曾经发出警告,表明它已被弃用,但现在它甚至不被识别为有效输入。我假设这是因为 API 24(Andro
根据 this reference sheet on hyperpolyglot.org , 下面的语法可以用来设置一个数组。 i=(1 2 3) 但是我在 dash 上遇到错误,它是 Ubuntu
我的 MacBook 上安装了 MYSQL 8.0.12(下载版本)。当我尝试转储 mysql40 的兼容版本时,收到错误 Invalid mode to --known: mysql40。我 100
您好,我正在更改我的版本控制系统,我调查了 perforce 是否与 bcm 补救措施兼容。有谁知道其他版本的控制系统也与 bcm 补救措施兼容?? 最佳答案 BMC Remedy 会更接近 Clea
我需要在 python 中的图像上绘制一般坐标网格。我可以计算网格线的像素坐标,因此我只需要一个能够将它们绘制为图像顶部的虚线 的模块。图像以 numpy 数组的形式出现,因此我需要能够在这些格式和绘
库接受文件输入的“传统”方式是做这样的事情: def foo(file_obj): data = file_obj.read() # Do other things here 客户端代
代码 Untitled Document #topDropDownMenu { position: relative;
我是一名优秀的程序员,十分优秀!