Referencing String Array

This is a discussion on Referencing String Array within the C Programming forums, part of the General Programming Boards category; Hi and thank for any help in this issues. I have a program who declare the following variables Code: #define ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    15

    Smile Referencing String Array

    Hi and thank for any help in this issues.

    I have a program who declare the following variables

    Code:
    #define idbuffsize 256
    #define nreswrd 59
    
    char idbuff[idbuffsize];
    char* reswrd[59];
    I then initiate the array of reswrd[0] with the following way

    Code:
        reswrd[  0] = "ARRAY";
        reswrd[  1] = "BEGIN";
        reswrd[  2] = "BY";
        reswrd[  3] = "CASE";
    I also used idbuff as a way to store every single character that i am reading such as idbuff[k] = ch; where ch is a character at a time.

    At the end I want to compare both string idbuff to my list of array of reswrd allowing me to see if yes or not in the list

    Now the following code does work

    Code:
    printf("%s", idbuff);
    char* foo;
    foo = idbuff;
    char* bar;
    bar = reswrd[1];
    
    if( cmpstr(foo, bar) == 0)...
    where reswrd[1] would work and compare the string but that not what i want it to achieve. I want to do the following.

    int i;
    and would like to go through the list of reswrd at i items and just do a loop or something to allow me to read it. However when running my program it freeze, i am wondering if i am miss something in c string structure or with pointer. Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Well you would actually need to show the code for your loop.
    Code:
    for ( i = 0 ; i < nreswrd ; i++ )
     if ( strcmp( idbuff, reswrd[i] ) == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    15
    I am not even trying to do a loop, atm
    Code:
    int i;
    i = 0;
    printf("%s", reswrd[i]);
    but at the end i want it to do this

    Code:
        do{
            //printf("%s", idbuff);
            char* foo;
            foo = idbuff;
            char* bar;
            bar = reswrd[1];
            //printf("%s\n", reswrd[0]);
    
    
            if(strcmp(foo, bar ) == 0)
            {
                reswrdflag = TRUE;
            }
            else
            {
                i++;
            }
        }while (reswrdflag != TRUE  && i < nreswrd);
    reswrdflag is boolean ( which is predefine at the start with #define TRUE 1, and nreswrd is = to 58 because there is technically 58 items in nreswrd word.

    Thanks for the quick reply i appreciate it.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    15
    One thing i have noticed, in my coding is when doing a reference
    //printf("%s\n", reswrd[0]); it freeze, however when doing a reference
    //printf("%s\n", reswrd[1]); it works, is there something i am missing with string of array in c???

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Without seeing a complete program, it's impossible to tell how you're messing this up.

    Sure I can take the random lines you've posted, and it will work just fine. But it won't tell you anything new and interesting that will help you solve your problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct/Array/Referencing Question
    By aussiemcgr in forum C Programming
    Replies: 6
    Last Post: 11-29-2010, 11:19 PM
  2. Referencing a structure inside an array
    By ridilla in forum C Programming
    Replies: 2
    Last Post: 05-19-2010, 07:12 PM
  3. Referencing and De-referencing Question
    By audinue in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 08:22 AM
  4. Array size referencing struct member
    By Russell in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 12:41 PM
  5. Referencing part of an array.
    By Russell in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21