UNIT3
UNIT3
UNIT III
DERIVED DATA TYPES IN C
Arrays
An array is a group of data items of same data type that share a common name.
Different types of arrays
1. One dimensional array
2. Two dimensional array
3. Multi dimensional array
1
UNIT3 MSR
data-type array-name[rowsize][columnsize];
For e.g. int a[2][2];
In this, array a is having two rows and columns. Matrix is an example for two
dimensional array. The elements of the array a are a[1][1], a[1][2],a[2][1] and
a[2][2].
{
for(j=0; j<m; j++)
{
printf(“%d”,a[i][j]\t;
}
printf(“\n”);
}
Programs using one dimensional and two dimensional arrays …..
4
UNIT3 MSR
We can also initialize a string without specifying the size. The size of the array is
automatically determined based on the length of the string.
char city[ ]=”NEWYORK”;
A string with length zero is called Null string, Null string refers to an empty
string (“”).
5
UNIT3 MSR
6
UNIT3 MSR
content, without any warning. This may cause the program to unexpectedly crash
in the middle of execution.
Writing strings to screen
To output a string, we can use two standard output functions printf() and puts().
The printf() function can be used along with %s as format specifier.
Example:
main()
{ char greet[25]=”Have a Nice Day”
printf(:%s”, greet);
}
The above code will display “Have a Nice Day” on the standard output device.
The puts() function can also be used to display the string on the standard
output device.
Syntax: puts(expression);
Example:
main()
{ char greet25]=”Have a Nice Day”
puts( greet);
}
2. strlwr(): This function is used to convert any uppercase letter in the given
string to its equivalent lowercase.
Syntax: strlwr(string);
Example:
#include<stdio.h> This program will produce
the output hello.
#include<string.h>
main()
{ char msg[10]= “Hello”;
strlwr(msg);
printf(“%s”,msg);
}
4. strrev(): This function is used to reverse a string. The converted string will be
stored in the source string itself.
Syntax: strupr(string);
Example:
#include<stdio.h>
#include<string.h>
This program will produce
main() the output olleH.
{ charmsg[10]= “Hello”;
strrev(msg);
printf(“%s”,msg);
8
UNIT3 MSR
9
UNIT3 MSR
will return a negative value (-32) because the ASCII value of ‘I’ is less than the
ASCII value of “i”, which is the difference between the ASCII values of ‘I’ and
‘i’.
8. strncmp(): This function compares at most n characters of str1 with str2 and
return a negative, zero or positive value depending on whether str1< str2, str1
= str2, str1>str2.
10. strncat(): This function appends first ‘n’ characters of source string to the
end of destination string.
Syntax: strncat(str1,str2,n);
Example This function produces
#include<stdio.h> the output Hello Wor.
#include<string.h>
main()
{ char str1[]= “Hello ”, str2[]= “World”;
strcat(str1,str2,3);
printf(“%s”,str1);
}
11. strstr(): This functions returns the first occurrence of string str2 in str1,
Null if not present.
atoi():
This function converts a string of digits into their integer values. The syntax is
x=atoi(string)
Where x is an integer variable and string is a string constant or string variable
which contains sequence of digits. atoi() function present under the header file
stdlib.h.
Ex: #include<stdio.h>
main()
{ int year;
char number[ ]=”2008”
year=atoi(number);
printf(“%d”, year);
}
Here, the function atoi() converts the string “2008” into numeric equivalent 2008
and assign it to the integer variable year.
POINTERS
x 10 5000
12
UNIT3 MSR
p 5000 5048
Consider int x=10; This will store the value of x in a memory location that is
having a unique memory address. Assume that the address is 5000 for x. Since
memory addresses are numbers, they can be assigned to some variables called as
pointer. Since pointer is a variable, its value is also stored in the memory in another
location. Suppose p is a pointer pointing to 5000, we can access the value of x using p.
Since pointer variables contain addresses that belong to a separate data type, they must
be declared as pointers. The declaration of pointer variable takes the following form:
Data type *pointer-name;
This tells the compiler three things about the pointer-name.
1. * tells that the variable pointer-name is a pointer variable.
2. pointer-name needs a memory location.
3. pointer-name points to a variable of type data type.
13
UNIT3 MSR
which causes p to point to x. Now p contains the address of x. This is known as pointer
initialization. Before a pointer is initialized, it should not be used.
For e.g. int x;
int *p;
p=&x;
This can also be written as int x,*p=&x;
But int *p=&x,x; is invalid because x is not declared first.
Pointer expressions
Like other variables, pointer variables can be used in expressions. For e.g. if p1
and p2 are properly declared and initialized pointers then the following
statements are valid.
X= *p1 * *p2;
sum=sum+ *p1;
*p2=*p2+10;
Z=*p1/ *p2; Note that there is a space between / and * otherwise /* is considered
as the beginning of a comment.
C allows us to add integers to or subtract integers from pointers as well as to
subtract one pointer from another. p1+4,p2-2 and p1-p2 are all allowed.
We may also use short-hand operators with pointers. For e.g.
p1++;
--p2;
sum+=*p2;
Pointers can also be compared using relational operators. E.g.
p1>p2, p1==p2,p1!=p2 are allowed.
14
UNIT3 MSR
15
UNIT3 MSR
p+1=&x[1] (1002)
p+2=&x[2] (1004)
p+3=&x[3] (1006)
p+4=&x[4] (1008)
**************************
17