0

I am trying to split a string in two to pass to two children. the first part should be 12 '0' characters and the second should be 13 '1' characters. When I execute this code, I do not get either of these things. specifically this is the output. Parent Left: 00000000000 Parent Right: 0000000000001 I have no idea why, can someone help?

int bit_count(char *passed, int len){ int left = len/2; int right =(len/2)+(len % 2); char *lstring = malloc(sizeof(char)*(left+1)); char *rstring = malloc(sizeof(char)*(right+1)); strncpy(lstring, passed, sizeof(char)*12); strncpy(rstring, passed+left, sizeof(char)*13); printf("Parent Left: %s\n", lstring); printf("Parent Right: %s\n", rstring); 
6
  • why calculate left and right if you are just gonna hardcode 12 and 13 here. It makes no sense. Commented Oct 12, 2013 at 19:35
  • right, I changed it to that just to try to understand what was happening. eventually I will user left and right. Commented Oct 12, 2013 at 19:36
  • 2
    You don't need to multiply by sizeof(char) -- the argument to strncpy is the number of characters to copy, not a size. Commented Oct 12, 2013 at 19:36
  • @Barmar, the second part of your comment is a bit misleading. Number of char and "size" in C are the same thing. Commented Oct 12, 2013 at 20:28
  • @JensGustedt I'm trying to distinguish it from how one uses malloc(), where one multiples the number of things by the size of each thing to get the size argument. The argument to strncpy is not a number of things, it's specifically the number of characters to copy. Commented Oct 12, 2013 at 20:31

3 Answers 3

2

Don't use strncpy if you don't need it and if you don't know exactly what it does.

In your case it is completely superfluous, you know the size of the array that you want to copy, anyhow. So use memcpy:

memcpy(lstring, passed, left); lstring[left] = '\0'; memcpy(rstring, passed+left, right); rstring[right] = '\0'; 
Sign up to request clarification or add additional context in comments.

3 Comments

"Don't use strncpy if you don't need it and if you don't know exactly what it does." The same could be said of any function. But yes, strncpy is particularly misleading.
Thanks. This worked, and you're right, I don't understand strncpy. Maybe someday.
A very nice explanation of the strncpy() function.
2

The problem is that strncpy() doesn't add a null terminator if it stops because of the limit rather than reaching the end of the source string. So you need to add the null terminator.

int bit_count(char *passed, int len) { int left = len/2; int right = len - left; char *lstring = malloc(left+1); char *rstring = malloc(right+1); strncpy(lstring, passed, left); lstring[left] = '\0'; strncpy(rstring, passed+left, right); rstring[right] = '\0'; printf("Parent Left: %s\nParent Right: %s\n", lstring, rstring); } 

Comments

0

Add NULL character after each strncpy

lstring[left]='\0'; rstring[right]='\0'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.