在数组中存储令牌

Storing tokens in array

我已准备好令牌,但我希望将它们存储在一个数组中。

我已经尝试制作一个数组并将标记存储在其中,但是当我在我的案例中打印出数组时 str[0]

它只打印出一个字符而不是整个标记。

   int i=0, j=0, t=0;
   char *ptr = argv[1];
   char *str;
   str =(char*) malloc(10 * sizeof(int));

   while (ptr[i] != '[=10=]')
   {
       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 因为我试图将整个令牌存储在数组的一个索引中 str[0]: true and false

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] != '[=10=]')
    {   
        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]!='[=11=]'){

        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: 

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]!='[=12=]'){

        if (ptr[i]!=';'){
            array1[flag][j]=ptr[i];     
            j++;
        }else if(ptr[i]==';'){
            flag=1;
            j=0;
        }
        i++;
    }
*/

    for(int i=0;ptr[i]!='[=12=]';i++){
        array1[flag][j]=ptr[i];     
        j++;
        if(ptr[i]==';'){
            flag=1;
            j=0;
        }
    }

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

    return 0;
}