1

I have my tokens ready but I want them to store in an array.

I have tried making an array and storing tokens in but when i print out array in my case str[0].

it only prints out a character not the whole token.

   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]);

I ran this on code

./check "true and false; 1 + 2"

and the output it give me is this

true and false
 1 + 2
r

My expected output should be 1 + 2 as I am trying to store whole token in one index of array str[0]: true and false

str[1]: 1 + 2

EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • `if (ptr[j] != ';') {...} else { if (ptr[j] == ';') {... }}` the `if (ptr[j] == ';')` is superfluous. See: [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) (answer: No) *"`str[0]`. it only prints out a character"*, just what type do you think `str[0]` is? (hint: `char`) What makes you think it will store more than that? – David C. Rankin Sep 27 '19 at 20:50
  • `str` is a pointer to char and `str[0]` therefore is a single character. You want a pointer to pointer to char, then you can have multiple chars in every element. – Sami Kuhmonen Sep 27 '19 at 20:53
  • 1
    Besides good @DavidC.Rankin comment to not cast result of `malloc()` you must check is it memory allocation on `str` successful or not. – EsmaeelE Sep 27 '19 at 20:53
  • second if `if (ptr[j] == ';' )` is useless. – EsmaeelE Sep 27 '19 at 20:56

1 Answers1

0

I think this work for you. And i will complete it soon and add more explanation.

Ugly version.

Code

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

Edit

Better Version

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

Static vs Dynamic, for/while version

#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;
}
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31