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);
sizeof(char)-- the argument tostrncpyis the number of characters to copy, not a size.charand "size" in C are the same thing.malloc(), where one multiples the number of things by the size of each thing to get the size argument. The argument tostrncpyis not a number of things, it's specifically the number of characters to copy.