Unit 5
Strings & Structure
In C programming, a string is a sequence of characters terminated with a null character \0.
For example:
char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.
Memory diagram of strings in C programming
To declare strings:
char s[5];
You can initialize strings in the following ways:
char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};
Assigning values to strings:
Arrays and strings are second-class citizens in C; they do not support the assignment operator once it is declared. For example,
char c[100];
c = "C programming"; // Error! array type is not assignable.
Read String from the user
Use scanf() function to read a string.
The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).
To read a string:
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output
Enter name: Dennis Ritchie
Your name is Dennis.
Creating a string
In the following example we are creating a string str using char character array of size 6.
char str[6] = "Hello";
The above string can be represented in memory as follows.
Creating a pointer for the string
The variable name of the string str holds the address of the first element of the array i.e., it points at the starting memory address.
So, we can create a character pointer ptr and store the address of the string str variable in it. This way, ptr will point at the string str.
In the following code we are assigning the address of the string str to the pointer ptr.
char *ptr = str;
The pointer variable ptr is allocated memory address 8000 and it holds the address of the string variable str i.e., 1000.
Accessing string via pointer
To access and print the elements of the string we can use a loop and check for the \0 null character.
In the following example we are using while loop to print the characters of the string variable str.
strlen():
strlen() function gives the length of the given string.
Syntax:
Strlen()
size_t_strlen(const char* str)
strlen() function counts the number of characters in a given string and returns integer value.
It stops counting the character when null character is found. Because, null character indicates the end of the string in C.
Program:
#include <stdio.h>
#include <string.h>
int main( )
{
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
printf ( "\string length = %d \n" , len ) ;
return 0;
}
OUTPUT:
string length = 17
strcpy():
The syntax of the strcpy() function is:
Syntax: char* strcpy (char* destination, const char* source);
The strcpy() function is used to copy strings. It copies string pointed to by source into the destination. This function accepts two arguments of type pointer to char or array of characters and returns a pointer to the first string i.e destination. Notice that source is preceded by the const modifier because strcpy() function is not allowed to change the source string.
#include<stdio.h>
#include<string.h>
int main()
{
char ch_arr1[20];
char ch_arr2[20];
printf("Enter first string (ch_arr_1): ");
gets(ch_arr1);
printf("Enter second string(ch_arr_1): ");
gets(ch_arr2);
printf("\nCopying first string into second... \n\n");
strcpy(ch_arr2, ch_arr1); // copy the contents of ch_arr1 to ch_arr2
printf("First string (ch_arr_1) = %s\n", ch_arr1);
printf("Second string (ch_arr_2) = %s\n", ch_arr2);
printf("\nCopying \"Greece\" string into ch_arr1 ... \n\n");
strcpy(ch_arr1, "Greece"); // copy Greece to ch_arr1
printf("\nCopying \"Slovenia\" string into ch_arr2 ... \n\n");
strcpy(ch_arr2, "Slovenia"); // copy Slovenia to ch_arr2
printf("First string (ch_arr_1) = %s\n", ch_arr1);
printf("Second string (ch_arr_2) = %s\n", ch_arr2);
// signal to operating system program ran fine
return 0;
}
Expected Output:
Enter first string (ch_arr_1): Mexico
Enter second string(ch_arr_1): South Africa
Copying first string into second...
First string (ch_arr_1) = Mexico
Second string (ch_arr_2) = Mexico
Copying "Greece" string into ch_arr1 ...
Copying "Slovenia" string into ch_arr2 ...
First string (ch_arr_1) = Greece
Second string (ch_arr_2) = Slovenia
strcat():
In C programming, the strcat() function contcatenates (joins) two strings.
The function definition of strcat() is:
char *strcat(char *destination, const char *source)
It is defined in the string.h header file.
strcat() arguments
the strcat() function takes two arguments:
destination - destination string
source - source string
The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";
// concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output
This is programiz.com
programiz.com
strcmp():
The C library function int strcmp(const char *str1, const char *str2) compares the string pointed to, by str1 to the string pointed to by str2.
Declaration
Following is the declaration for strcmp() function.
int strcmp(const char *str1, const char *str2)
Parameters
- str1 − This is the first string to be compared.
- str2 − This is the second string to be compared.
Return Value
This function return values that are as follows −
- if Return value < 0 then it indicates str1 is less than str2.
- if Return value > 0 then it indicates str2 is less than str1.
- if Return value = 0 then it indicates str1 is equal to str2.
#include <stdio.h>
#include <string.h>
int main () {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret < 0) {
printf("str1 is less than str2");
} else if(ret > 0) {
printf("str2 is less than str1");
} else {
printf("str1 is equal to str2");
}
return(0);
}
Let us compile and run the above program that will produce the following result −
str2 is less than str1
The array of characters is called a string. “Hi”, “Hello”, and e.t.c are the examples of String. Similarly, the array of Strings is nothing but a two-dimensional (2D) array of characters. To declare an array of Strings in C, we must use the char data type.
Example of two dimensional characters or the array of Strings is,
char language[5][10] = {"Java", "Python", "C++", "HTML", "SQL"};
Declaration of the array of strings
Syntax:-
char string-array-name[row-size][column-size];
Here the first index (row-size) specifies the maximum number of strings in the array, and the second index (column-size) specifies the maximum length of every individual string.
For example, char language[5][10]; In the “language” array we can store a maximum of 5 Strings and each String can have a maximum of 10 characters.
In C language, each character take 1 byte of memory. For the “language” array it will allocate 50 bytes (1*5*10) of memory. Where each String will have 10 bytes (1*10) of memory space.
Initialization of array of strings
Two dimensional (2D) strings in C language can be directly initialized as shown below,
char language[5][10] = {"Java", "Python", "C++", "HTML", "SQL"};
char largestcity[6][15] =
{"Tokyo", "Delhi", "Shanghai", "Mumbai", "Beijing", "Dhaka"};
The two dimensional (2D) array of Strings in C also can be initialized as,
char language[5][10] ={
{'J','a','v','a','\0'},
{'P','y','t','h','o','n','\0'},
{'C','+','+','\0'},
{'H','T','M','L','\0'},
{'S','Q','L','\0'}
};
Since it is a two-dimension of characters, so each String (1-D array of characters) must end with null character i.e. ‘\0’
2d array of strings in C
The second way of declaring the array of strings is a lengthy process, and other programmers can’t read them easily compared to the previous declaration, that why most of the time we prefer the first declaration.
The each String in this array can be accessed by using its index number. The index of array always starts with 0.
language[0] => "Java";
language[1] => "Python";
language[2] => "C++";
language[3] => "HTML";
language[4] => "SQL";
Note1:- the number of characters (column-size) must be declared at the time of the initialization of the two-dimensional array of strings.
// it is valid
char language[ ][10] = {"Java", "Python", "C++", "HTML", "SQL"};
But that the following declarations are invalid.
// invalid
char language[ ][ ] = {"Java", "Python", "C++", "HTML", "SQL"};
// invalid
char language[5][ ] = {"Java", "Python", "C++", "HTML", "SQL"};
The two-dimensional array of strings can be read by using loops. To read we can use scanf(), gets(), fgets() or any other methods to read the string.
// reading strings using for loop
for(i=0;i<n;i++)
{
scanf("%s[^\n]",name[i]);
}
Or,
// reading strings using while loop
int i=0;
while(i<n)
{
scanf("%s[^\n]",name[i]);
i++;
}
The two-dimensional array of strings can be displayed by using loops. To display we can use printf(), puts(), fputs() or any other methods to display the string.
// displaying strings using for loop
for(i=0;i<n;i++)
{
puts(name[i]);
}
Or,
// displaying strings using while loop
int i=0;
while (i<n)
{
puts(name[i]);
i++;
}
An array of pointers to strings is an array of character pointers where each pointer points to the first character of the string or the base address of the string.
To declare and initialize an array of pointers to strings.
char *sports[5] = {
"golf",
"hockey",
"football",
"cricket",
"shooting"
};
Here sports is an array of pointers to strings. If initialization of an array is done at the time of declaration, then we can omit the size of an array. So, the above statement can also be written as:
char *sports[] = {
"golf",
"hockey",
"football",
"cricket",
"shooting"
};
It is important to note that each element of the sports array is a string literal and since a string literal points to the base address of the first character, the base type of each element of the sports array is a pointer to char or (char*).
The 0th element i.e arr[0] points to the base address of string "golf". Similarly, the 1st element i.e arr[1] points to the base address of string "hockey" and so on.
Here is how an array of pointers to string is stored in memory.
The following program demonstrates how to access strings literal in the array of pointers to string and in the process prints the address of each string literal.
#include<stdio.h>
#include<string.h>
int factorial(int );
int main()
{
int i = 1, *ip = &i;
char *sports[] = {
"golf",
"hockey",
"football",
"cricket",
"shooting"
};
for(i = 0; i < 5; i++)
{
printf("String = %10s", sports[i] );
printf("\tAddress of string literal = %u\n", sports[i]);
}
// signal to operating system program ran fine
return 0;
}
Expected Output:
String = golf Address of string literal = 4206592
String = hockey Address of string literal = 4206597
String = football Address of string literal = 4206604
String = cricket Address of string literal = 4206613
String = shooting Address of string literal = 4206621
When we are using a two-dimensional array of characters we are at liberty to either initialize the strings where we are declaring the array, or receive the strings using scanf( ) function. However, when we are using an array of pointers to strings we can initialize the strings at the place where we are declaring the array, but we cannot receive the strings from keyboard using scanf( ).
main( )
{
char *names[6] ;
int i ;
for ( i = 0 ; i <= 5 ; i++ )
{
printf ( "\nEnter name " ) ;
scanf ( "%s", names[i] ) ;
}
}
The program doesn’t work because; when we are declaring the array it is containing garbage values. And it would be definitely wrong to send these garbage values to scanf( ) as the addresses where it should keep the strings received from the keyboard.
SOLUTION
If we are bent upon receiving the strings from keyboard using scanf( ) and then storing their addresses in an array of pointers to strings we can do it in a slightly round about manner.
#include "alloc.h"
main( )
{
char *names[6] ;
char n[50] ;
int len, i ;
char *p ;
for ( i = 0 ; i <= 5 ; i++ )
{
printf ( "\nEnter name " ) ;
scanf ( "%s", n ) ;
len = strlen ( n ) ;
p = malloc ( len + 1 ) ;
strcpy ( p, n ) ;
names[i] = p ;
}
for ( i = 0 ; i <= 5 ; i++ )
printf ( "\n%s", names[i] ) ;
}
But why did we not use array to allocate memory?
This is because with arrays we have to commit to the size of the array at the time of writing the program. Moreover, there is no way to increase or decrease the array size during execution of the program.
In other words, when we use arrays static memory allocation takes place.
A structure can be considered as a template used for defining a collection of variables under a single name. Structures help programmers to group elements of different data types into a single logical unit (Unlike arrays which permit a programmer to group only elements of same data type).
Why Use Structures
• Ordinary variables can hold one piece of information
• arrays can hold a number of pieces of information of the same data type.
For example, suppose you want to store data about a book. You might want to store its name (a string), its price (a float) and number of pages in it (an int).
If data about say 3 such books is to be stored, then we can follow two approaches:
- Construct individual arrays, one for storing names, another for storing prices and still another for storing number of pages.
Use a structure variable.
Suppose we want to create a employee database. Then, we can define a structure
called employee with three elements id, name and salary. The syntax of this structure is as
follows:
struct employee
{ int id;
char name[50];
float salary;
};
Note:
- Struct keyword is used to declare structure.
- Members of structure are enclosed within opening and closing braces.
- Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined or maintained in separate header file.
- Declaration of Structure reserves no space.
- It is nothing but the “ Template / Map / Shape ” of the structure .
- Memory is created , very first time when the variable is created / Instance is created.
Structure variable declaration:
We can declare the variable of structure in two ways
- Declare the structure inside main function
- Declare the structure outside the main function.
1. Declare the structure inside main function
Following example show you, how structure variable is declared inside main function
struct employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct employee e1, e2;
return 0;
}
In this example the variable of structure employee is created inside main function that e1 ,e2.
2. Declare the structure outside main function
Following example show you, how structure variable is declared outside the main function
struct employee
{
int id;
char name[50];
float salary;
}e1,e2;
- Memory allocation for structure
Memory is allocated to the structure only when we create the variable of structure.
Consider following example
- Structure Initialization
1. When we declare a structure, memory is not allocated for un-initialized variable.
2. Let us discuss very familiar example of structure student , we can initialize structure variable
in different ways –
Way 1 : Declare and Initialize
struct student
{
char name[20];
int roll;
float marks;
}std1 = { "Poonam",89,78.3 };
In the above code snippet, we have seen that structure is declared and as soon as after declaration we
have initialized the structure variable.
std1 = { "Poonam",89,78.3 }
This is the code for initializing structure variable in C programming
Way 2 : Declaring and Initializing Multiple Variables
struct student
{
char name[20];
int roll;
float marks;
}
std1 = {"Poonam" ,67, 78.3};
std2 = {"Vishal",62, 71.3};
In this example, we have declared two structure variables in above code. After declaration of variable we have initialized two variable.
std1 = {"Poonam" ,67, 78.3};
std2 = {"Vishal",62, 71.3};
Way 3 : Initializing Single member
struct student
{
int mark1;
int mark2;
int mark3;
} sub1={67};
Though there are three members of structure,only one is initialized , Then remaining two membersare initialized with Zero
Data Type Default value if not initialized
integer 0
float 0.00
char NULL
Way 4 : Initializing inside main
struct student
{
int mark1;
int mark2;
int mark3;
};
void main()
{
struct student s1 = {89,54,65};
- - - - --
- - - - --
- - - - --
};
When we declare a structure then memory won’t be allocated for the structure. i.e only writing below
declaration statement will never allocate memory
struct student
{
int mark1;
int mark2;
int mark3;
};
We need to initialize structure variable to allocate some memory to the structure.
struct student s1 = {89,54,65};
- Array elements are accessed using the Subscript variable , Similarly Structure members are accessed using dot [.] operator.
- (.) is called as “Structure member Operator”.
- Use this Operator in between “Structure variable” & “member name”
struct employee
{
int id;
char name[50];
float salary;
} ;
void main()
{
struct employee e= { 1, “ABC”, 50000 };
printf(“%d”, e. id);
printf(“%s”, e. name);
printf(“%f”, e. salary);
}
#include<stdio.h>
struct book
{
char title[5];
int year;
double price;
};
int main()
{
struct book b1 = {"Book1", 1988, 4.51};
printf("Address of title = %u\n", b1.title);
printf("Address of year = %u\n", &b1.year);
printf("Address of price = %u\n", &b1.price);
printf("Size of b1 = %d\n", sizeof(b1));
// signal to operating system program ran fine
return 0;
}
Expected Output:
1 2 3 4 | Address of title = 2686728 Address of year = 2686736 Address of price = 2686744 Size of b1 = 24
|
Syntax for declaring structure array
struct struct-name
{
datatype var1;
datatype var2;
- - - - - - - - - -
- - - - - - - - - -
datatype varN;
};
struct struct-name obj [ size ];
Example for declaring structure array
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
int i;
struct Employee Emp[ 3 ]; //Statement 1
for(i=0;i<3;i++)
{
printf("\nEnter details of %d Employee",i+1);
printf("\n\tEnter Employee Id : ");
scanf("%d",&Emp[i].Id);
printf("\n\tEnter Employee Name : ");
scanf("%s",&Emp[i].Name);
printf("\n\tEnter Employee Age : ");
scanf("%d",&Emp[i].Age);
printf("\n\tEnter Employee Salary : ");
scanf("%ld",&Emp[i].Salary);
}
printf("\nDetails of Employees");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);
}
Output :
Enter details of 1 Employee
Enter Employee Id : 101
Enter Employee Name : Suresh
Enter Employee Age : 29
Enter Employee Salary : 45000
Enter details of 2 Employee
Enter Employee Id : 102
Enter Employee Name : Mukesh
Enter Employee Age : 31
Enter Employee Salary : 51000
Enter details of 3 Employee
Enter Employee Id : 103
Enter Employee Name : Ramesh
Enter Employee Age : 28
Enter Employee Salary : 47000
Details of Employees
101 Suresh 29 45000
102 Mukesh 31 51000
103 Ramesh 28 47000
In the above example, we are getting and displaying the data of 3 employee using
array of object. Statement 1 is creating an array of Employee Emp to store the records
of 3 employees.
Array within Structure
As we know, structure is collection of different data type. Like normal data type, It can
also store an array as well.
Syntax for array within structure
struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
- - - - - - - - - -
- - - - - - - - - -
datatype varN;
};
struct struct-name obj;
Example for array within structure
struct Student
{
int Roll;
char Name[25];
int Marks[3]; //Statement 1 : array of marks
int Total;
float Avg;
};
void main()
{
int i;
struct Student S;
printf("\n\nEnter Student Roll : ");
scanf("%d",&S.Roll);
printf("\n\nEnter Student Name : ");
scanf("%s",&S.Name);
S.Total = 0;
for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&S.Marks[i]);
S.Total = S.Total + S.Marks[i];
}
S.Avg = S.Total / 3;
printf("\nRoll : %d",S.Roll);
printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
printf("\nAverage : %f",S.Avg);
}
Output :
Enter Student Roll : 10
Enter Student Name : Kumar
Enter Marks 1 : 78
Enter Marks 2 : 89
Enter Marks 3 : 56
Roll : 10
Name : Kumar
Total : 223
Average : 74.00000
In the above example, we have created an array Marks[ ] inside structure representing
3 marks of a single student. Marks[ ] is now a member of structure student and to
access Marks[ ] we have used dot operator(.) along with object S.
References:
The C Programming Language. 2nd Edition Book by Brian Kernighan and Dennis Ritchie
C Programming: A Modern Approach Book by Kim N. King
C Programming Absolute Beginner’s Guide book by S.D Perry