簡體   English   中英

引用指向字符數組的指針,strtok的示例

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

我正在C語言中嘗試對數組進行標記化,然后將標記存儲到字符串的全局數組中。 問題是我正在嘗試使用指針來執行此操作,因此我不必引用字符串數組的索引。 我知道數組應該有多大,這就是為什么使用索引很容易做到這一點; 我正在嘗試僅使用指針。 我不知道是否有可能,所以請在這里糾正我。 這是我嘗試實現但未成功實現的代碼。

    #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)
        {

這是有問題的部分,我可以用這樣的子句更改全局數組的索引嗎? 看來我無法用此打印出有效負載數組。

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

    }



    int main(void)
    {

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

        ReadIn2(input);

由於某些原因,此打印輸出被打亂了

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

        return 0;
    }

任何技巧將不勝枚舉。

char *PayloadPtr;

應該

char **PayloadPtr;

除此之外,您的代碼還可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM