botch botch - 2 months ago 6
C Question

Struct Char not Assigning properly

Im trying to make a linked list type of data structure, and at the moment it only has a single

char
as data, but I can't get it to assign properly. When I run the following code:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

FILE* outfile;
FILE* infile;

int linkedlist_size;
struct linked_list
{
char* data;
struct linked_list* next;
struct linked_list* previous;
};

int main()
{
outfile = fdopen(STDOUT_FILENO,"w");
infile = fdopen(STDIN_FILENO,"r");

struct linked_list line_buffer;
struct linked_list* current = &line_buffer;
int linkedlist_size = 0;

int input_char;
char input_cast;
for (input_char = fgetc(infile); (input_char != EOF && input_char != '\n') ;input_char = fgetc(infile))
{
input_cast = input_char;
current->data = malloc(sizeof(char));
(current->data)[0] = input_cast;
linkedlist_size++;
current->next = malloc(sizeof(struct linked_list));
current = current->next;
printf("\nMy address is: %p",current);
printf("\nMy number is: %d",input_char);
printf("\nMy char cast is: %c",input_cast);
printf("\nMy char is: %s",current->data);
}

return 0;
}


Compiled with
gcc ll_test.c
,run with
./a.out
, and using
something
as input from the keyboard, I get the following output:

My address is: 0x10558a0
My number is: 115
My char cast is: s
My char is: (null)
My address is: 0x1055cf0
My number is: 111
My char cast is: o
My char is: (null)
My address is: 0x1055d30
My number is: 109
My char cast is: m
My char is: (null)
My address is: 0x1055d70
My number is: 101
My char cast is: e
My char is: (null)
My address is: 0x1055db0
My number is: 116
My char cast is: t
My char is: (null)
My address is: 0x1055df0
My number is: 104
My char cast is: h
My char is: (null)
My address is: 0x1055e30
My number is: 105
My char cast is: i
My char is: (null)
My address is: 0x1055e70
My number is: 110
My char cast is: n
My char is: (null)
My address is: 0x1055eb0
My number is: 103
My char cast is: g
My char is: (null)


Which means that the letters are going into
STDIN
properly, are being interpreted properly (the loop stops after typing a
\n
) and the cast is being done properly, but the assignment is not working. For what it is worth, I also tried making
linked_list.data
a regular
char
and assigning directly (via
current->data = input_cast
) and received similar results (blank output, rather than
(null)
, implying a
\0
was "printed"). I presume this is some nitpicky point about structs that I am not familiar with, but I can't for the life of me figure out what it is. Feel free to grab/compile/test the code as you please.

Also, I am aware there is a memory leak... this is a modified snippet from a larger body of code, so many features are not academic perfection. I just wanted to demonstrate the behavior I was getting.

Thanks everybody!

EDIT: As noted below, the error was that I was trying to print the char of my current node AFTER I had switched to the next, empty node. Stupid logic error on my part.

Answer

You need to allocate 2 bytes, like this: current->data = malloc(2);First bytes will store you character and second byte will store string terminator '\0', after that you can print it as a string. You forget to use your previous field you can:

 current->next = malloc(sizeof(struct linked_list));
 current->next->previous=current;
 current = current->next;

You printing your string from newly allocated node, it is not initialized in time you printing it. Move your line current = current->next; above printf statements.