In the C programming language, a struct is a custom data type that allows for the grouping of diverse data types within a single variable identifier. The components within the struct are referred to as its members, which can encompass a variety of permissible data types. Structs offer a means to structure and encapsulate interconnected data.
What are Character Arrays in C?
Character arrays store sequences of characters, frequently utilized to depict strings in the C programming language.
In C, strings are represented as arrays of characters terminated by the null character ('\0').
Syntax:
It has the following syntax:
struct StructName structVariable = { "string value"};
1. Direct Initialization During Declaration
We have the option to employ direct initialization during declaration to set initial values for a character array within a struct in C. This involves specifying the initial values of the array directly within the struct declaration.
Example 1:
Let's consider an example to demonstrate the immediate initialization during declaration in the C programming language.
#include <stdio.h>
#include <string.h>
// defining struct name employee_data
struct Employee_data
{
// char array creation in a struct
char Name[50];
};
int main()
{
struct Employee_data person_1 = { "Rajin" };
struct Employee_data person_2 = { "Mogili" };
struct Employee_data person_3 = { "Pavan" };
printf("Person-1 Name is : %s\n", person_1.Name);
printf("Person-2 Name is : %s\n", person_2.Name);
printf("Person-3 Name is : %s\n", person_3.Name);
return 0;
}
Output:
Person-1's Name is : Rajin
Person-2's Name is : Mogili
Person-3's Name is : Pavan
Explanation:
This C code declares the Employee_data struct, containing a Name member as a character array with a maximum storage capacity of 50 characters. Three instances of this struct, Person 1, Person 2, and Person 3, are created and assigned unique names ("Rajin", "Mogili," and "Pavan"). Subsequently, printf functions are utilized to display the names of these individuals. Upon successful completion of the program, it returns 0.
Example 2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void DisplayCharArray(char *arr, size_t len)
{
printf(" The char array is : ");
for (int q = 0; q < len; ++q)
{
printf("%c, ", arr[q]);
}
printf("\n");
}
enum { LENGTH = 15, HEIGHT = 4 };
int main()
{
char array[LENGTH] = {'G',' O',' D',' I',' S', 'G', 'R', 'E',' A', 'T'};
DisplayCharArray(array, LENGTH);
exit(EXIT_SUCCESS);
}
Output:
The char array is: G, O, D, I, S, G, R, E, A, T, , , , , ,
Explanation:
- The program defines a function called DisplayCharArray for printing character arrays and includes the standard libraries string.h, stdio.h, and stdlib.h that are required.
- The DisplayCharArray function illustrates array traversal and character display by printing the characters from the input array accompanied by commas.
- Additionally, even though they are not utilized in this code, it defines the variables LENGTH and HEIGHT.
- A character array is first initialized in the main function with the characters 'G', 'O', 'D', 'I', 'S', 'G', 'R', 'E', 'A', 'T', and a length of 15.
- The message "The char array is:" and the contents of the array are then printed by using DisplayCharArray from the main function.
- Lastly, using exit(EXIT_SUCCESS), the programme ends its execution with a success status.
2. strcpy function
The strcpy function is a commonly used function in the C standard library that is declared in the string.h header file. It allows for the duplication of a string from one location to another. It requires two parameters: the target string (where the string will be duplicated) and the source string (the string that will be duplicated).
Initializing Char Array in Struct using strcpy:
- Define the Struct: The struct with the character array as one of its members should be defined.
- Declare an Instance: Declare an instance of the struct.
- Use strcpy: Use strcpy to copy a string into the character array member of the struct instance.
Example:
Let's consider a scenario to demonstrate the process of initializing a character array within a struct by utilizing the strcpy function in the C programming language.
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
// Define the struct
struct Employee {
char name[MAX_NAME_LENGTH];
int age;
};
int main() {
// Declare an instance of the struct
struct Employee person;
// Use strcpy to initialize the character array
strcpy(person.name, "Venkat");
person.age = 22;
printf("The name of the Person is : %s",person.name);
printf("\nThe age of the Person is : %d",person.age);
return 0;
}
Output:
The name of the Person is: Venkat
The age of the Person is: 22
Explanation:
- The code defines a struct called Employee, with a maximum name length of MAXNAMELENGTH (50 characters) and an integer age contained in a character array name.
- An instance of the Employee struct called Person is defined in the main function.
- The name member of the Person struct is initialized with the string "Venkat" using the strcpy function.
- The Person struct's age member has been set to be 22.
- Afterwards, the program uses printf to output the user's name and age.
- Upon successful execution, the programme returns 0.