I'm building a text editor using doubly linked lists. These are my structs:
#define N 4
typedef struct node
{
char data[N];
int size;
struct node* next;
struct node* prev;
}node;
typedef struct text
{
struct node* head;
struct node* tail;
int count;
int size;
}text;
This is the piece of code that I use to fill the first node.
void push_text (text * t, char * s)
{
int i;
int len = strlen(s);
node *newnode, *nextnode, *head;
newnode = create_node();
t->count++;
head = newnode;
for (i=0; i<len; i++)
{
if (newnode->size < 4)
{
newnode->data[newnode->size] = s[i];
newnode->size++;
}
.
.
.
When I print the node through printf or through the debugger the output is 4 chars long, as expected. Note that, I print it as soon as the first node is filled so the problem lies in this piece of code. However, when I use strlen(newnode->data)
I get an output of 5. This is causing me many problems later on.
What's wrong here?