> printf("Name: %s
", temp->name);
This will not print anything in my output, but if I use %C
instead it prints first character of a string.
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("
insertion sucess
");
printf("
__________________
");
} 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
", 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
Press 2 to enter enroll in LKG B
press -1 to exit:
");
scanf("%d", &choice);
if (choice == 1) {
printf("you are enrolling for lkgA
");
printf("enter the name and gender:
");
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
and fflush
too but it doesn't work in my scenerio. I think I have made mistakes in pointers but don't know how to solve the problem.
question from:
https://stackoverflow.com/questions/65641965/why-printf-does-not-print-anything-in-c-with-string 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…