UNIT-III_PPS
UNIT-III_PPS
Pointers: Idea of pointers, Defining pointers, usage of self-referential structures in linked list
(noimplementation), passing pointers to functions, idea of call by reference.
Strings: Introduction to strings, handling strings as array of characters, basic string functions
available in C (strlen( ), strcat( ), strcpy( ), strstr( ) etc.), arrays of strings
Structures: Defining structures, initializing structures, unions, Array of structures, Pointers to Arrays
and Structures, Use of Pointers in self-referential structures, Enumerationdata type.
C Strings
So scanf function read entire line you are supposed to mention like
scanf(“ %[^\n]s “, name); this statement read multiple words like complete line . NEW YORK
String Library functions
The predefined functions which are designed to handle strings are available in
the library string.h. They are −
strlen ()
strcmp ()
strcpy ()
strncmp ()
strncpy ()
strrev ()
strcat ()
strstr ()
strncat ()
Syntax
int strlen (string name)
Example
#include <string.h>
main (){
char a[30] = “Hello”;
int l;
l = strlen (a);
printf (“length of the string = %d”, l);
getch ();
}
Output
length of the string = 5
The strcpy () function
It is for copying source string into destination string.
The length of the destination string >= source string.
Syntax
strcpy (Destination string , Source String);
For example,
1) char a[50];
strcpy (“Hello”,a);
o/p: error
2) char a[50];
strcpy ( a,”hello”);
o/p: a= “Hello”
Example
#include <string.h>
main (){
char a[50], b[50];
printf ("enter a source string");
scanf("%s", a);
printf("enter destination string");
scanf("%s",b);
strcpy ( b,a);
printf ("copied string = %s",b);
getch ();
}
Output
Enter a source string : Hello
Copied string = Hello
The strncpy () function
It copy’s ‘n’ characters of source string into destination string.
The length of the destination string must >= that of the source string.
Syntax
strncpy (Destination string , Source String , n);
Example
#include<string.h>
main (){
char a[50], b[50];
printf ("enter a string");
gets (a);
gets(b);
strncpy (b,a,3);// copy first 3 char from a string
b[3] = '\0';
printf ("copied string = %s",b);
getch ();
}
Output
Enter a string : Hello
Copied string = Hel
It is also used for extracting substrings;
Syntax
strcat (Destination String , Source string);
Example
#include <string.h>
main(){
char a[50] = "Hello";
char b[20] = "Good Morning";
clrscr ();
strcat (a,b);
printf("concatenated string = %s", a);
getch ();
}
Output
Concatenated string = Hello Good Morning
The strncat () function
This is used for combining or concatenating n characters of one string into another.
The length of the destination string must be greater than the source string
The resultant concatenated string will be in the destination string.
Syntax
strncat (Destination String , Source string , n);
Example
#include <string.h>
main (){
char a [30] = "Hello";
char b [20] = "Good Morning";
clrscr ();
strncat (a,b,4);
a [9] = '\0';
printf("concatenated string = %s", a);
getch ();
}
Output
Concatenated string = Hello Good.
Syntax
int strcmp (string1 , string2);
//If the difference is equal to zero, then string1 = string2
//If the difference is positive, then string1 > string2
//If the difference is negative, then string1 < string2
Example
#include<stdio.h>
#include<string.h>
int main (){
char a[50], b [50];
int d;
printf ("Enter 2 strings:");
scanf ("%s %s", a,b);
d = strcmp(a,b);
if (d==0){
printf("%s is (alphabetically) equal to %s",
a,b);
}else if (d>0){
printf("%s is (alphabetically) greater than
%s",a,b);
}else if (d<0){
printf("%s is (alphabetically) less than %s",
a,b);
}
}
Output
Enter 2 strings:apple ball
apple is (alphabetically) less than ball
Syntax
strncmp ( string1 , string2,2)
strncmp (a,b,4);
Output − Both strings are equal
Syntax
strrev (string)
Example
#include<stdio.h>
main (){
char a[50] ;
clrscr();
printf ("enter a string");
gets (a);
strrev (a);
printf("reversed string = %s",a)
getch ();
}
Output
enter a string Hello
reversed string = olleH
Syntax
strstr(mainsring , substring);
Example
#include<stdio.h>
void main(){
char a[30],b[30];
char *found;
printf("Enter a string:\t");
gets(a);
printf("Enter the string to be searched for:\t");
gets(b);
found=strstr(a,b);
if(found)
printf("%s is found in %s in %d
position",b,a,found-a);
else
printf("-1 since the string is not found");
getch();
}
Output
Enter a string: how are you
Enter the string to be searched for: you
you is found in 8 position
arrays of strings
arrays of strings
arrays of strings
arrays of strings
Program to read and display array of strings
Structures
Ex: struct book {
char name[30];
int pages;
float price;
} b1,b2,b3;
Define a structure type book, that would contain book name, author, pages and price. Write a
program to read this data using member operator (‘.’) and display the same.
Ans
union
A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same memory
location for multiple-purpose.
Defining a Union
To define a union, you must use the union statement in the same way as you did while defining
a structure. The union statement defines a new data type with more than one member for your
program. The format of the union statement is as follows −
The union tag is optional and each member definition is a normal variable definition, such as
int i; or float f; or any other valid variable definition. At the end of the union's definition, before
the final semicolon, you can specify one or more union variables but it is optional. Here is the
way you would define a union type named Data having three members i, f, and str −
union Data {
int i;
float f;
char str[20];
} data;
Now, a variable of Data type can store an integer, a floating-point number, or a string of
characters. It means a single variable, i.e., same memory location, can be used to store multiple
types of data. You can use any built-in or user defined data types inside a union based on your
requirement.
The memory occupied by a union will be large enough to hold the largest member of the union.
For example, in the above example, Data type will occupy 20 bytes of memory space because
this is the maximum space which can be occupied by a character string. The following example
displays the total memory size occupied by the above union −
To access any member of a union, we use the member access operator (.). The member access
operator is coded as a period between the union variable name and the union member that we
wish to access. You would use the keyword union to define variables of union type. The
following example shows how to use unions in a program −
Array of structures
The most common use of structure in C programming is an array of
structures.
To declare an array of structure, first the structure must be defined and then
an array variable of that type should be defined.
For Example − struct book b[10]; //10 elements in an array of structures
of type ‘book’
Pointers with Arrays
Pointer to arrays
The pointer that points to whole array instead of only one element of the array is called pointer
to array.
Syntax:
data_type (*var_name)[size_of_array];
Example:
int (*p)[5];
Here p is pointer that can point to an array of 5 integers.Since subscript have higher
precedence than indirection, it is necessary to enclose the indirection operator and pointer name
inside parentheses. Here the type of p is ‘pointer to an array of 5 integers’.
Note : The pointer that points to the 0th element of array and the pointer that points to the whole
array are totally different. The following program shows this:
p1 = 5000, p2 = 5000
p1 = 5002, p2 = 5010
p1: is pointer to 0th element of the array a, while p2 is a pointer that points to the whole array a.
The base type of p1 is int while base type of p2 is ‘an array of 5 integers’.
We know that the pointer arithmetic is performed relative to the base size, so if we write
p2++, then the pointer p2 will be shifted forward by 10 bytes.
for(i=0;i<=3-1;i++)
{
for(j=0;j<=3-1;j++)
{
printf(“\t %d “, *(*(p+i)+j));
}
}
}
Out put:
1 2 3 4 5 6 7 8 9
Pointer to Structure:
output
What is Enumerated data type? Explain with example
An enumeration is a user-defined data type consists of integral constants and
each integral constant is give a name. Keyword enum is used to defined
enumerated data type.
enum suit
{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
Declaration of enumerated variable
Above code defines the type of the data but, no any variable is created. Variable
of type enum can be created as:
enum Boolean
{
false;
true;
};