CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2001
    Posts
    76

    Termination of character string array

    How do I terminate character string array?

    The following example does not work!!!

    void main (void) {

    int i;
    char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "" };

    // notice "" - empty string at the end of array

    char **work_day;

    work_day=workdays;

    while (*work_day) printf("%d\n", i++);

    exit(1);
    }

    This example will print numbers from 1 to xxx instead of printing from 1 to 6.

    I need explanation why "" at the end of array isn't treated as \n?

    Tomislav


  2. #2
    Join Date
    Oct 2001
    Posts
    76

    Re: Termination of character string array

    Sorry instead of \n should be \0!!!



  3. #3
    Join Date
    Oct 2001
    Posts
    29

    Re: Termination of character string array

    Hi,
    What you had done is u never incremened the pointer to point to the next location. U can do that like this
    [ccode]
    void main (void) {

    int i=0;
    char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "" };

    // notice "" - empty string at the end of array

    char **work_day;

    work_day=&workdays[0];

    while (strlen(*work_day)>0)
    {
    printf("%s\n", *work_day);
    i++;
    work_day=&workdays[i];


    }


    }
    [/code]

    Regards,
    Balaji.
    If it helps rate it


  4. #4
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Termination of character string array

    Another approach:


    int main (void)
    {
    char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

    for (int i = 0; i < sizeof(workdays)/sizeof(*workdays); ++i)
    printf("%d\0", i);

    exit(1);
    }







    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Member of Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  5. #5
    Join Date
    May 2001
    Location
    Kansas City, Missouri
    Posts
    45

    Re: Termination of character string array

    The simple answer is that '\n' and "" are not the same in C++. Either use '\n' and '\n' or "" and '\0'.


  6. #6
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Termination of character string array

    There are two problems with this.
    The first as balijj pointed out, is that you are not incrementing work_day, so that it always points to workdays[0]. Since that is never 00, the loop never terminates.

    That's easy enough to fixed.while (*work_day++) printf("%d\n", i++);


    But that's not going to solve your problem, which is a bit more fundemental.

    work_day is a pointer to a char pointer, or, more exactly, a pointer into an array of char pointer. So first work_day points to workdays[0], then, after incrementing, it points to workdays[1], and so on.

    workdays[0], workdays[1], etc are themselves pointer. workdays[0] *points* to the text literal "Monday", and workdays[5] *points* to the text literal "".

    Now, let's look at your test while (*work_day)

    which says "loop until what work_day points to is zero". But work_day only points to char*, and each of the char* in workdays have non-zero values, that is, they all point to something. (What that something is, is irrelevant).

    So, we don't want workdays[5] to *point* to a zero. workdays[5] must *be* a zero. The is also easily accomplished:char *workdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 0 };






    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured