array of chars in c code example

Example 1: how to print char array in c

char a_static[] = { 'q', 'w', 'e', 'r', '\0' };
char b_static[] = { 'a', 's', 'd', 'f', '\0' };

printf("value of a_static: %s\n", a_static);
printf("value of b_static: %s\n", b_static);
return 0;

Example 2: return char array in c

char * createStr() {

    char char1= 'm';
    char char2= 'y';

    char *str = malloc(3);
    str[0] = char1;
    str[1] = char2;
    str[2] = '\0';

    return str;

}

Example 3: how to feed a char array to function in C

//If you know the size of the array you can pass it like this
void function(char array[10]) {
	//Do something with the array...
}

int main() {
	char array[] = {'a', 'b', ..., 'j'};
    function(array);
}

Tags:

Cpp Example