cccc
cccc
Advance Programming in C
Pointer:
The pointers perform the function of storing the addresses of other variables in the program.
These variables could be of any type- char, int, function, array, or other pointers.
The pointer sizes depend on their architecture. (For ex. the size of a pointer in the 32-bit architecture
is 2 bytes.)
Let us look at an example where we define a pointer storing an integer’s address in a program.
int x = 10;
int* p = &x;
Here, the variable p is of pointer type, and it is pointing towards the address of the x variable, which
is of the integer type.
Declaration of a Pointer
Just like the variables, we also have to declare the pointers in C before we use them in any program.
2
We can name these pointers anything that we like, as long as they abide by the naming rules of the C
language. Normally, the declaration of a pointer would take a form like this: data_type *
name_of_pointer_variable;
● The data_type refers to this pointer’s base type in the variable of C. It indicates which type of
variable is the pointer pointing to in the code.
● The asterisk ( * ) is used to declare a pointer. It is an indirection operator, and it is the same
asterisk that we use in multiplication.
We can declare pointers in the C language with the use of the asterisk symbol ( * ). It is also called
the indirection pointer, as we use it for dereferencing any pointer. Here is how we use it:
pointer = &variable;
As we can assess in the figure shown above, the pointer variable is storing the digit variable’s
3
address, named fff4. This digit variable’s value is equal to 100. But aaa3 is the pointer variable’s
address (variable r).
Here, we can print the r pointer variable’s value using the indirection pointer ( * ). We will look at an
example for the same, as explained in the figure mentioned above.
#include<stdio.h>
int main(){
int digit=100;
int *r;
r=&digit; // for storing the address of the digit variable in the program
printf(“The address of the variable r is equal to %x \n”,r); // pointer r contains the digit variable’s
address, and thus, printing the variable r gives us the address of the digit.
printf(“The value of the variable r is equal to %d \n”,*r); // We are using * to dereference the pointer,
and thus, printing *r, would give the value that is stored at that address which is contained by r.
return 0;
Use of Pointers in C
Here is a summary of how we use the following operators for the pointers in any program:
int *q [20] = &arr; // The q variable of the pointer type is pointing towards the integer array’s address
or the address of arr.
void(*q)(int) = &show; // The q pointer is pointing towards the function’s address in the program
struct str {
int x;
float y;
}ref;
void apoo()
int* ptr;
ptr = &var;
// Driver program
6
int main()
apoo();
return 0;
Types of Pointers
There are various types of pointers that we can use in the C language. Let us take a look at the most
important ones.
#include <stdio.h>
int main()
return 0;
The output obtained out of the program mentioned above will be:
data type, and we create it with the use of the keyword void. The void pointer is generally used for
the storage of any variable’s address. Let us take a look at a program that illustrates how we use the
void pointer in a C program:
#include <stdio.h>
int main()
return 0;
The output obtained out of the program mentioned above will be:
#include <stdio.h>
int main()
printf(“\n%d”,*w);
return 0;
}
8
The output obtained out of the program mentioned above will be a compile-time error.
Here are a few more types of pointers that you can find in the C programs:
● Near pointer
● Complex pointer
● Huge pointer
● Far pointer
● Dangling pointer
● Direct Access: In this case, we use the variable name in the program directly.
● Indirect Access: In this case, we use some pointers to the variables in the program.
Let us take a look at a program to understand this better. Consider the below program:
#include <stdio.h>
int variable = 1;
int *ptr;
ptr = &variable;
/*changing of the content of the variable through the ptr pointer in the program*/
*ptr=48;
return 0;
The compilation of this program devoid of errors would generate an output like this:
Cons of Pointers in C
10
Applications of Pointers in C
There are various uses of the pointers in C language. Let us look at some of them:
1. Dynamic allocation of memory: We can allocate the memory dynamically in C language when we
use the calloc() and malloc() functions. The pointers are primarily used in such cases.
2. Structures, Functions, and Arrays: We generally use the pointers in the C language in the cases of
structures, functions, and arrays. Here, the pointers help in reducing the code and improving the
program’s performance.
#include<stdio.h>
int main(){
int digit=100;
printf(“The value of the digit is equal to %d and the address of the digit is equal to %u”,digit,&digit);
return 0;
The value of the digit is equal to 100 and the address of the digit is equal to fff4
11
Pointer Arithmetics in C:
Pointer Arithmetic is the set of valid arithmetic operations that can be performed
on pointers.
These operations are:
1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
5. Comparison of pointers
1. Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a pointer is
incremented, it actually increments by the number equal to the size of the data
type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is incremented, then it will
increment by 4(size of an int), and the new address will point to 1004. While if a
float type pointer is incremented then it will increment by 4(size of a float) and
the new address will be 1004.
For Example:
If an integer pointer that stores address 1000 is decremented, then it will
decrement by 4(size of an int), and the new address will point to 996. While if a
float type pointer is decremented then it will decrement by 4(size of a float) and
the new address will be 996.
12
Example:
#include <stdio.h>
int main()
int a = 22;
int *p = &a;
p++;
p--;
float b = 22.22;
float *q = &b;
q++;
q--;
char c = 'a';
char *r = &c;
r++;
r--;
return 0;
For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr
14
+ 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 =
1020.
#include <stdio.h>
// Driver Code
int main()
{
// Integer variable
int N = 4;
// Pointer to an integer
int *ptr1, *ptr2;
// Addition of 3 to ptr2
ptr2 = ptr2 + 3;
printf("Pointer ptr2 after Addition: ");
printf("%p \n", ptr2);
return 0;
}
For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we subtract integer 5 from it using the expression,
ptr = ptr – 5, then, the final address stored in the ptr will be ptr = 1000 –
sizeof(int) * 5 = 980.
16
For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are
subtracted. The difference between addresses is 4 bytes. Since the size of int is
4 bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1.
#include <stdio.h>
int main()
17
int N = 4;
// Pointer declaration
x = ptr2 - ptr1;
x);
return 0;
}
18
5. Comparison of Pointers
We can compare the two pointers by using the comparison operators in C. We
can implement this by using all operators in C >, >=, <, <=, ==, !=. It returns true
for the valid condition and returns false for the unsatisfied condition.
1. Step 1: Initialize the integer values and point these integer values to the
pointer.
2. Step 2: Now, check the condition by using comparison or relational
operators on pointer variables.
3. Step 3: Display the output.
#include <stdio.h>
int main()
int arr[5];
if (ptr1 == ptr2) {
"are Equal.");
else {
return 0;
// Driver code
int main()
{
int a = 10, b = 20;
swap(a, b);
printf("Values after swap function are: %d, %d",
a, b);
return 0;
}
utput
Values after swap function are: 10, 20
C
21
// swap function.
#include <stdio.h>
int temp;
temp = *a;
22
*a = *b;
*b = temp;
// Driver code
int main()
a, b);
23
swap(&a, &b);
a, b);
return 0;
Output
C – Pointer to Pointer:
The pointer to a pointer in C is used when we want to store the address of
another pointer. The first pointer is used to store the address of the variable. And
the second pointer is used to store the address of the first pointer. That is why
they are also known as double-pointers. We can use a pointer to a pointer to
change the values of normal pointers or create a variable-sized 2-D array. A
double pointer occupies the same amount of space in the memory stack as a
normal pointer.
24
int val = 5;
The first pointer ptr1 stores the address of the variable and the second pointer
ptr2 stores the address of the first pointer.
#include <stdio.h>
int main()
int* ptr2;
int** ptr1;
ptr2 = &var;
ptr1 = &ptr2;
return 0;
}
26
Structure in C:
The structure in C is a user-defined data type that can be used to group items of
possibly different types into a single type. The struct keyword is used to define
the structure in the C programming language. The items in the structure are
called its member and they can be of any valid data type.
C Structure Declaration
We have to declare structure in C before using it in our program. In structure
declaration, we specify its member variables along with their datatype. We can
use the struct keyword to declare the structure in C using the following syntax:
Syntax
struct structure_name {
data_type member_name1;
28
data_type member_name1;
....
....
};
C Structure Definition
To use structure in our program, we have to define its instance. We can do that
by creating variables of the structure type. We can define structure variables
using two methods:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Syntax
structure_name.member1;
strcuture_name.member2;
struct Point
The reason for the above error is simple. When a datatype is declared, no
memory is allocated for it. Memory is allocated only when variables are created.
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.
.
In this type of initialization, the values are assigned in sequential order as they
are declared in the structure template.
Example:
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int citNo;
float salary;
} person1;
int main() {
person1.citNo = 1984;
return 0;
Output
Salary: 2500.00
Keyword typedef
We use the typedef keyword to create an alias name for data types. It is commonly used with
structures to simplify the syntax of declaring variables.
struct Distance{
int feet;
float inch;
};
int main() {
}
32
int feet;
float inch;
} distances;
int main() {
#include <stdio.h>
#include <string.h>
char name[50];
int citNo;
float salary;
} person;
int main() {
person p1;
33
p1.citNo = 1984;
return 0;
Output
Salary: 2500.00
Nested Structures:
You can create structures within a structure in C programming. For example,
struct complex {
int imag;
34
float real;
};
struct number {
int integers;
} num1, num2;
Suppose, you want to set imag of num2 variable to 11. Here's how you can do it:
num2.comp.imag = 11;
struct complex {
int imag;
float real;
};
struct number {
int integer;
} num1;
int main() {
35
num1.comp.imag = 11;
num1.comp.real = 5.25;
num1.integer = 6;
return 0;
Output:
Imaginary Part: 11
Integer: 6
struct student {
char name[50];
int age;
36
};
// function prototype
int main() {
// \n is discarded
scanf("%[^\n]%*c", s1.name);
scanf("%d", &s1.age);
return 0;
printf("\nDisplaying information\n");
37
Output
Enter age: 13
Displaying information
Name: Bond
Age: 13
C Pointers to struct
Here's how you can create pointers to structs.
struct name {
member1;
member2;
};
int main()
}
38
#include <stdio.h>
struct person
int age;
float weight;
};
int main()
personPtr = &person1;
scanf("%d", &personPtr->age);
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
return 0;
C Unions
A union is a user-defined type similar to structs in C except for one key
difference.
Structures allocate enough space to store all their members, whereas unions
can only hold one member value at a time.
union car
char name[50];
int price;
};
is allocated. To allocate memory for a given union type and work with it, we
need to create variables.
union car
char name[50];
int price;
};
int main()
return 0;
union car
char name[50];
int price;
In both cases, union variables car1, car2, and a union pointer car3 of union car type
41
are created.
#include <stdio.h>
union unionJob
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
42
char name[32];
float salary;
int workerNo;
} sJob;
int main()
return 0;
Output
size of union = 32
size of structure = 40
However, the size of uJob is 32 bytes. It's because the size of a union variable will always be the size
of its largest element. In the above example, the size of its largest element, (name[32]), is 32 bytes.
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
j.workerNo = 100;
return 0;
Output
Salary = 0.0
C enums
In C programming, an enumeration type (also called enum) is a data type that consists of integral
44
By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements
during declaration (if necessary).
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3,
};
Or
Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant
integrals or integers that are given names by a user. The use of enum in C to name the integer values
makes the entire program easy to learn, understand, and maintain by the same or even different
programmer.
In the above syntax, the default value of int_const1 is 0, int_const2 is 1, int_const3 is 2, and so on.
However, you can also change these default values while declaring the enum. Below is an example
of an enum named cars and how you can change the default values.
BMW=0, Ferrari=1, Jeep=2, and Mercedes-Benz=3. However, to change the default values, you can
define the enum as follows:
enum cars{
BMW=3,
Ferrari=5,
Jeep=0,
Mercedes-Benz=1
};
Suppose we have declared an enum type named condition; we can create a variable for that data
type as mentioned above. We can also converge both the statements and write them as:
For the above statement, the default value for true will be 1, and that for false will be 0.
int main(){
for(int i=Sunday;i<=Saturday;i++){
printf("%d, ",i);
return 0;
Output:
In the above code, we declared an enum named days consisting of the name of the weekdays
starting from Sunday. We then initialized the value of Sunday to be 1. This will assign the value for
the other days as the previous value plus 1. To iterate through the enum and print the values of each
day, we have created a for loop and initialized the value for i as Sunday.
enum containers{
cont1 = 5,
cont2 = 7,
cont3 = 3,
cont4 = 8
};
int main(){
47
cur_cont = cont3;
cur_cont = cont1;
return 0;
Output:
We have declared an enum named containers with four different containers as the elements in the
above code. We have then given custom values to the elements and initialized the variable for the
enum multiple times to print the relevant output.
#include <stdio.h>
int main(){
enum directions d;
d=West;
switch(d){
case North:
break;
case East:
break;
case West:
break;
case South:
break;
return 0;
}
49
Output: