Thread: How do you create and print an array of characters

  1. #1
    Unregistered
    Guest

    How do you create and print an array of characters

    I can't seem to find anything on how to print out an array of characters:

    I want to make a table of dashes such as below:
    ----------
    ----------
    ----------

    How would I go about doing that?

    so far, my code goes like this:
    int i, m=0;
    char bufferarray [30];
    for (i=0; i <31; i++)
    {
    bufferarray[i] = '\45';
    }
    for (m=0; m<i; m++)
    {
    printf("%s \n", bufferarray);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When indexing an array with a loop, use one of the following:

    char array[SIZE];

    for( x = 0; x < SIZE; x++ )

    or

    for( x = 0; x <= (SIZE-1); x++ )

    but not

    for( x = 0; x <= SIZE; x++ )

    Remember: And array is a total of SIZE elements, and it's indexes range from 0 to SIZE-1.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    how come when i use /045 to get a dash, i end up with a percent sign? That's weird, huh?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The counter should run to 30, not 31, since the first index is 0. So the counter runs from 0 until 29.

    Code:
    for (i = 0; i < 30; i++) 
    { 
        bufferarray [i] = '\45'; 
    }
    You could consider printing each character one by one.

    Code:
    for (i = 0; i < NR_OF_LINES; i++)
    {
        for (m = 0; m < 30; m++)
        {
            printf ("%c", bufferarray [m]);
        }
        printf ("\n");
    }
    If you want to print the array as a string, note that the array must be ended with the null-character: '\0'.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: How do you create and print an array of characters

    Originally posted by Unregistered
    I can't seem to find anything on how to print out an array of characters:

    I want to make a table of dashes such as below:
    ----------
    ----------
    ----------

    How would I go about doing that?

    so far, my code goes like this:
    int i, m=0;
    char bufferarray [30];
    for (i=0; i <31; i++)
    {
    bufferarray[i] = '\45';
    }
    for (m=0; m<i; m++)
    {
    printf("%s \n", bufferarray);
    }
    %s expects a NUL ('\0') terminated character arrays ( the '\0' signals the end of the string ) and it prints all the the characters till it finds the NUL character
    so , do this instead

    Code:
    char bufferarray[31];
    int i;
    for(i=0;i<31;i++) bufferarray[i]='\45';
    bufferarray[31]='\0';
    printf("%s \n",bufferarray);
    the second loop is not requried
    Also , most string manipulation functions declared in string.h expect NUL terminated character arrays .
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char bufferarray[31];
    int i;
    for(i=0;i<31;i++) bufferarray[i]='\45';
    bufferarray[31]='\0';
    Another way of initializing the array to null values upon creation is to do:

    char bufferarray[SIZE] = {0};

    You can initialize array elements upon creation as I've done above. Any element you do not specificly set will be set to zero, provided you set at least one element to something.

    Thus, by setting the first one to zero, the rest default to zero, and we end up with a "null filled" array.

    [edit]
    char bufferarray[31];
    int i;
    for(i=0;i<31;i++) bufferarray[i]='\45';
    bufferarray[31]='\0';

    One correction:
    bufferarray[31] = '\45';

    This is beyond the scope of the array, and may crash your program.
    [/edit]



    Quzah.
    Last edited by quzah; 07-09-2002 at 03:13 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest
    thank you very much....

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > bufferarray[i] = '\45';
    Mmm
    bufferarray[i] = '-';

    Is so much clearer, no?

  9. #9
    Unregistered
    Guest

    Talking

    didn't know that would work... out thought myself, i guess

    Also, any suggestions on how to format it like i wanted? to do a carriage return every nth one?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can include a newline a few ways:

    1) Include it on the end of your string (array) and just print the whole string at once, X number of times.

    2) Include a statement in your loop:
    Code:
    for( x = 0; x < SOMETHING; x++ )
    {
        printf("%s", array );
        if( x % 10 == 9 )
            printf("\n");
    }
    3) Use a pair of loops:
    Code:
    for( x = 0; x < LINES; x++ )
    {
        for( y = 0; y < CHARACTERS; y++ )
            printf("%c", array[y] );
        printf("\n");
    }
    Take your pick.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by quzah


    Another way of initializing the array to null values upon creation is to do:

    char bufferarray[SIZE] = {0};

    You can initialize array elements upon creation as I've done above. Any element you do not specificly set will be set to zero, provided you set at least one element to something.

    Thus, by setting the first one to zero, the rest default to zero, and we end up with a "null filled" array.

    [edit]
    char bufferarray[31];
    int i;
    for(i=0;i<31;i++) bufferarray[i]='\45';
    bufferarray[31]='\0';

    One correction:
    bufferarray[31] = '\45';

    This is beyond the scope of the array, and may crash your program.
    [/edit]



    Quzah.
    Yes , it should be
    buffearray[30]='\0';
    thanks for the correction
    The one who says it cannot be done should never interrupt the one who is doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create name array help beginner
    By moscoworbust in forum C Programming
    Replies: 2
    Last Post: 06-08-2009, 06:04 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM