Pointers
Consider the declaration,
int i = 3
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer
value
(b) Associate the name i with this memory
location
(c) Store the value 3 at this location.
• The location number 65524 is not a number to
be relied upon, because some other time the
computer may choose a different location for
storing the value 3.
• The important point is, i’s address in memory
is a number.
main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
}
The output of the above program would be:
Address of i = 65524 Value of i = 3
• The other pointer operator available in C is
‘*’, called ‘value at address’ operator.
• It gives the value stored at a particular
address.
main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3
Note that printing the value of *( &i ) is same
as printing the value of i
The expression &i gives the address of the
variable i
This address can be collected in a variable, by
saying,
j = &i ;
j is a variable that contains the address of
other variable (i in this case)
Since j is a variable the compiler must provide
it space in the memory.
• i’s value is 3 and j’s value is i’s address.
• But we can’t use j in a program without
declaring it.
• And since j is a variable that contains the
address of i, it is declared as, int *j
• This declaration tells the compiler that j will
be used to store the address of an integer
value.
• In other words j points to an integer.
• Let us go by the meaning of *. It stands for
‘value at address’.
• Thus, int *j would mean, the value at the
address contained in j is an int.
main( )
{
int i = 3 ;
int *j ; j = &i ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}
• Address of i = 65524
• Address of i = 65524
• Address of j = 65522
• Value of j = 65524
• Value of i = 3
• Value of i = 3
• Value of i = 3
• int *alpha ;
• char *ch ;
• float *s ;
pointers are variables that contain addresses,
and since addresses are always whole
numbers, pointers would always contain
whole numbers.
• float *s
s is going to contain the address of a
floating-point value
• char *ch means that ch is going to contain the
address of a char value
main( )
{
int i = 3, *j, **k ;
j = &i ;
k = &j ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of i = %u", *k ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nAddress of j = %u", k ) ;
printf ( "\nAddress of k = %u", &k ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of k = %u", k ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", * ( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}
• Address of i = 65524
• Address of i = 65524
• Address of i = 65524
• Address of j = 65522
• Address of j = 65522
• Address of k = 65520
• Value of j = 65524
• Value of k = 65522
• Value of i = 3
• Value of i = 3
• Value of i = 3
• Value of i = 3
• int i, *j, **k ;
• Here, i is an ordinary int
• j is a pointer to an int (often called an integer
pointer), whereas k is a pointer to an integer
pointer.
User Defined Data Type
Structures in C
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
• But quite often we deal with entities that are
collection of dissimilar data types. 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)
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
• But quite often we deal with entities that are
collection of dissimilar data types. 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)
Structures
• A structure is a collection of variables of
different data types under a single name
• The variables are called members of the
Structure
• Structure is also called a user-defined data
type
Declaring a Structure
Syntax Example
Keyword
struct [structure name] { struct book
member definition; {
member definition; char name[30];
... Member float price;
member definition; s of int pages;
structur
}; e. };
In our example program, the above statement
declares the structure type.
Structure variables
• Once the new structure data type has been
defined, one or more variables can be
declared to be of that type. For example, the
variables b1, b2, b3 can be declared to be of
the type struct book, as:
struct book b1, b2, b3 ;
Structure variables
• If we so desire, we can combine the declaration of the
structure type and the structure variables in one statement
• For example,
struct book struct book
{ {
char name[30] ; char name[30] ;
float price ; IS SAME AS float price ;
int pages ; int pages ;
}; } b1, b2, b3;
struct book b1, b2, b3 ;
Initialization
Structure variables can also be initialized where they are
declared
struct book
{
char name[30] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
Accessing Structure Elements
• Structure elements can be accessed through a
structure variable using a dot (.) operator
#include <stdio.h>
struct book
{
char name[30] ;
float price ;
int pages ;
};
int main()
{
struct book b1 = { “Basic”, 130.00, 550 } ;
printf("\n %s %f %d", b1.name, b1.price, b1.pages);
return 0;
} Output
Basic 130.00 550
Taking input from user
#include <stdio.h>
struct book {
char name[30];
float price;
int pages;
};
int main()
{
struct book b1;
printf("\nEnter name of book\n");
scanf("%[^\n]%*c",b1.name);
printf("\nEnter price of book\n");
scanf("%f%*c",&b1.price);
printf("\nEnter number of pages:\n");
scanf("%d%*c",&b1.pages);
printf("\n%s\t%f\t%d", b1.name, b1.price, b1.pages);
}
Taking input from user(contd..)
#include <stdio.h>
struct book {
Output:
char name[30];
float price;
int pages;
};
int main()
{
struct book b1;
printf("\nEnter name of book\n");
scanf("%[^\n]%*c",b1.name);
printf("\nEnter price of book\n");
scanf("%f%*c",&b1.price);
printf("\nEnter number of pages:\n");
scanf("%d%*c",&b1.pages);
printf("\n%s\t%f\t%d", b1.name, b1.price, b1.pages);
}
Array of Structures
• To store data of 100 books, we would be required to use 100
different structure variables from b1 to b100, which is
definitely not very convenient
• A better approach would be to use an array of structures
• It is declared as(based on above declaration of struct book):
struct book b[10];
• Each of the members may be accessed as a normal array with
index. For example:
b[1].name
name price pages
b[2].name
b[1]
b[2]
……
……
b[10]
Example - To create a record of books
#include <stdio.h>
struct book
{
char name[30] ;
float price ;
int pages ;
};
int main()
{
struct book b[20];
int n, i;
printf("Enter the total number of books"); //Ask user
to enter total number of books
scanf("%d%*c", &n);
Example - To create a record of
for (i=1; i<=n; i++)
books(contd..) //to input records
{
printf("\nEnter name of book\n");
scanf("%[^\n]%*c",b[i].name);
printf("\nEnter price of book\n");
scanf("%f%*c",&b[i].price);
printf("\nEnter number of pages:\n");
scanf("%d%*c",&b[i].pages);
}
printf("\nEntered records are:"); //to print records
for (i=1; i<=n; i++)
{
printf("\n%s\t%f\t%d", b[i].name, b[i].price, b[i].pages);
}
} //End of main
Output