我已准备好 token ,但我希望将它们存储在一个数组中。
我已经尝试制作一个数组并将标记存储在其中,但是当我在我的例子中打印出数组时 str[0]
。
它只打印出一个字符而不是整个标记。
int i=0, j=0, t=0;
char *ptr = argv[1];
char *str;
str =(char*) malloc(10 * sizeof(int));
while (ptr[i] != '\0')
{
if (ptr[j] != ';')
{
printf("%c", ptr[j]);
str[t] = ptr[j];
j++;
}else
{
if (ptr[j] == ';')
{
j++;
str[t] = ptr[j];
printf("%c\n", ptr[j]);
}
}
i++;
t++;
}
printf("\n%c",str[1]);
我在代码上运行了这个
./check "true and false; 1 + 2"
它给我的输出是这样的
true and false
1 + 2
r
我的预期输出应该是 1 + 2
因为我试图将整个 token 存储在数组的一个索引中
str[0]:真假
str[1]: 1 + 2
最佳答案
我认为这对你有用。我会尽快完成并添加更多解释。
丑陋的版本。
代码
#include <stdio.h>
int main(int argc, char *argv[]){
int i,j,t;
i=j=t=0;
int nrows=2;
int ncolumns=50;
char *ptr = argv[1];//"true and false; 1 + 2";
char *str;
char **array1 = malloc(nrows * sizeof(char *)); // Allocate row pointers
for(i = 0; i < nrows; i++)
array1[i] = malloc(ncolumns * sizeof(char)); // Allocate each row separately
i=0;
while (ptr[i] != ';')
{
array1[0][i]=ptr[i];
i++;
//printf("%c ",ptr[i]);
}
j=0;
i++;//not store ;
while (ptr[i] != '\0')
{
array1[1][j]=ptr[i];
i++;
j++;
}
printf("%s\n", array1[1]);
}
编辑
更好的版本
#include <stdio.h>
#include <stdlib.h> //for malloc
int main(int argc, char *argv[]){
int i,j,t;
i=j=t=0;
int nrows=2;
int ncolumns=50;
char *ptr = argv[1];//"true and false; 1 + 2";
char **array1 = malloc(nrows * sizeof(char *)); // Allocate row pointers
for(i = 0; i < nrows; i++){
array1[i] = malloc(ncolumns * sizeof(char)); // Allocate each row separately
}
i=0;
int flag=0;
while(ptr[i]!='\0'){
if (ptr[i]!=';'){
array1[flag][j]=ptr[i];
j++;
}else if(ptr[i]==';'){
flag=1;
j=0;
}
i++;
}
printf("%s\n", array1[1]);
return 0;
}
静态与动态,for/while 版本
#include <stdio.h>
#include <stdlib.h> //for malloc
///Can use either
///source: https://stackoverflow.com/a/2614257/7508077
char ** alloc_mem(int nrows, int ncolumns){
char ** array2 = malloc(nrows * sizeof(char *)); // Allocate the row pointers
array2[0] = malloc(nrows * ncolumns * sizeof(char)); // Allocate all the elements
for(int i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;
return array2;
}
char ** alloc_mem2(int nrows, int ncolumns){
char ** array2 = malloc(nrows * sizeof(char *)); // Allocate row pointers
for(int i = 0; i < nrows; i++){
array2[i] = malloc(ncolumns * sizeof(char)); // Allocate each row separately
}
return array2;
}
int main(int argc, char *argv[]){
int i,j,t;
i=j=t=0;
int nrows=2;
int ncolumns=50;
char *ptr;
if (argc==2){
ptr = argv[1];//"true and false; 1 + 2";
}else{
printf("Use true arguments...: ./check \"true and false; 1 + 2 \"\n ");
exit(0);
}
///Memory
//dynamic memory allocation for 2-D char array
//char **array1;
//array1=alloc_mem(nrows, ncolumns);
//or just static allocation with
char array1[nrows][ncolumns];
i=0;
int flag=0;
///use while or for, i prefer for
/*
while(ptr[i]!='\0'){
if (ptr[i]!=';'){
array1[flag][j]=ptr[i];
j++;
}else if(ptr[i]==';'){
flag=1;
j=0;
}
i++;
}
*/
for(int i=0;ptr[i]!='\0';i++){
array1[flag][j]=ptr[i];
j++;
if(ptr[i]==';'){
flag=1;
j=0;
}
}
printf("%s\n", array1[1]);
return 0;
}
关于c - 在数组中存储 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58141152/