C Programming 3
C Programming 3
A pointer is variable that store address of another variable as its value .These variable could be any type
of char , int , functions, array, or other variable
The pointer sizes depend on their architecture. But, keep in mind that the size of a pointer in the 32-bit
architecture is 2 bytes
Syntax of Pointers in C
data_type * pointer_variable_name;
Declaration of a pointer
The asterisk (*)is used to declare a pointer
The dereference operator * returns the value stored at the memory address pointed to by the
pointer
Declaring a pointer
Dereferencing a pointer
Initialization of a pointer
&(ampersand) operator used to get the variable address in the program Pointer initialization is done
with the following syntax.
pointer = &variable;
simple program
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%x\n",p); //accessing the address
printf("Value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}
Types of Pointers
Null Pointer
We can create a null pointer by assigning null value during the pointer declaration.
This method is useful when you do not have any address assigned to the pointer. A
null pointer always contains value 0.
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not
have any standard data type. A void pointer is created by using the keyword void. It
can be used to store an address of any variable.
#include <stdio.h>
int main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}
Wild pointer
Dangling pointer
Complex pointer
Near pointer
Far pointer
Huge pointer
Direct and Indirect Access Pointers
In C, there are two equivalent ways to access and manipulate a variable content
#include <stdio.h>
/* Declare and initialize an int variable */
int var = 1;
return 0;}
Pointer to an array
#include <stdio.h>
int main()
{
int a[5]={1,2,3,4,5}; //array initialization
int *p; //pointer declaration
/*the ptr points to the first element of the array*/
Pointer to a structure
#include <stdio.h>
#include <string.h>
struct student {
char name[20];
int age;
float score;
};
int main() {
struct student s;
struct student *ptr;
Pointer to a String
#include <stdio.h>
int main() {
char *str = "Hello, world!";
printf("%s\n", str);
return 0;
Advantages of Pointers in C
Pointers are useful for accessing memory locations.
Pointers provide an efficient way for accessing the elements of an array
structure.
Pointers are used for dynamic memory allocation as well as deallocation.
Pointers are used to form complex data structures such as linked list, graph,
tree, etc.
Disadvantages of Pointers in C
Pointers are a little complex to understand.
Pointers can lead to various errors such as segmentation faults or can access
a memory location which is not required at all.
If an incorrect value is provided to a pointer, it may cause memory
corruption.
Pointers are also responsible for memory leakage.
Pointers are comparatively slower than that of the variables.
Programmers find it very difficult to work with the pointers; therefore it is
programmer’s responsibility to manipulate a pointer carefully
Function
In this way, if we break a complex program into functions, where each function performs a much
simpler task, the program will become simple to code and will improve readability. functions can
be reused again and again throughout the program, whenever required. This also helps in
reducing repetitive codes in our program.
C functions are used to avoid rewriting same logic/code again and again in a program.
There is no limit in calling C functions to make use of same functionality wherever required.
We can call functions any number of times in a program and from any place in a program.
A large C program can easily be tracked when it is divided into functions.
The core concept of C functions are, re-usability, dividing a big task into small pieces to
achieve the functionality and to improve understandability of very large C programs
functions can be classified into two categories,
1. Library functions
2. User-defined functions
Library functions are those functions which are already defined in C library,
example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use
these functions. These are already declared and defined in C libraries.
A User-defined functions on the other hand, are those functions which are defined by the user
at the time of writing program. These functions are made for code reusability and for saving
time and space.
Function Declaration
The function declaration tells the compiler about function name, the data type of the
return value and parameters. The function declaration is also called a function
prototype. The function declaration is performed before the main function or inside the
main function or any other function.It's also called as Function Prototyping. Function
declaration consists of 4 parts.
Return type: This specifies the data type of the value that the function returns.
Function name: This is a unique identifier for the function.
Parameters: These are the values that are passed to the function when it is called. Each
parameter consists of a data type and a parameter name.
Function body: This is the block of code that defines the actions that the function will
perform
Function declaration syntax
Defining a function means specifying the body inside the function. This will be the code
that will be executed every time the function is called .
The function definition provides the actual code of that function. The function definition is
also known as the body of the function. The function body contains the declarations and the
statements(algorithm) necessary for performing the required task the actual instructions to
be performed by a function are written in function definition. The body is enclosed within
curly braces { ... } and consists of three parts.
The function definition is performed before the main function or after the main function.
Function declaration or prototype – This informs compiler about the function name, function parameters
and return value’s data type.
Function definition – This contains all the statements to be executed.
Function call – This calls the actual function
Calling a function
When a function is called, control of the program gets transferred to the function.
A function call is the act of invoking a function in order to execute its code. It involves
passing arguments to the function and receiving a return value from it.
In C, a function call is made by using the function name followed by a set of parentheses
that contain the arguments to be passed to the function. The arguments are separated by
commas.
Function calling in C is the process of executing the code inside a function by using its
name. It's a way of telling the program to run a specific block of code when it's needed.
Think of a function as a mini-program within your main program. When you want to use
the mini-program, you "call" it by writing the function name followed by a set of
parentheses. Inside the parentheses, you can pass in any arguments that the function
needs to do its job.
1. Call by value
2. Call by reference
CALL BY VALUE:
When passing arguments to a function by value, a copy of the actual argument is created and passed to the function. This means
that any changes made to the parameter inside the function have no effect on the actual argument outside the function. In call by
value, only the value of the variable is passed to the function.
In call by value method, the value of the variable is passed to the function as parameter.
The value of the actual parameter can not be modified by formal parameter.
Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is
copied to formal parameter.
CALL BY REFERENCE:
When passing arguments to a function by reference, a reference or pointer to the actual argument is passed to the function
In call by reference method, the address of the variable is passed to the function as parameter.
The value of the actual parameter can be modified by formal parameter.
Same memory is used for both actual and formal parameters since only address is used by both parameters.
functions can accept parameters, also known as arguments. These arguments are passed to the
function when it is called, and the function can use the values of these arguments to perform its
operations.
Arguments are the values specified during the function call, for which the formal parameters are
declared while defining the function.
Note:
Formal parameters and actual parameters are terms used in computer programming to describe the
parameters used in a function.
Formal parameters are the parameters listed in the function declaration or definition. They are
placeholders for the values that will be passed to the function when it is called.
Actual parameters, on the other hand, are the values passed to the function when it is called
Actual parameter – This is the argument which is used in function call.
Formal parameter – This is the argument which is used in function definition
A Function can return a value to the calling code using the return statement. The return type of
a function is specified in the function declaration and determines the type of value that the
function will return
#include <stdio.h>
int main() {
int x = 10;
int y = square(x);
printf("The square of %d is %d\n", x, y);
return 0;
}
Library Functions
The library functions are built-in functions. In C programming language, the standard functions
are declared in header files and defined in .dll files. In simple words, the standard functions can
be defined as "the ready made functions defined by the system to make coding more easy".
The standard functions are also called as standard functions or pre-defined functions.
when we use standard functions, we must include the respective header file
using #include statement. For example, the function printf() is defined in header
file stdio.h (Standard Input Output header file). When we use printf() in our
program, we must include stdio.h header file using #include<stdio.h> statement.
Header
File Purpose Example
time.h Provides functions to perform operations on time and date time(), localtime()
setjmp.h Provides functions that are used in function calls setjump(), longjump()
locale.h Defines the location specific settings such as date formats setlocale()
and currency symbols
stdarg.h Used to get the arguments in a function if the arguments are va_start(), va_end(), va_arg()
not specified by the function
Recursive Functions in C
A function called by itself in order to solve a problem. this technique is known as recursion
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("The factorial of %d is %d\n", num, factorial(num));
return 0;
}
Preprocessor
The C Preprocessor is not a part of the compiler, but is a separate step in the compilation
process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the
compiler to do required pre-processing before the actual compilation.
The C preprocessor provides four separate facilities that you can use as you see fit:
Inclusion of header files. These are files of declarations that can be substituted into your program.
Macro expansion. You can define macros, which are abbreviations for arbitrary fragments of C
code, and then the C preprocessor will replace the macros with their definitions throughout the
program.
Macro
Macro substitution has a name and replacement text, defined with #define directive. The preprocessor
simply replaces the name of macro with replacement text from the place where the macro is defined in the
source code.
include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main() {
float radius, area;
return 0;
}
C programming language, there are some pre-defined macros and they are as follows...
Control statements
Loops statement
Break statement
Decision making