Reputation: 557
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?
Upvotes: 3
Views: 8290
Reputation: 1
All the answers explained before were correct but no one explain the mistake.
With an example you will see better the mistake
node.data[0] = 'a';
node.data[1] = 'b';
node.data[2] = 'c';
node.data[3] = 'd';
node.datasize = 4;
In a little endian machine the memory will be filled in that way:
Hex value of 'a', 'b', 'c','d' coded in a byte and four bytes to code the int
Memory: 'a''b''c''d'4000
Then you will obtain...
strlen(node.data) --> 5
printf(node.data) --> abcd (plus 4 ascii code).
Upvotes: -1
Reputation: 53026
You are not copying the nul
terminator, a c string needs a '\0'
at the end, so if it has 4 characters it uses 5 bytes, the last one being '\0'
which is not copied in your loop.
You should however use strcpy()
instead of copying they bytes one by one in a loop.
The strlen()
function scans the bytes until it finds the '\0'
, so the missing '\0'
is causing the Wrong Value!, also that's a reason not to call strlen()
in a loop, which is a very common mistake.
Upvotes: 13
Reputation: 727047
You cannot put a four-character C string into a four-element array of char
, because you need space for null terminator. Change all declarations of data
to
char data[N+1];
You should also use N
in place of constant 4
in expressions that expect the length to be less than N
(e.g. newnode->size < N
instead of newnode->size < 4
).
Upvotes: 7