0

I have been working on some code that tokenizes a string from a line and then creates a temp array to copy the string into it (called copy[]) and it is filled with 0's initially (The end game is to split this copy array into temp arrays of length 4 and store them in a struct with a field char* Value). For some reason my temp arrays of size 4 end up having a size of 6.

char* string = strtok(NULL, "\"");
printf("%s", string);                         
int len = (int)strlen(string);

while(len%4 != 0) {
   len++;
}
char copy[len];
for(int i = 0; i < len; i++){
   copy[i] = '0';
}
printf("%s\n", copy);

int copyCount = 0;
int tmpCount = 0;
char temp[4];
while (copyCount < len) {
   if(tmpCount == 4) {
       tmpCount = 0;
   }
   while(tmpCount < 4) {
       temp[tmpCount] = copy[copyCount];
       tmpCount++;
       copyCount++;
   }
   printf("%s %d\n", temp, (int)strlen(temp));  
}

This yields:

This is the end
0000000000000000
This is the end0
This� 6
 is � 6
the � 6
end0� 6

And should yield:

This is the end
0000000000000000
This is the end0
This 4
 is  4
the  4
end0 4

I've been messing around with this for awhile and can't seem to figure out why its making temp have a length of 6 when I set it to 4. Also I'm not sure where the random values are coming from. Thanks!

3
  • Your snippet of code shows the first call to strtok as char* string = strtok(NULL, "\""); Have you called it once with the string to tokenize passed as the first argument? Commented Dec 3, 2013 at 22:21
  • 1
    Shouldn't you allocate "copy" array? Commented Dec 3, 2013 at 22:27
  • @AdrianKrupa I thought so too, but after looking around on SO for a minute it looks like char copy[len]; is valid C code nowadays, even in the middle of a function like this. Commented Dec 3, 2013 at 22:30

1 Answer 1

2

The reason is that your string temp is not null-terminated. C-style strings should be terminated with a \0 character. For some (lucky) reason there is a \0 three bytes in memory after wherever the end of temp lives, so when strlen tries to compute its length, it gets 6. This is also why printf is printing garbage: it will print temp until it finds the null terminator, and there are garbage characters in memory before printf reaches the null terminator.

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

2 Comments

So would making temp size 5 and adding a \0 after copying the 4 chars work?
Yes, it should. You don't need to add the \0 repeatedly. For example: char temp[5] = "xxxx"; will add the \0 for you automatically. You could do this explicitly with char temp[5]; temp[4] = '\0';.

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.