如何解决为什么 printf 不使用字符串在 c 中打印任何内容?
> printf("名称: %s\n",temp->name);
这不会在我的输出中打印任何内容,但如果我使用 %C
代替它打印字符串的第一个字符。
这是我的完整代码:
#include <stdio.h>
#include <stdlib.h>
struct lkg {
char *name;
char *gender;
struct lkg *link;
} *new,*temp,*front = NULL,*rear = NULL,*new2,*front2 = NULL,*rear2 = NULL,*temp2;
int isempty1() {
return (front == NULL);
}
void enqueue1(char *data,char *gend) {
new = malloc(sizeof(struct lkg));
new->name = data;
new->gender = gend;
if (isempty1) {
front = rear = new;
printf("\ninsertion sucess\n");
printf("\n__________________\n");
} else {
rear->link = new;
rear = new;
}
}
int isempty2() {
return (front2 == NULL);
}
void enqueue2(char *data,char *gend) {
new2 = malloc(sizeof(struct lkg));
new2->name = data;
new2->gender = gend;
if (isempty2) {
front2 = rear2 = new2;
} else {
rear2->link = new2;
rear2 = new2;
}
}
void displayA() {
temp = front;
while (temp != NULL) {
printf("Name: %s\n",temp->name);
temp = temp->link;
}
}
void displayB() {
temp2 = front2;
while (temp2 != NULL) {
printf("Name: %c",temp2->name);
temp2->link = temp2;
}
}
void main() {
int choice;
char *name,*gender;
do {
printf("press 1 to enter enroll in LKG A \n Press 2 to enter enroll in LKG B\n press -1 to exit:\n");
scanf("%d",&choice);
if (choice == 1) {
printf("you are enrolling for lkgA\n");
printf("enter the name and gender:\n");
scanf("%s%s",&name,&gender);
enqueue1(name,gender);
} else
if (choice == 2) {
printf("you are enrolling for lkg- B");
printf("enter the name and gender");
scanf("%s%s",&gender);
enqueue2(name,gender);
} else {
printf("not a valid choice");
break;
}
} while (choice != -1);
displayA();
displayB();
}
我也尝试过 \n
和 fflush
,但在我的场景中不起作用。我想我在指针中犯了错误,但不知道如何解决问题。
解决方法
您的代码中存在多个问题:
- 您将未初始化的指针传递给
scanf()
以进行%s
转换。您应该改为传递指向实际char
数组的指针。 -
enqueue
函数必须复制参数字符串,因为它们将被下一个输入覆盖。 -
main
的返回类型为int
,如果成功,应该返回0
。 - 您应该将大部分全局变量设为本地变量。
这是修改后的版本:
#include <stdio.h>
#include <stdlib.h>
struct lkg {
char *name;
char *gender;
struct lkg *link;
} *front = NULL,*rear = NULL,*front2 = NULL,*rear2 = NULL;
int isempty1() {
return (front == NULL);
}
void enqueue1(char *data,char *gend) {
struct lkg *new = malloc(sizeof(struct lkg));
new->name = strdup(data);
new->gender = strdup(gend);
if (isempty1) {
front = rear = new;
printf("\ninsertion success\n");
printf("\n__________________\n");
} else {
rear->link = new;
rear = new;
}
}
int isempty2() {
return (front2 == NULL);
}
void enqueue2(char *data,char *gend) {
struct lkg *new = malloc(sizeof(struct lkg));
new->name = strdup(data);
new->gender = strdup(gend);
if (isempty2) {
front2 = rear2 = new;
} else {
rear2->link = new;
rear2 = new;
}
}
void displayA() {
struct lkg *temp = front;
while (temp != NULL) {
printf("Name: %s\n",temp->name);
temp = temp->link;
}
}
void displayB() {
struct lkg *temp = front2;
while (temp != NULL) {
printf("Name: %c",temp->name);
temp->link = temp;
}
}
int main() {
int choice;
char name[100],gender[10];
do {
printf("Enter 1 to enter enroll in LKG A\n"
"Enter 2 to enter enroll in LKG B\n"
"Enter -1 to exit:\n");
if (scanf("%d",&choice) != 1)
break;
if (choice == 1) {
printf("you are enrolling for lkgA\n");
printf("enter the name and gender:\n");
scanf("%99s%9s",&name,&gender);
enqueue1(name,gender);
} else
if (choice == 2) {
printf("you are enrolling for lkgB\n");
printf("enter the name and gender:\n");
scanf("%99s%9s",&gender);
enqueue2(name,gender);
} else {
printf("not a valid choice\n");
break;
}
} while (choice != -1);
displayA();
displayB();
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。