Table of Contents

2. Char Arrays


2. Char Arrays

A char array is an array of characters that can store a sequence of characters, such as a word or a sentence. A char array can be declared as follows:

char name[10];

This declares a char array named name that can store up to 10 characters. The size of the array must be specified at the time of declaration and cannot be changed later. The elements of the array are indexed from 0 to size-1, where size is the number of elements in the array. For example, name[0] refers to the first character in the array, name[1] refers to the second character, and so on.

To initialize a char array, we can use either of the following methods:

char name[10] = {'J', 'o', 'h', 'n', '\0'};

This assigns the characters ‘J’, ‘o’, ‘h’, ‘n’ to the first four elements of the array and the null character ‘\0’ to the fifth element. The null character is used to mark the end of the string and is not considered as part of the string. The remaining elements of the array are filled with garbage values.

char name[10] = "John";

This assigns the string “John” to the array and automatically appends a null character at the end. The remaining elements of the array are filled with zeros.

To access or modify a char array, we can use either of the following methods:

printf("%s\n", name);

This prints the contents of the array as a string until it encounters a null character.

scanf("%s", name);

This reads a string from the standard input and stores it in the array. It stops reading when it encounters a whitespace character (such as space, tab, or newline) or when the array is full.

name[0] = 'M';

This changes the first character of the array to ‘M’.

name[4] = '!';

This changes the fifth character of the array to ‘!’ and overwrites the null character. This makes the array invalid as a string and may cause errors when printing or reading it.

To compare two char arrays, we cannot use the == operator, as it will only compare the addresses of the arrays and not their contents. Instead, we can use a function called strcmp from the string.h library, which compares two strings lexicographically (alphabetically) and returns an integer value indicating their relationship. For example:

#include
char name1[10] = "John";
char name2[10] = "Jane";
int result = strcmp(name1, name2);

If result is negative, then name1 comes before name2 in alphabetical order.

If result is zero, then name1 and name2 are equal.

If result is positive, then name1 comes after name2 in alphabetical order.

Another way to work with char arrays is to use pointers. A pointer is a variable that stores the address of another variable. For example:

char name[10] = "John";
char *ptr = name;

This declares a pointer named ptr and assigns it the address of the first element of the array name. We can use the * operator to access or modify the value stored at the address pointed by the pointer. For example:

printf("%c\n", *ptr);

This prints the first character of the array, which is ‘J’.

*ptr = 'M';

This changes the first character of the array to ‘M’.

We can also use pointer arithmetic to move the pointer to different elements of the array. For example:

ptr++;

This increments the pointer by one, which means it now points to the second element of the array.

printf("%c\n", *ptr);

This prints the second character of the array, which is ‘o’.

ptr += 2;

This adds two to the pointer, which means it now points to the fourth element of the array.

printf("%c\n", *ptr);

This prints the fourth character of the array, which is ‘n’.

We can also use pointers to pass char arrays as arguments to functions. For example:

void print(char *str) {
printf("%s\n", str);
}
char name[10] = "John";
print(name);

This defines a function named print that takes a pointer to a char array as a parameter and prints it as a string. Then it calls the function with the array name as an argument. The function will print “John” on the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *