-1
 entry -> data 

holds

"key_string \0 value_string \0"

(that is, two concatenated and null terminated strings)

I want to pass the key and value into

kvstore_put(&(server ->store), key, value);

as arguments.

Zong
  • 6,160
  • 5
  • 32
  • 46
Jobs
  • 3,317
  • 6
  • 26
  • 52
  • 1
    Why not just run a loop to find the position of the first terminating char? Then copy from the start to that position, and that position+1 to the end. – Bill Harper Apr 29 '16 at 01:21
  • have you tried to use strtok(). Just keep in note that it will modify the string passed into it. [Similar Post](http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c) – James Peruggia Apr 29 '16 at 01:24
  • Because the delim is 0, `strlen` will work. – 001 Apr 29 '16 at 01:26
  • I agree with @BillHarper but you have to pay attention to know when to stop. If always is two strings to one, ok, you always expects a string after the first. But if you don't know how many strings you have to split, so you have to put some flag or something else to know when to stop and don't overflow the limits of your string. – ViniciusArruda Apr 29 '16 at 01:28
  • char *str2 = strchr(str1, 0)+1; – Mike Apr 29 '16 at 01:32

2 Answers2

2

You don't need to copy anything - it can all be done with pointers. Because it's null terminated, the input string can double as the key. The value can be set in a string pointer with:

char *pszValue = strchr (pszKeyString, 0)+1;

No copying, very simple implementation.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Mike
  • 2,721
  • 1
  • 15
  • 20
1

One example of the way:

#include <stdio.h>
#include <string.h>

void kvstore_put(char key_value[], char key[], char value[]){
    strcpy(key, key_value);
    strcpy(value, strchr(key_value, '\0') + 1);
}

int main(void){
    char kv[] = "key\0value\0";
    char key[sizeof kv];
    char value[sizeof kv];

    kvstore_put(kv, key, value);
    puts(key);
    puts(value);
    return 0;
}

another way:

void kvstore_put(char key_value[], char key[], char value[]){
    while(*key++ = *key_value++);
    while(*value++ = *key_value++);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70