簡體   English   中英

來自指向c中字符串的指針數組的字符數組

[英]Array of chars from an array of pointer to strings in c

我有一個來自用戶的行輸入,我存儲在一個字符input數組中。 我將字符串input復制到另一個數組words並使用帶有空格分隔符的strtok將單個單詞存儲到指向字符串astArray[]的指針數組中。 現在單個單詞存儲在*astArray指向的內存位置和*token指向的單詞的單個字符中。

在修改了需要對單詞執行的操作后,我想用存儲在astArray的字符串重新構建 char input數組。 我想將所有字符串復制到字符input數組中,它們之間有一個空格,最后是一個空值。 我該怎么做? 我試過strcpy但我得到的字符之間沒有空格。

內存已分配。

* token
token = strtok(words, " ");
loop
strcpy(astArray[i], token);
char *astArray[] = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
char input[128];
int wc = 9;

char *out = input;
int i;
for(i=0;i<wc;++i){
    char *p = astArray[i];
    while(*p){
        *out++=*p++;
    }
    if(i<wc-1){
        *out++ = ' ';
    }
}
*out = '\0';
printf("%s\n", input);

暫無
暫無

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

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