Introduction to C Programming Pointers & Structures
Pointers: Understanding Memory Addresses, Address Operator (&), Pointer: Declaring
a Pointer, Initializing Pointers. Void Pointer, Null Pointer, Use of Pointers, Arrays and
Pointers, Pointers and Strings,
Structures: Declaring Structures and Structure Variables, Accessing the Members of a
Structure, Initialization of Structures, Typedef and its Use in Structure Declarations,
Nesting of Structures. Arrays of Structures, Initializing Arrays of Structures, Arrays
within the Structures. Structures and Pointers, Structures and Functions.
Introduction to Pointers:
A pointer is a variable that stores the memory address of another variable.
Pointers allow direct memory access and manipulation.
They are used for dynamic memory allocation, passing large data structures
efficiently, and working with arrays and strings.
Dynamic Memory Allocation: Pointers are crucial for allocating memory
dynamically using malloc, calloc, or realloc.
Flexibility: Pointers allow the implementation of complex data structures like linked
lists, stacks, and trees.
Understanding Memory Addresses:
All computers have primary memory, also known as RAM or Random Access
Memory.
Example: A computer may have 16, 32, 64, 128, 256, or 512 MB of RAM installed.
RAM holds the programs that the computer is currently running along with the data
they are currently manipulating (their variables and data structures).
All the variables used in a program (and indeed, the program itself) reside in the
memory when the program is executed.
The organization of the memory is rather straightforward.
It is a sequence of a large number of memory locations (cells), each of which has an
address.
Each memory location is capable of storing a small number (0 to 256), which is
known as a byte.
Dept. of CSE 1
Introduction to C Programming Pointers & Structures
Memory layout summary:
Address of Operator (&):
Readers might have noticed that when we call certain functions in C the & sign is
used.
Example:
scanf(“%d”, &n);
Takes the input from the terminal and stores it in integer format in the variable
named n.
Dept. of CSE 2
Introduction to C Programming Pointers & Structures
The & sign indicates the address in memory of the integer n, which must be
previously declared using int n; where the function stores the input data.
A segmentation fault in C occurs when a program tries to access a memory location
that it is not allowed to access.
Example:
int i = 3;
This declaration tells the C compiler to
reserve space in memory to hold the integer value
associate the name i with this memory location
store the value 3 at this location
i’s location in the memory may be logically represented with the memory map shown
in Fig. Variable Name
Value
Memory Address
printf(“\nAddress of i = %u”, &i);
The output will be: 2147478276.
NOTE:
After declaring a variable, where the variable is to be located is decided by the
compiler and the operating system at run time.
After declaring a variable, if an attempt is made to output its value without
assigning a value, the result is a garbage value.
Dept. of CSE 3
Introduction to C Programming Pointers & Structures
Pointer:
A pointer variable holds the memory address of another variable.
Put another way, the pointer does not hold a value in the traditional sense; instead, it
holds the address of another variable.
They are called pointers for the simple reason that by storing an address, they ‘point’
to a particular point in memory. a pointer points to that variable by holding a copy of
its address.
Declaring a Pointer:
A pointer variable is declared by preceding its name with an asterisk.
Syntax:
datatype * pointer_variable;
Example:
char *ptr;
The above declaration should be evaluated as: ptr is a pointer to char type
data.
Meaning of some pointers type variable declarations
Dept. of CSE 4
Introduction to C Programming Pointers & Structures
Consider the following program.
#include<stdio.h>
int main()
{
int *p;
float *q;
double *r;
printf(“\n the size of integer pointer is %d”, sizeof(p));
printf(“\n the size of floating pointer is %d”, sizeof(q));
printf(“\n the size of double pointer is %d”, sizeof(r));
printf(“\n the size of char pointer is %d”, sizeof(char
*));
return 0;
}
OUTPUT:
the size of integer pointer is 4
the size of floating pointer is 4
the size of double pointer is 4
the size of char pointer is 4
NOTE:
Pointers have data types, but the size of a pointer variable is always four bytes (in
a 32-bit machine) whatever the data type used in declaring it.
Initializing Pointers:
A pointer should be initialized with another variable’s memory address, with 0, or
with the keyword NULL prior to its use; otherwise, the result may be a compiler error
or a run-time error.
A pointer is bound to a specific data type (except pointer to void). A pointer to an int
cannot hold the address of a character variable in which case a compiler error would
result.
Dept. of CSE 5
Introduction to C Programming Pointers & Structures
Example:
#include<stdio.h>
int main()
{
int i = 5;
int *ptr = &i;
printf("\nThe address of i using &num is %p", &i);
printf("\nThe address of i using Pointer is %p", ptr);
return 0;
}
Pointer pointing to an integer variable
Three pointers pointing to the same variable
The variable i can be accessed through i, *p, *q, and *r.
There is no limit on the number of pointers that can hold and therefore point to the
same address.
Dept. of CSE 6
Introduction to C Programming Pointers & Structures
Printing pointer value:
A pointer variable contains a memory address that points to another variable.
To print the memory address stored in pointers and non-pointer variables using the
%p conversion specifier and to learn the use of the %p conversion specifier.
Example:
#include<stdio.h>
int main()
{
int a=10, *p;
p = &a;
printf("\n p = %p", p); // address in hexadecimal format
printf("\n p = %u", p); // address in decimal format
printf("\n the value at p is = %d", *p);
//using dereferencing operator (value at operator).
}
OUTPUT:
p = 0x7ffd516e0dd4
p = 1366166996
the value at p is = 10.
NOTE:
Address of operator (&): It is used as a variable prefix and can be translated as
‘address of’. Thus, &variable can be read as .address of variable.
Dereference operator (*): It can be translated by .value pointed by or ‘value at
address’. *ptr can be read as ‘value pointed by ptr’. It indicates that what has to
be evaluated is the content pointed by the expression considered as an address.
void Pointer
A void pointer is a special type of pointer.
It can point to any data type, from an integer value or a float to a string of characters.
Its sole limitation is that the pointed data cannot be referenced directly (the asterisk *
operator cannot be used on them) since its length is always undetermined.
Dept. of CSE 7
Introduction to C Programming Pointers & Structures
Therefore, type casting or assignment must be used to turn the void pointer to a
pointer of a concrete data type to which we can refer.
Example:
#include<stdio.h>
int main()
{
int a=5;
double b=3.1415;
void *vp;
vp=&a;
printf("\n a = %d", *((int *)vp));
vp=&b;
printf("\n a = %d", *((double *)vp));
return 0;
}
OUTPUT:
a = 5
a = 4202512
NULL pointer:
NULL is a constant that is defined in the standard library and is the equivalent of zero
for a pointer.
NULL is a value that is guaranteed not to point to any location in memory.
Example:
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s",p);
return 0;
}
Dept. of CSE 8
Introduction to C Programming Pointers & Structures
OUTPUT:
(null)
Use of Pointers
Call by address
Example:
In the above code, the addresses of x and y are passed to the function
swap().
The parameters of the swap() function, int *a and int *b are pointers to
integer type data.
These pointers receive the addresses of x and y respectively that are passed
in the call to swap(). Within the function swap(), a local variable temp is
declared.
Dept. of CSE 9
Introduction to C Programming Pointers & Structures
The pointer a is dereferenced, meaning that the value at the address held in
a is retrieved. This value is stored into temp.
Then the value at the address held in b is retrieved and assigned to the value
at the address held in a, thus exchanging values.
The final statement in the function completes the exchange of values.
Arrays and Pointers
Pointers and arrays are inseparably related, but they are not synonymous.
Array name is a pointer constant.
It cannot be used as lvalue, that, is array names cannot be used as variables on the left
of an assignment operator.
Both array and &array would give the base address of the array, but the only
difference is under ANSI/ISO Standard C, &array yields a pointer, of type pointer-to
array of-the data type to the entire array.
Dept. of CSE 10
Introduction to C Programming Pointers & Structures
Example:
As indirection operator * implies value at address, a[i] is equivalent to *(a+i).
Dept. of CSE 11
Introduction to C Programming Pointers & Structures
Differences between pointers and arrays:
STRUCTURES:
A structure is a collection of variables under a single name.
These variables can be of different types, and each has a name which is used to select
it from the structure.
Therefore, a structure is a convenient way of grouping together several pieces of
related information.
Declaring Structures and Structure Variables
A structure is declared by using the keyword struct followed by an optional
structure tag followed by the body of the structure.
The variables or members of the structure are declared
within the body.
Syntax:
Dept. of CSE 12
Introduction to C Programming Pointers & Structures
There are three different ways to declare and/or define a structure.
These are:
1. Variable structure
2. Tagged structure
3. Type-defined structure
A variable structure may be defined as follows.
Syntax:
struct
{
member_list
}variable_identifier;
Example:
A tagged structure may be defined as follows.
Syntax: Example:
Dept. of CSE 13
Introduction to C Programming Pointers & Structures
Example:
Accessing the Members of a Structure
The members of a structure can be accessed in three ways.
One of the ways consists of using the ‘.’, which is known as the ‘dot operator’.
Dept. of CSE 14
Introduction to C Programming Pointers & Structures
Syntax:
structure_variable . member_name;
The .(dot) operator selects a particular member from a structure.
It has the same precedence as () and [], which is higher than that of any unary or
binary operator. Like () and [], it associates from left to right.
Initialization of Structures:
A structure can be initialized in much the same way as any other data type. This
consists of assigning some constants to the members of the structure.
Structures that are not explicitly initialized by the programmer are, by default,
initialized by the system.
For integer and float data type members, the default value is zero. For char and
string type members, the default value is ‘\0’.
Syntax:
Dept. of CSE 15
Introduction to C Programming Pointers & Structures
Example:
Dept. of CSE 16