简体   繁体   English

引用指向字符数组的指针,strtok的示例

[英]referencing pointers to character arrays, a strtok worked example

I am working in C trying to tokenize an array and then store the tokens to a global array of strings. 我正在C语言中尝试对数组进行标记化,然后将标记存储到字符串的全局数组中。 The catch is that i am trying to do this with pointers so I do not have to refrence the index of the array of strings. 问题是我正在尝试使用指针来执行此操作,因此我不必引用字符串数组的索引。 I know how big the array is supposed to be which is why it is easy to do it with the index, HOWEVER; 我知道数组应该有多大,这就是为什么使用索引很容易做到这一点; i am trying to do this with just pointers. 我正在尝试仅使用指针。 I don't know if this is possible so correct me here. 我不知道是否有可能,所以请在这里纠正我。 Here is the code I have tried to implement but not been successful.. 这是我尝试实现但未成功实现的代码。

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

            char *cPayload2[PARAMS];

    void ReadIn2(char *input)
    {
        //Initialize the pointer to the 
        char *PayloadPtr;
        //start the parse
        char *token = strtok(input, "#");
        //pointer to an array of strings(pointers to character arrays)
        PayloadPtr = &cPayload2[0];

        while(token != NULL)
        {

This is the part in question, can i change the index of my global array with a clause like this. 这是有问题的部分,我可以用这样的子句更改全局数组的索引吗? It seems I cannot print out the payload array with this. 看来我无法用此打印出有效负载数组。

            *PayloadPtr = token;
            //increment the index that the ptr refrences
            PayloadPtr++;
            //tokenize again
            token = strtok(NULL, "#");
        }

    }



    int main(void)
    {

        char input[] = "jsiUjd3762BNK==#KOIDKKkdkdwos==";

        ReadIn2(input);

This printout is bunked for some reason 由于某些原因,此打印输出被打乱了

        printf("%s\n",cPayload2[0]);
        printf("%s\n",cPayload2[1]);

        return 0;
    }

Any tips would be greatly appriciated. 任何技巧将不胜枚举。

char *PayloadPtr;

should be 应该

char **PayloadPtr;

apart from that, your code is alright. 除此之外,您的代码还可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM