c splitting a char* into an char**

后端 未结 4 898
孤城傲影
孤城傲影 2021-01-22 09:20

I\'m reading in a line from a file (char by char, using fgetc()), where all fields(firstname, lastname, ...) are seperated by an ;. What I now want to do is create

4条回答
  •  -上瘾入骨i
    2021-01-22 09:36

    I now want to do is create a char**, add all the chars to that

    You would allocate an array of char (char*) to store the characters, not an array of char* (char**).

    And when I create a char*, e.g. char * buf = malloc(80) can I treat it like a one dimensional array? If the memory returned by malloc contiguous?

    Yes, malloc always returns continguous blocks. Yes, you can treat it as single-dimensional an array of char* (with 80/sizeof char* elements). But you'll need to seperately allocate memory for each string you store in this array, or create another block for storing the string data, then use your first array to store pointers into that block.

    (or something else; lots of ways to skin this cat)

提交回复
热议问题