【问题标题】:Storing tokens in array将令牌存储在数组中
【发布时间】:2019-09-27 20:45:39
【问题描述】:

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

我尝试过创建一个数组并将令牌存储在其中,但是当我在我的情况下打印出数组时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 因为我试图将整个令牌存储在数组的一个索引中 str[0]: true and false

str[1]: 1 + 2

【问题讨论】:

  • if (ptr[j] != ';') {...} else { if (ptr[j] == ';') {... }} if (ptr[j] == ';') 是多余的。见:Do I cast the result of malloc?(答案:否)str[0]。它只打印一个字符”,你认为str[0]是什么类型? (提示:char)是什么让您认为它会存储更多?
  • str 是指向 char 的指针,str[0] 因此是单个字符。你想要一个指向 char 的指针,然后你可以在每个元素中有多个 char。
  • 除了很好的@DavidC.Rankin 评论以不转换malloc() 的结果,您必须检查str 上的内存分配是否成功。
  • 第二个如果if (ptr[j] == ';' )没用。

标签: c malloc command-line-arguments


【解决方案1】:

我认为这对你有用。我会尽快完成并添加更多解释。

丑陋的版本。

代码

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 2015-11-14
    • 2011-12-02
    • 1970-01-01
    • 2015-09-15
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多