0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct data{ 
    char help[5];
    struct data *next;
    struct data *prev;
}*head = NULL, *tail = NULL, *curr = NULL, *temp = NULL;

void backpush(char input[5]){
    curr = (struct data*) malloc(sizeof(struct data));
    strcpy(curr->help, input);
    if(head == NULL){
        head = tail = curr;
        head->next = NULL;
        head->prev = NULL;
    }
    else{
        tail->next = curr;
        curr->prev = tail;
        curr->next = NULL;
        tail = curr;
    }
}

int main(){
    char code[][5] = {"7R", "4R", "6B", "0B", "8R", "5R", "3B"};
    for(int i = 0;i<7;i++){
        backpush(code[i]);
    }
}

So in int main i try this

curr = head; printf("%s", curr->help[0]);

but the result is error, i add help[0] because i just want to print '7' not '7R', is there something wrong?

5
  • 1
    curr->help[0] has type char. You can use the %c printf specifier to print it. The %s printf specifier expects a char * pointing to a null-terminated string. Commented Apr 3, 2024 at 15:08
  • 1
    @Fernando Gunawan, Save time and enable all warnings. printf("%s", curr->help[0]); typical generates a warning as to why it is bad. Commented Apr 3, 2024 at 15:10
  • 1
    @FernandoGunawan, "the result is error" --> this lacks detail. Better to post the exact error message or description. Commented Apr 3, 2024 at 15:12
  • return value not 0, yeah i just remember the correct is %c not %s Commented Apr 3, 2024 at 15:19
  • try printf("first letter of node at head is '%c'\n", curr->help[0]);. If you want to print a single letter use %c specifier. Your code does not even have a list. And do not try to print one, as the topic title suggest. I believe you need a better model... Commented Apr 3, 2024 at 22:55

1 Answer 1

1

To print a single character, use %c:

printf("%c", curr->help[0]);

To print a string, use %s:

printf("%s", curr->help);

curr->help[0] is a single character.

curr-help is an array, which is converted to a pointer to its first element, so it represents a string by pointing to the first character of a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.