Every variable in C language has a name and a value associated with it. When a variable is declared, a specific block of memory within the computer is allocated to hold the value of that variable.
INTRODUCTION
TO POINTERS
Every
variable in C language has a name and a value associated with it. When a
variable is declared, a specific block of memory within the computer is allocated
to hold the value of that variable. The size of the allocated block depends on
the type of the data. Let us write a program to find the size of the various
data types on your system. (Note the size of integer may vary from one system
to another. In 32 bit systems, integer variable is allocated 4 bytes while on
16 bit systems it is allocated 2 bytes.)
1. Write a program to
find the size of various data types on your system.
#include <stdio.h>
int main()
{
printf("\n The size of short
integer is : %d", sizeof (short int));
printf("\n The size of
unsigned integer is: %d", sizeof (unsigned int));
printf("\n The size of signed
integer is: %d", sizeof (signed int));
printf("\n The size of integer
is: %d", sizeof (int));
printf("\n The size of long
integer is: %d", sizeof (long int));
printf("\n The size of
character is: %d", sizeof (char));
printf("\n The size of
unsigned character is: %d", sizeof (unsigned char));
printf("\n The size of signed
character is: %d", sizeof (signed char));
printf("\n The size of
floating point number is: %d", sizeof(float));
printf("\n The size of double
number is: %d", sizeof (double));
return 0;
}
Output
The size of short integer is: 2
The size of unsigned integer is: 2
The size of signed integer is: 2
The size of integer is: 2
The size of long integer is: 4
The size of character is: 1
The size of unsigned character is:
1
The size of signed character is: 1
The size of floating point number
is: 4
The size of double number is: 8
Consider
the statement below:
int x = 10;
When
this statement executes, the compiler sets aside 2 bytes of memory to hold the
value 10. It also sets up a symbol table in which it adds the symbol x and the
relative address in memory where those 2 bytes are set aside.
Thus,
every variable in C has a value and a memory location (commonly known as
address) associated with it. Some texts use the term rvalue and Ivalue for the
value and the address of the variable, respectively.
The
rvalue appears on the right side of the assignment statement (10 in the above
statement) and cannot be used on the left side of the assignment statement.
Therefore, writing 10 = k; is illegal. If we write
int
x, y;
x
= 10;
y
= x;
then
in this code we have two integer variables x and y. Compiler reserves memory
for integer variable x and stores rvalue
10 in it. When we say y x, then x is interpreted as its rvalue (since it is on
the right hand side of the assignment operator '='). Here x refers to the value
stored at the memory location set aside for x, in this case 10. After this
statement is executed, the rvalue of y is also 10.
You
must be wondering why we are discussing addresses and Ivalues? Actually
pointers are nothing but memory addresses. A pointer is a variable that
contains the memory location of another variable. Therefore, a pointer is a
variable that represents the location of a data item, such as a variable or an
array element. Pointers are frequently used in C language as they have a number
of useful applications. These applications include:
•
to pass information back and forth between a function and its reference point
•
enable the programmers to return multiple data items from a function via
function arguments
•
provide an alternate way to access individual elements of the array
•
to pass arrays and strings as function arguments
•
enable references to functions. So with pointers, the programmer can even pass
functions as argument to another function
•
to create complex data structures such as trees, linked lists, linked stacks,
linked queues, and graphs 5.200velle
•
for dynamic memory allocation of a variable
Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Introduction to Pointers
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation