[英]Why printf does not print anything in c with string?
> printf("Name: %s\n", temp->name); > printf("名称: %s\n", temp->name);
This will not print anything in my output, but if I use %C
instead it prints first character of a string.这不会在我的 output 中打印任何内容,但如果我使用
%C
代替它会打印字符串的第一个字符。
Here is my complete code:这是我的完整代码:
#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", &name, &gender);
enqueue2(name, gender);
} else {
printf("not a valid choice");
break;
}
} while (choice != -1);
displayA();
displayB();
}
I have tried \n
and fflush
too but it doesn't work in my scenerio.我也尝试过
\n
和fflush
但它在我的场景中不起作用。 I think I have made mistakes in pointers but don't know how to solve the problem.我想我在指针上犯了错误,但不知道如何解决问题。
There are multiple issues in your code:您的代码中有多个问题:
scanf()
for the %s
conversion.scanf()
以进行%s
转换。 You should instead pass pointers to actual char
arrays.char
arrays。enqueue
functions must make copies of the argument strings as they will be overwritten by the next input. enqueue
函数必须复制参数字符串,因为它们将被下一个输入覆盖。main
has a return type of int
and should return 0
for success. main
有一个返回类型int
并且应该返回0
表示成功。 Here is a modified version:这是修改后的版本:
#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", &name, &gender);
enqueue2(name, gender);
} else {
printf("not a valid choice\n");
break;
}
} while (choice != -1);
displayA();
displayB();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.