Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

debugging - Why printf does not print anything in c with string?

> 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are multiple issues in your code:

  • you pass uninitialized pointers to scanf() for the %s conversion. You should instead pass pointers to actual char arrays.
  • the enqueue functions must make copies of the argument strings as they will be overwritten by the next input.
  • main has a return type of int and should return 0 for success.
  • you should make most of these global variables local.

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("
insertion success
");
        printf("
__________________
");
    } 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
", 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
"
               "Enter 2 to enter enroll in LKG B
"
               "Enter -1 to exit:
");
        if (scanf("%d", &choice) != 1)
            break;
        if (choice == 1) {
            printf("you are enrolling for lkgA
");
            printf("enter the name and gender:
");
            scanf("%99s%9s", &name, &gender);
            enqueue1(name, gender);
        } else
        if (choice == 2) {
            printf("you are enrolling for lkgB
");
            printf("enter the name and gender:
");
            scanf("%99s%9s", &name, &gender);
            enqueue2(name, gender);
        } else {
            printf("not a valid choice
");
            break;
        }
    } while (choice != -1);
    displayA();
    displayB();
    return 0;      
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...