Programing help needed, using string/char in C

Discussion in 'OT Technology' started by Disguy, Apr 2, 2007.

  1. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    I have some understanding of programing but I need some help with strings. I want to create an array of strings and use it later. I'm not sure how to do this.

    i think that this is about how it should look.

    char list[5] = {cat, dog, bird, eagle, snake};
    int count =0;

    for (count =0; count =< 5; count++)
    printf(" List[count] ");

    _____________
    appears on screen as

    cat dog bird eagle snake
     
  2. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    What you're trying to do is create a two-dimensional character grid, with each row storing a single word. Your code is only creating a one-dimensional character array, and so it can only store one word total.

    Are you using C or C++ or C#? This will make a huge difference in the way you handle strings.
     
  3. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    i am using c
     
  4. kilamasta

    kilamasta New Member

    Joined:
    Aug 14, 2006
    Messages:
    108
    Likes Received:
    0
    char *list[5]={"1","2","3","4","5"};
    or
    char list[5][5]={"1","2","3","4","5"};
    or
    char **list;
    list=(char**)malloc(sizeof(char*)*5);
    /* then for each string*/
    list[0]=(char*)malloc(sizeof(char)*(strlen("cat")+1));
    strcpy(list[0],"cat");


    left square is how many string and right square is the longest a string can be.


    for (count =0; count =< 5; count++)
    printf(" %s",List[count] ");

    OH! The count should be like this :
    for (count =0; count < 5; count++)
     
  5. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Ugh. I don't know how anyone still uses C anymore. That code is nasty.
     
  6. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    #include<stdio.h>
    #include<stdlib.h>

    int main()
    {
    int count = 0;
    char list[2][3]={"1","2"};

    list=(char**)malloc(sizeof(char*)*5);

    list[0]=(char*)malloc(sizeof(char)*(strlen("cat")+1));
    strcpy(list[0],"cat");

    list[0]=(char*)malloc(sizeof(char)*(strlen("dog")+1));
    strcpy(list[0],"dog");

    //left square is how many string and right square is the longest a string can be.

    for (count =0; count =1; count++)
    printf(" %s",list[count] );
    }

    ______________

    Error test.c: 9 operands of = have illegal types 'array 2,3 of char' and 'pointer to pointer to char'
    Error test.c: 9 the left hand side of the assignment can't be assigned to
    Warning test.c: 11 missing prototype for strlen
    Warning test.c: 11 Missing prototype for 'strlen'
    Error test.c: 11 operands of = have illegal types 'array 3 of char' and 'pointer to char'
    Error test.c: 11 the left hand side of the assignment can't be assigned to
    Warning test.c: 12 missing prototype for strcpy
    Warning test.c: 12 Missing prototype for 'strcpy'
    Warning test.c: 14 Missing prototype for 'strlen'
    Error test.c: 14 operands of = have illegal types 'array 3 of char' and 'pointer to char'
    Error test.c: 14 the left hand side of the assignment can't be assigned to
    Warning test.c: 15 Missing prototype for 'strcpy'
    6 errors, 7 warnings
    1 error

    Tool completed with exit code 1
     
  7. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    btw this is for my c programming class and we haven't touched strings yet, so i am prolly doing some simple mistakes.
     
  8. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    god dude those were different examples of ways to do it with the ones after the first one being really horrible.

    char * list[5] = {"dog", "cat", "bird', "eagle", "whatever"};

    for (int i =0; i<5; i++)
    printf("%s ", list);
     
  9. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    those were some wild examples, it generally never looks like that
     
  10. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    Well, C is still widely popular. It's also whats taught to CS students the first year in most universities.

    C is almost identical to C++ on a per-line basis (minus OOD/OOP)
     
  11. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    int main()
    {
    int count = 0;
    char * list[5] = {"dog", "cat", "bird", "eagle", "whatever"};

    for (int i =0; i<5; i++)
    printf("%s ", list[count]);

    list=(char**)malloc(sizeof(char*)*5); //error here

    list[0]=(char*)malloc(sizeof(char)*(strlen("cat")+1));
    strcpy(list[0],"cat");

    list[0]=(char*)malloc(sizeof(char)*(strlen("dog")+1));
    strcpy(list[0],"dog");

    //left square is how many string and right square is the longest a string can be.

    for (count =0; count <5; count++)
    printf(" %s",list[count] );
    }

    ___

    Error test.c: 13 operands of = have illegal types 'array 5 of pointer to char' and 'pointer to pointer to char'
    Error test.c: 13 the left hand side of the assignment can't be assigned to
    2 errors, 0 warnings
    1 error

    Tool completed with exit code 1
     
  12. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    take this stuff out

    for (int i =0; i<5; i++)
    printf("%s ", list[count]);

    list=(char**)malloc(sizeof(char*)*5); //error here

    list[0]=(char*)malloc(sizeof(char)*(strlen("cat")+1));
    strcpy(list[0],"cat");

    list[0]=(char*)malloc(sizeof(char)*(strlen("dog")+1));
    strcpy(list[0],"dog");

    those were other examples of how to initialize that array
    this should be your whole program


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    int main()
    {
    int count = 0;
    char * list[5] = {"dog", "cat", "bird", "eagle", "whatever"};

    for (count =0; count <5; count++)
    printf(" %s",list[count] );
    }
     
  13. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    thanks that works much better!!
     
  14. gnp

    gnp New Member

    Joined:
    Mar 11, 2007
    Messages:
    280
    Likes Received:
    0
    apparently not very well
     
  15. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    :rofl:
     

Share This Page