Pointers are among C’s most powerful, yet most difficult concepts to master. Some tasks like dynamic memory allocation done only by using pointers. So it is essential to learn pointers.
Pointers are a type of variable, just like int, double, etc., except instead of storing a value, they store a memory address of another variable.
This Presentation gives you all knowledge about #CPointers, #PointersInC.
The given slides are as follows:
1. Introduction of Pointers.
2. Systems to create a pointer in C.
3. Valid Pointer Examples in C.
4. Graphical representation of address assigning in c using pointer.
5. 1st Sample program of pointers in C.
6. Types of Pointers
7. NULL Pointer in C (Types of Pointer)
8. void Pointer in C (Types of Pointer)
9. Wild Pointer in C (Types of Pointer)
10. Changing value of variable using pointer
11. Update and Access value of variable using pointer in example.
12. Types of format specifies to print address in different format.
13. Advantages of Pointer in C.
14. Disadvantages of Pointer in C.
15. Pointers with Array using Programs. #pointerwitharray
16. Another Example of #pointerwitharray.
17. Relationship Between Arrays and Pointers.
18. Relationship Between Arrays and Pointers explanation with diagram.
19. Pointers and String. #PointersAndString
20. Function with Array Parameters using pointers.
21. Passing Pointers to Function or Pointer argument function. #PointerWithFunction
Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.
This Presentation gives you all knowledge about #CPointers, #PointersInC.
The given slides are as follows:
1. Introduction of Pointers.
2. Systems to create a pointer in C.
3. Valid Pointer Examples in C.
4. Graphical representation of address assigning in c using pointer.
5. 1st Sample program of pointers in C.
6. Types of Pointers
7. NULL Pointer in C (Types of Pointer)
8. void Pointer in C (Types of Pointer)
9. Wild Pointer in C (Types of Pointer)
10. Changing value of variable using pointer
11. Update and Access value of variable using pointer in example.
12. Types of format specifies to print address in different format.
13. Advantages of Pointer in C.
14. Disadvantages of Pointer in C.
15. Pointers with Array using Programs. #pointerwitharray
16. Another Example of #pointerwitharray.
17. Relationship Between Arrays and Pointers.
18. Relationship Between Arrays and Pointers explanation with diagram.
19. Pointers and String. #PointersAndString
20. Function with Array Parameters using pointers.
21. Passing Pointers to Function or Pointer argument function. #PointerWithFunction
Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.
In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.
There are few important operations, which we will do with the help of pointers very frequently. (a) we define a pointer variable (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.
There are few important operations, which we will do with the help of pointers very frequently. (a) we define a pointer variable (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
At the end of this lecture students should be able to;
Define the C pointers and its usage in computer programming.
Describe pointer declaration and initialization.
Apply C pointers for expressions.
Experiment on pointer operations.
Identify NULL pointer concept.
Experiment on pointer to pointer, pointer arrays, arrays with pointers and functions with pointers.
Apply taught concepts for writing programs.
C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
Cyber security refers to every aspect of protecting an organization and its employees and assets against cyber threats. As cyberattacks become more common and sophisticated and corporate networks grow more complex, a variety of cyber security solutions are required to mitigate corporate cyber risk.
This presentation focus on the basic concept of pointers. so that students can easily understand. it also contains basic operations performed by pointer variables and implementation using c language.
We all have good and bad thoughts from time to time and situation to situation. We are bombarded daily with spiraling thoughts(both negative and positive) creating all-consuming feel , making us difficult to manage with associated suffering. Good thoughts are like our Mob Signal (Positive thought) amidst noise(negative thought) in the atmosphere. Negative thoughts like noise outweigh positive thoughts. These thoughts often create unwanted confusion, trouble, stress and frustration in our mind as well as chaos in our physical world. Negative thoughts are also known as “distorted thinking”.
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
How to Create Map Views in the Odoo 17 ERPCeline George
The map views are useful for providing a geographical representation of data. They allow users to visualize and analyze the data in a more intuitive manner.
Palestine last event orientationfvgnh .pptxRaedMohamed3
An EFL lesson about the current events in Palestine. It is intended to be for intermediate students who wish to increase their listening skills through a short lesson in power point.
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
2. Introduction
2
Pointers are among C’s most powerful, yet most difficult concepts to master
Some tasks like dynamic memory allocation done only by using pointers. So
it is essential to learn pointers.
Pointers are a type of variable, just like int, double, etc., except instead of
storing a value, they store a memory address of another variable.
(or)
Pointer is a variable that stores the address of the other variable.
In this sense, a variable directly references a value, and a pointer indirectly
references a value.
The goal of the pointer is to save memory space and perform faster
execution.
The size of the pointer in C is 8 bytes but on a 32-bit machine, they take up
to 4 bytes.
3. How does Pointer Works?
3
If we declare a variable x of type Integer (int) then x will actually store the
value.
int x= 1;
x is equal to one now.
But, every variable has both value and address, and that address can be
retrieved by putting an ampersand (&) before the variable name like this.
"&x"
If you are willing to print an address of a variable that address may be a
random number and that random number will be different whenever you
run your program. which means the same program may give different
outputs.
4. How does Pointer Works?
(cont.)
4
Example:
#include<stdio.h>
int main()
{
int x=1;
printf(“%d”, &x);
return 0;
}
Output: 6422040 and if you run the same code for the second time the
output may be different.
In simple terms, variable store values and pointers store the address of
the variable.
5. Pointer Declaration
5
Like variables, pointers should be declared before using it in the
program. We can name pointers anything as long as they obey C’s
naming rules.
Syntax:
datatype *pointer_variable_name;
Example:
int *ptr;
Here, ptr is a pointer variable of type integer which holds the address of
any integer variable.
6. Initializing a pointer
6
After declaring a pointer, we have to initialize the pointer with the standard
variable address.
If pointers are not initialized then there may be a problem in the output.
Syntax:
pointer_var_name= &variable;
Example:
ptr= &v;
Here, the address of integer variable ‘v’ is assigned to pointer variable
‘ptr’.
7. Examples
7
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
double *T /* T is a double pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
8. How to Use Pointers?
8
a) First define a pointer variable,
b) Assign the address of a variable to a pointer and
c) Finally access the value at the address available in the
pointer variable.
This is done by using unary operator * that returns the value
of the variable located at the address specified by its
operand.
9. 9
#include <stdio.h>
int main () {
int v = 50; /* actual variable
declaration */
int *ptr; /* pointer variable declaration */
ptr = &v; /* store address of v in pointer variable*/
printf("Address of var variable: %un", &v);
/* address stored in pointer variable */
printf("Address stored in ptr variable: %unn", ptr );
printf("Value of v: %dn", v );
/* access the value of v using the pointer */
printf("Value pointed by *ptr variable: %dn", *ptr );
return 0;
Output:
Address of var variable: 6422036
Address stored in ptr variable:
6422036
Value of v: 50
Value pointed by *ptr variable: 50
10. Address (&) Operator
10
The address of (&) operator can be used in front of any variable
object in C -- that returns the location in memory of the variable
Syntax:
&VariableReference;
Examples:
int V;
int *P;
int A[5];
&V - memory location of integer variable V
&(A[2]) - memory location of array element 2 in array A
&P - memory location of pointer variable P
11. Indirection (*) Operator
11
It is also called as Value at address or dereferencing operator
A pointer variable contains a memory address.
To refer to the contents of the variable that the pointer points to, we
use indirection operator.
Syntax:
*PointerVariable;
Example:
int v = 50;
int *ptr = &v
// Then *ptr would refer to the contents of the variable v (in this case, the integer
50.)
printf(“%d”,*P); /* Prints 50 */
12. Sample pointer code snippet.
12
int A = 3;
int B;
int *P = &A;
int *Q = P;
int *R = &B;
printf(“Enter value:“);
scanf(“%d”,R);
printf(“%d %dn”,A,B);
printf(“%d %d %dn”,
*P,*Q,*R);
Q = &B;
if (P == Q)
printf(“1n”);
if (Q == R)
printf(“2n”);
if (*P == *Q)
printf(“3n”);
if (*Q == *R)
printf(“4n”);
if (*P == *R)
printf(“5n”);
13. Pointer to pointer
13
A pointer to a pointer is a form of multiple indirection, or a chain of pointers.
When we define a pointer to a pointer, the first pointer contains the address
of the second pointer, which points to the location that contains the actual
value as shown below.
Pointer to pointer relations can be applied up to 12 stages but generally,
there is no limitation.
When we are increasing the pointer to pointer relations, then performance
will be decreased.
Syntax:
datatype **var;
14. Pointer to pointer…
14
Example:
int v = 50;
int *ptr;
int **ptr;
ptr = &v; /* ptr points the address of v */
pptr = &ptr; /* pptr points the address of ptr using address of
operator & */
printf(“%d %d %dn”, v,*ptr,**pptr); /* prints 50 3 times */
15. Address of and Dereference operators
15
The address of (&) and dereference (*) operators are actually
inverses of each other.
They cancel each other out.
Example:
*&myVar == myVar
and
&*yPtr == yPtr
16. L and R values
16
L-value is something that can appear on the left side of an equal sign ( =
).
A place i.e. memory location for a value to be stored
R-value is something that can appear on the right side of an equal sign (
= ).
A value
Example:
a = b+25 vs b+25 = a.
17. Types of Pointers
17
There are eight different types of pointers. They are:
1. Null pointer
2. Void pointer
3. Wild pointer
4. Dangling pointer
5. Complex pointer
6. Near pointer
7. Far pointer
8. Huge pointer
18. NULL Pointer
18
NULL Pointer is a pointer which is pointing to nothing. In case, if we
don’t have address to be assigned to a pointer, then we can simply
use NULL.
A NULL pointer always contains value 0.
Some advantages of Null pointer are:
We can initialize a pointer variable when that pointer variable is not assigned any
actual memory address.
We can pass a null pointer to a function argument when we are not willing to pass
any actual memory address.
Example 1: Example 2:
int *ptr = NULL; //null pointer
printf("The value inside ptr is:%dn",ptr);
//prints 0
int fun(int *ptr)
{
return 15;
}
fun(NULL);
19. Casting operators
19
When assigning a memory address of a variable of one type to a
pointer that points to another type it is best to use the cast operator
to indicate the cast is intentional (this will remove the warning)
Example:
int V = 50;
float *P = (float *) &V; /* Casts int address to float * */
Removes warning, but is still a somewhat unsafe thing to do.
20. Void pointer
20
The void pointer is a pointer that is not allied with any data types. This points to some
data location within the storage which doesn’t have any specific type.
It is also known as a generic pointer.
If we assign address of char data type to void pointer it will become char Pointer, if int
data type then int pointer and so on.
It is created by using the keyword void.
A void pointer cannot be dereferenced.
21. 21
Let us consider the following code snippet to understand how void pointers works:
#include <stdio.h>
int main()
{
int x= 10;
char y= 'a';
void *ptr = &x; // void pointer contains address of int x
// (int *) ptr - does type casting of void.
// *((int*)ptr) dereferences the typecasted void pointer variable.
printf("Integer variable is = %d", *( (int*) ptr) );
ptr = &y; //void pointer holds of char y.
printf("ncharacter variable is= %c", *( (char*) ptr) );
return 0;
}
22. Wild pointer
22
Pointers that are not initialized are called wild pointers.
This pointer may be initialized to a non-NULL garbage value which may not be
a valid address.
Because they may point to some unknown memory location which may cause
problems in our program. This may lead to the crashing of the program.
Example :
int main()
{
int *p; // wild pointer
*p= 10;
printf("%d",ptr);
return 0;
}
Note: This pointer becomes wild pointer until it points to a normal variable
23. Dangling pointer
23
A pointer that points to a memory location that has been deleted (freed) is called a
dangling pointer.
There are three different ways where Pointer acts as dangling pointer
1. De-allocation of memory.
2. Function call.
3. Variable goes out of scope.
Example:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
// After below free call, ptr becomes a dangling pointer
free(ptr);
// No more a dangling pointer
ptr = NULL;
}
24. 24
These are some old concepts used in 16 bit intel architectures in the days of MS DOS,
not much useful anymore.
In TURBO C there are three types of pointers. TURBO C works under DOS operating
system which is based on 8085 microprocessor.
1. Near pointer
2. Far pointer
3. Huge pointer
Near pointer is used to store 16 bit addresses means within current segment on a 16
bit machine. The limitation is that we can only access 64kb of data at a time.
A far pointer is typically 32 bit that can access memory outside current segment. To
use this, compiler allocates a segment register to store segment address, then another
register to store offset within current segment.
Like far pointer, huge pointer is also typically 32 bit and can access outside segment. In
case of far pointers, a segment is fixed. In far pointer, the segment part cannot be
modified, but in Huge it can be.
Near, Far and Huge
pointers
25. Constant Pointers
25
Const Pointers, just like any const variable, is unable to be
changed once it is initialized.
A constant pointer is one that cannot change the address it
contains. In other words, we can say that once a constant pointer
points to a variable, it cannot point to any other variable.
Const Pointers are Pointers that always point to the same memory
location.
These must be initialized when they are declared.
Syntax:
<type of pointer> *const <pointer name>;
Example:
int *const ptr;
26. 26
Let us consider the following example to understand how constant
pointers works:
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
//Definition of constant constant
int *const ptr = &a; //Now, ptr is pointing to the value of the variable a.
*ptr = 30; // Works, since the pointer pointing to the value is not
constant.
ptr = &b; //Error: Now, ptr is pointing to the value of the variable b.
return 0;
}
Constant Pointers…
27. Pointer to constant
27
As the name itself indicates, the value of the variable to which
the pointer is pointing, is constant.
In other words, a pointer through which one cannot change the
value of the variable to which it points is known as a pointer to
constant.
Syntax:
const <type of pointer> *<pointer name>;
Example:
const int *ptr;
28. 28
Let us consider the example given below to understand how a pointer to
constant works:
#include <stdio.h>
int main( )
{
int a = 10;
//Definition of pointer to constant
const int* ptr = &a; //Now, ptr is pointing to the value of the variable a.
*ptr = 30; //Error: Since the value is constant.
return 0;
}
In the above code, in Line No. 7, we are trying to change the value of the variable
to which the pointer is pointing to, but this is not possible since the value is
Pointer to constant…
29. Pointer arithmetic
29
Welcome to the world of weird programming errors.
This is another example of the powerful but dangerous
nature of pointers.
Pointer Arithmetic is so error prone, it’s not allowed in
Java or C#.
This doesn’t mean don’t use – you may have, or at least
understand it so you can understand other people’s
code. So master this.
30. Pointer arithmetic…
30
Pointer is a variable that points to a memory location. Memory
addresses are numeric value that ranges from zero to maximum
memory size in bytes.
These addresses can be manipulated like simple variables. You can
increment, decrement, calculate or compare these addresses manually.
You can perform a limited number of arithmetic operations on pointers.
These operations are:
Increment (++)
Decrement (--)
Addition (+)
Subtraction (-)
Comparison (>, >=, <, <=, ==, !=)
31. Pointer arithmetic…
31
Pointer increment and decrement:
Increment (++) operator when used with a pointer variable returns next address
pointed by the pointer.
The next address returned is the sum of current pointed address and size of
pointer data type.
Similarly, decrement operator returns the previous address pointed by the
pointer.
The returned address is the difference of current pointed address and size of
pointer data type.
The Rule to increment/decrement the pointer is given below:
Where n is the number by which the pointer get increased.
new_address= current_address ± n * size_of(data type)
32. Pointer arithmetic…
32
Let's see the example of incrementing and decrementing pointer
variable.
#include<stdio.h>
int main()
{
int v=50;
int *ptr; //pointer to int
ptr=&v; //Stores the address of variable v
printf("Address of p variable is %u n",p);
p++;
printf("After increment: Address of p variable is %u n",p);
p--;
printf("After decrement: Address of p variable is %u n",p);
return 0;
}
33. 33
Expression
Assuming p is a
pointer to a…
…and the size
of *p is…
Value added to
the pointer
p+1 char 1 1
p+1 short 2 2
p+1 int 4 4
p+1 double 8 8
p+2 char 1 2
p+2 short 2 4
p+2 int 4 8
p+2 double 8 16
34. Pointer arithmetic…
34
Pointer comparison:
You can compare two pointers with the following operators: ==, !=, <, >, <=,
and >=.
The == and != operators are used to compare two pointers whether they
contain the same address or not.
Two pointers are equal when they both are null, or contains the address of
the same variable.
Use of these (i.e. == and !=) operators are valid only when pointers are of
the same base type, or between a null pointer and any other pointer, or void
pointer and any other pointer.
Pointer comparisons can be performed even when the pointers point to
elements of different arrays.
35. Pointer arithmetic…
35
#include<stdio.h>
int main() {
int a = 12, b=12;
int *ptr1, *ptr2;
ptr1 = &a;
ptr2 = &b,
printf("address of a = %un", ptr1);
printf("address of b = %un", ptr2);
if (ptr1==ptr2)
printf("Addresses are samen");
else
printf("Addresses are differentn");
if (*ptr1==*ptr2)
printf("Values are samen");
else
printf("Values are differentn");
return 0;
}
Let's see the example of comparison of two pointer variables.
36. lllegal arithmetic with pointers
36
There are various operations which can not be performed on pointers.
Since, pointer stores address hence we must ignore the operations which
may lead to an illegal address.
A list of such operations is given below.
Address + Address = illegal
Address * Address = illegal
Address % Address = illegal
Address / Address = illegal
Address & Address = illegal
Address ^ Address = illegal
Address | Address = illegal
~Address = illegal
37. Pointer to an array
So again, what is an array really?
That’s right, a const pointer to the first element of the array.
So, we can create a Pointer to the first element of an array with
code like
int b[5] = { 10,5,18,33,21 };
int *p;
p = b; //equivalent to p = &b[0];
p is a pointer to &b[0], which is the address of the first element of
the array b.
38. Pointer to an array…
Normally, when we would want to access the 4th element in our array, we’d use notation
like b[3];
but, we can also do
( p + 3 ); //actually does address of p + 3 * 4
This is called using Pointer/Offset notation.
We can also access pointers using subscripts
p[3]; // same as above
This is called, Pointer/Subscript notation.
Once you store the address of the first element in 'p', you can access the array
elements using *p, *(p+1), *(p+2) and so on.
39. Pointer to an array…
39
Given below is the example to show all the concepts discussed above
−int main() {
int b[5]={10,5,18, 33,21}; /* an array with 5 elements */
int *p;
int i;
p = b; //address of first element is assigned to p
printf("Array values using pointersn");
for ( i = 0; i < 5; i++ )
printf("*(p + %d) : %dn", i, *(p + i) );
printf( "Array values using b as addressn");
for ( i = 0; i < 5; i++ )
printf("*(b + %d) : %dn", i, *(b + i) );
return 0;
}
41. Combining Indirection operator (*) and Inc/Dec operator
41
While processing elements of an array, C programmers often mix indirection
operator (*) and increment/decrement operator( ++ and -- ).
Suppose x is integer variable and p is a pointer to int. Now consider the
following statements and try to interpret them.
Example 1:
Example 2:
Example 3:
x = *p++;
x = *++p;
x = ++*p;