Professional Documents
Culture Documents
Presentation 14071 Content Document 20231212073424PM
Presentation 14071 Content Document 20231212073424PM
What is a Pointer in C?
• A pointer is defined as a derived data type that can
store the address of other C variables or a memory
location. We can access and manipulate the data
stored in that memory location using pointers.
• As the pointers in C store the memory addresses,
their size is independent of the type of data they are
pointing to.
• This size of pointers in C only depends on the system
architecture.
Syntax of C Pointers
• The syntax of pointers is similar to the variable
declaration in C, but we use the ( * ) dereferencing
operator in the pointer declaration.
datatype * ptr;
where
• ptr is the name of the pointer.
• datatype is the type of data it is pointing to.
• The above syntax is used to define a pointer to a
variable. We can also define pointers to functions,
structures, etc.
How to Use Pointers?
The use of pointers in C can be divided into three steps:
• Pointer Declaration
• Pointer Initialization
• Pointer Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not
initialize it. To declare a pointer, we use the ( * ) dereference
operator before its name.
• Example
int *ptr;
• We can also declare and initialize the pointer in a single step. This method
is called pointer definition as the pointer is declared and initialized at the
same time.
Example
int *ptr = &var;
b = 20; 0x1008 20 b
0x100C
p1 = &a; 0x1010
Variable s1
Address 0x120
Contents 0x100
C Pointers and Arrays
Pointers & Arrays
• You can also use pointers to access arrays.
• Consider the following array of integers:
Example
int myNumbers[4] = {25, 50, 75, 100};
• You learned from the arrays chapter that you can loop through the array elements
with a for loop:
• Instead of printing the value of each array element, let's print the
memory address of each array element:
pa = arr; /* This initializes pointer ‘pa’ to point to the first element of array
‘arr’ – The name of an array evaluates to the starting address of the array */
To access the ‘i’th element of array arr, we can use the following syntax:
int b;
b = pa[i]; /* The pointer variable can be used like the array name */
printf("arr1[0][0]=%d\n",p2[0][0]); printf("arr1[2][3]=
%d\n",p2[2][3]);
Another way to declare 2D pointers
printf("arr1[0][0]=%d\n",(*p2)[0][0]);
printf("arr1[2][3]=%d\n",(*p2)[2][3]);
printf("arr1[1][4]=%d\n",(*p2)[1][4]);
Pointer Arithmetic in C
Note: Function call is neccessary to bring the program control to the function definition. If
not called, the function statements will not be executed.
Create a Function
• Declared functions are not executed immediately. They are "saved for
later use", and will be executed when they are called.
• To call a function, write the function's name followed by two
parentheses () and a semicolon ;
• In the following example, myFunction() is used to print a text (the action),
when it is called:
A function can be called multiple times:
C Function Parameters
Parameters and Arguments
• Information can be passed to functions as a parameter. Parameters act as variables inside the
function.
• Parameters are specified after the function name, inside the parentheses. You can add as
many parameters as you want, just separate them with a comma:
• A parameter is a variable that we use in a function definition. (Formal parameters)
• An argument is the actual data that we pass to the function parameter.(Actual Paramaters)
When we want to pass an array to a function, it is always passed by giving the name of the array, which is nothing but the
start address of the array.
• Passing the pointers to the function means the memory location of the variables is passed to
the parameters in the function, and then the operations are performed.
• The function definition accepts these addresses using pointers, addresses are stored using
pointers.
Arguments Passing without pointer
• When we pass arguments without pointers the changes made by the function would be done
to the local variables of the function.
Arguments Passing with pointers
The value of each variable in the calling function The address of actual variables in the calling
is copied into corresponding dummy variables of function is copied into the dummy variables of
the called function. the called function.
The changes made to the dummy variables in the Using addresses we would have access to the
called function have no effect on the values of actual variables and hence we would be able to
actual variables in the calling function. manipulate them.
In call-by-values, we cannot alter the values of In call by reference, we can alter the values of
actual variables through function calls. variables through function calls.
Values of variables are passed by the Simple Pointer variables are necessary to define to store
technique. the address values of variables.
This method is preferred when we have to pass This method is preferred when we have to pass a
some small values that should not change. large amount of data to the function.
Recursion