C programming printing array of characters

59,075

Solution 1

Your array is not initialized, and also you have an array of pointers, instead of an array of char's. It should be char* array = (char*)malloc(sizeof(char)*size);, if you want an array of char's. Now you have a pointer to the first element of the array.

Solution 2

You declare an array of characters like so:

char foo[size];

You seem to have it mixed up with char *, which is a pointer to a character. You could say

char *bar = foo;

which would make bar point to the contents of foo. (Or, actually, to the first character of foo.)

To then print the contents of the array, you can do one of the following:

// either print directly from foo:
printf("%s", foo);
// or print through bar:
printf("%s", bar);

Note, however, that C performs no initialization of the contents of variables, so unless you specifically set the contents to something, you'll get garbage. In addition, if that garbage doesn't happen to contain a \0; that is, a char with value 0, it will keep on outputting past the end of the array.

Solution 3

Why are we making such a simple thing sound so difficult?

char array[SIZE];
... /* initialize array */
puts(array);   /* prints the string/char array and a new line */
/* OR */
printf("%s", array); /* prints the string as is, without a new line */

The char in array after the end of what you want to be your string (ie. if you want your string to read "Hello" that would be the next char after the 'o') must be the terminating NUL character '\0'. If you use a C function to read input that would automatically be appended to the end of your buffer. You would only need to worry about doing it manually if you were individually writing characters to your buffer or something for some reason.

EDIT: As with pmg's comment, the '\0' goes wherever you want the string to end, so if you wanted to shorten your string you could just move it up closer to the front, or to have an empty string you just have array[0] = '\0';. Doing so can also be used to tokenise smaller strings inside a single buffer, just as strtok does. ie. "Part1\0Part2\0Part3\0". But I think this is getting away from the scope of the question.

ie. you wanted to store the first 3 chars of the alphabet as a string (don't know why anyone would do it this way but it's just an example):

char array[4];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = '\0';
printf("%s\n", array);

If you have something like char array[] = "Hello"; the '\0' is automatically added for you.

Share:
59,075
ilovetolearn
Author by

ilovetolearn

Updated on March 19, 2020

Comments

  • ilovetolearn
    ilovetolearn over 4 years

    I have an array of characters declared as:

    char *array[size];
    

    When I perform a

    printf("%s", array);
    

    it gives me some garbage characters, why it is so?

    http://www.cplusplus.com/reference/clibrary/cstdio/printf/

    This url indicates printf takes in the format of: `int printf ( const char * format, ... );

    #include <stdio.h>
    #include <string.h>
    #define size 20
    #define buff 100
    char line[buff];
    
    int main ()
    {
        char *array[100];
        char *sep = " \t\n";
    
        fgets(line, buff, stdin);
    
        int i;
    
        array[0] = strtok(line, sep);
    
        for (i = 1; i < size; i++) {
            array[i] = strtok(NULL, sep);
    
            if (array[i] == NULL)
                break;
        }
    
        return 0;
    }