Pointers are variables that store memory addresses. A pointer must be declared with a data type and is initialized by using the address-of operator (&) to point to another variable. Pointer variables can be used to access and modify the value of the variable being pointed to using the indirection operator (*). Arrays can also be accessed using pointers by treating array names as pointers to the first element. Memory for dynamic data can be allocated at runtime using functions like malloc() and calloc() and pointers are used to access this memory.
The document provides an overview of pointers in C programming. It defines pointers as variables that contain the address of another variable in memory. The document outlines various pointer operations such as dereferencing with *, address of with &, arithmetic, comparisons, NULL pointers, function pointers, pointers to arrays, arrays of pointers, and pointers to pointers. It provides examples to illustrate how to declare, initialize, and manipulate pointers in C code.
This document provides an overview of pointers in C programming. It defines pointers as variables that store memory addresses rather than values. Pointers have several useful applications like accessing variables outside functions, passing information between functions, and more efficiently handling data tables. The document explains how to declare pointer variables, assign the address of a variable to a pointer, dereference a pointer to access the value at an address, and pass pointers to functions. It also discusses pointers and arrays, structures, and arrays of structures.
The document discusses pointers in C programming. It defines pointers as variables that store the memory addresses of other variables. It provides examples of declaring pointer variables and using dereference and reference operators. It also covers pointer arithmetic, pointers to pointers, pointers to arrays, pointers as function arguments, pointers to structures including self-referential structures, enumerations, and bitfields. Key concepts are illustrated with code examples and outputs.
The document discusses pointers in C programming. It begins by defining a pointer as a variable that holds the memory address of another variable. Pointers allow indirect access to variables using their addresses. Pointers are declared with a * before the variable name and initialized using the address operator &. The indirection operator * is used to dereference a pointer and access the value at the addressed memory location. Pointers can be used to pass arguments to functions by address, access arrays indirectly, and dynamically allocate memory on the heap. Operations on pointers include assignment, addition/subtraction with integers to offset the address, subtracting/comparing pointers, and incrementing/decrementing.
Pointer variables contain memory addresses that point to other variables in memory. A pointer contains the address of another variable. Pointers provide indirect access to data in memory. Pointer variables must be declared with a data type and the * symbol indicates it is a pointer. The & operator returns the memory address of a variable and * dereferences a pointer to access the value at that memory address. Pointers can be assigned, compared, and perform arithmetic operations like incrementing to point to the next memory location.
Pointers in C programming store the address of other variables or memory locations. Pointers allow accessing and manipulating the data stored at those memory addresses. Pointers are useful for accessing arrays, dynamically allocating memory, and passing arguments by reference. Pointer variables must be declared with a data type and dereferenced using the * operator. Pointers can be initialized by assigning the address of another variable using the & operator. Pointer arithmetic allows incrementing or decrementing pointers to access successive memory locations.
A pointer is a variable that stores the memory address of another variable. Pointers allow accessing and modifying the data stored at the referenced memory location. Pointers can be declared by specifying the data type followed by an asterisk, and are initialized by assigning the address of a variable to the pointer variable. Pointer variables can be used in expressions and arithmetic and can be passed to functions to modify the referenced data. Arrays can also be accessed and traversed using pointers by treating the array name as a pointer to its first element. Pointers to functions allow functions to be passed as arguments and enables polymorphism.
- A structure in C is a user-defined data type that allows storing multiple data types together under one name.
- Structures can store different attributes like name, roll number, marks of a student entity together rather than separate arrays.
- Structures are defined using the struct keyword followed by the structure name and members.
- Structure variables can access members using dot (.) operator.
- Arrays of structures can store information of multiple entities. Structures can also be nested to store sub-parts of an attribute together.
The document provides an overview of pointers in C programming. It defines pointers as variables that contain the address of another variable in memory. The document outlines various pointer operations such as dereferencing with *, address of with &, arithmetic, comparisons, NULL pointers, function pointers, pointers to arrays, arrays of pointers, and pointers to pointers. It provides examples to illustrate how to declare, initialize, and manipulate pointers in C code.
This document provides an overview of pointers in C programming. It defines pointers as variables that store memory addresses rather than values. Pointers have several useful applications like accessing variables outside functions, passing information between functions, and more efficiently handling data tables. The document explains how to declare pointer variables, assign the address of a variable to a pointer, dereference a pointer to access the value at an address, and pass pointers to functions. It also discusses pointers and arrays, structures, and arrays of structures.
The document discusses pointers in C programming. It defines pointers as variables that store the memory addresses of other variables. It provides examples of declaring pointer variables and using dereference and reference operators. It also covers pointer arithmetic, pointers to pointers, pointers to arrays, pointers as function arguments, pointers to structures including self-referential structures, enumerations, and bitfields. Key concepts are illustrated with code examples and outputs.
The document discusses pointers in C programming. It begins by defining a pointer as a variable that holds the memory address of another variable. Pointers allow indirect access to variables using their addresses. Pointers are declared with a * before the variable name and initialized using the address operator &. The indirection operator * is used to dereference a pointer and access the value at the addressed memory location. Pointers can be used to pass arguments to functions by address, access arrays indirectly, and dynamically allocate memory on the heap. Operations on pointers include assignment, addition/subtraction with integers to offset the address, subtracting/comparing pointers, and incrementing/decrementing.
Pointer variables contain memory addresses that point to other variables in memory. A pointer contains the address of another variable. Pointers provide indirect access to data in memory. Pointer variables must be declared with a data type and the * symbol indicates it is a pointer. The & operator returns the memory address of a variable and * dereferences a pointer to access the value at that memory address. Pointers can be assigned, compared, and perform arithmetic operations like incrementing to point to the next memory location.
Pointers in C programming store the address of other variables or memory locations. Pointers allow accessing and manipulating the data stored at those memory addresses. Pointers are useful for accessing arrays, dynamically allocating memory, and passing arguments by reference. Pointer variables must be declared with a data type and dereferenced using the * operator. Pointers can be initialized by assigning the address of another variable using the & operator. Pointer arithmetic allows incrementing or decrementing pointers to access successive memory locations.
A pointer is a variable that stores the memory address of another variable. Pointers allow accessing and modifying the data stored at the referenced memory location. Pointers can be declared by specifying the data type followed by an asterisk, and are initialized by assigning the address of a variable to the pointer variable. Pointer variables can be used in expressions and arithmetic and can be passed to functions to modify the referenced data. Arrays can also be accessed and traversed using pointers by treating the array name as a pointer to its first element. Pointers to functions allow functions to be passed as arguments and enables polymorphism.
- A structure in C is a user-defined data type that allows storing multiple data types together under one name.
- Structures can store different attributes like name, roll number, marks of a student entity together rather than separate arrays.
- Structures are defined using the struct keyword followed by the structure name and members.
- Structure variables can access members using dot (.) operator.
- Arrays of structures can store information of multiple entities. Structures can also be nested to store sub-parts of an attribute together.
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.
Pointer variables store the memory addresses of other variables. They can be used to access and modify the values stored at those addresses. Pointers allow values to be passed by reference rather than by value, enabling changes made within functions to be reflected back in the calling function. Common pointer operations include dereferencing a pointer to access the value at an address, pointer arithmetic to increment or decrement a pointer to other memory locations, and passing pointers as function arguments to allow modification of variable values.
- A pointer is a variable that stores the address of another variable. Pointers allow dynamic memory allocation and access to the value of the variable being pointed to using the indirection operator (*).
- Pointer variables are declared with a data type followed by an asterisk, such as int *ptr. They can be initialized by using the address-of operator (&) to store the address of another variable.
- Pointers can be used to access elements in an array by using pointer arithmetic. An array name itself is a constant pointer to the first element of the array.
Pointers allow programs to store and manipulate memory addresses in C. A pointer is a variable that contains the address of another variable. Pointers can be used to access array elements, pass arguments by reference, and dynamically allocate memory on the heap. There are different types of pointers like void pointers, constant pointers, and pointers to functions. Pointer arithmetic and pointer declarations are also discussed in the document.
This document discusses pointers in C programming. It defines pointers as a data type that contains the memory address of another variable. Pointers allow dynamically allocating memory and reduce program complexity. The document shows how to declare and initialize pointer variables, use pointers to access values, demonstrates pointers to pointers, and uses pointers with arrays. Examples are provided to illustrate pointers, pointer to pointer, and using pointers in arrays and 2D arrays.
Pointer is a variable that stores the address of another variable. Pointers allow indirect referencing of values and are useful for returning multiple values from functions, dynamic memory allocation, and building complex data structures like linked lists. Pointers are declared with a data type followed by an asterisk and are initialized with the address of another variable. The address-of operator & returns the address of its operand, while the indirection operator * accesses the value at the address stored in the pointer. Pointers can reference values indirectly, be passed by reference to functions to modify caller's variables, and implement call by reference parameter passing in C.
Pointer variables allow programmers to indirectly access and manipulate the memory addresses where variables are stored. Pointers must be declared with a data type and initialized by assigning the address of an existing variable using the address-of operator (&). Pointer variables can then be used to read from and write to the memory location of the variable being pointed to using indirection (*). Pointers enable operations like traversing arrays, passing arguments by reference, and dynamically allocating memory. Key pointer concepts covered include declaration, initialization, dereferencing, arithmetic, comparisons, NULL pointers, and their usage with arrays.
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
Pointers are a data type in C that contain memory addresses as their values. They allow programs to indirectly access and manipulate the data stored at those addresses. Pointers can be used to pass arguments by reference, return values from functions, access array elements, and link data structures like linked lists. Proper initialization of pointers is important to avoid issues like accessing protected memory or going out of array bounds.
The document discusses pointers in C/C++. Some key points:
1) A pointer is a variable that stores the address of another variable. Pointers allow accessing and modifying the data being pointed to.
2) The & operator returns the address of a variable. Dereferencing a pointer using * accesses the data being pointed to.
3) Pointer arithmetic allows treating pointers like arrays - incrementing/decrementing a pointer adjusts its address by the size of the pointed-to type.
The document provides an introduction to pointers in C programming. Some key points:
1. Pointers store the address of another variable of the same data type. They allow accessing and modifying values using their memory location rather than their name.
2. Pointers are declared with a data type followed by an asterisk, like int *ptr. They must be initialized with the address of a variable using the & operator before being accessed.
3. The dereference operator * accesses the value at the address stored in the pointer. Pointer arithmetic and increment/decrement allow iterating through arrays using pointers.
4. Pointers enable passing by reference in functions and dynamic memory allocation. They are useful for handling arrays
The document discusses various topics related to arrays and pointers in C programming. It begins by defining arrays as a data structure that stores homogeneous data in a linear sequence. It describes single dimensional, multi-dimensional, and different types of arrays. It then discusses pointers as variables that store the memory addresses of other variables and data types. It explains pointer declaration, initialization, dereferencing, arithmetic operations, and different types of pointers like void pointers and pointer to pointers.
This document discusses arrays and pointers in C programming. It begins by defining arrays as a collection of homogeneous data stored under a common name. It then covers single dimensional arrays, multidimensional arrays, pointers, void pointers, null pointers, and the relationship between arrays and pointers. It provides examples of array declaration, initialization, and accessing elements. It also discusses pointers, dereferencing, pointer arithmetic, arrays of pointers, and pointers to arrays.
Pointer is a variable that stores the address of another variable. The document discusses the syntax and declaration of pointers, initialization of pointers, pointer operators like * and &, void pointers, NULL pointers, pointers to pointers, and the relationship between arrays and pointers. Pointers allow accessing and modifying the value of the variable to which they point. The address of operator & returns the address of a variable, and the indirection operator * accesses the value of the variable at the address stored in the pointer.
Here are the programs to print elements of array and characters in string along with their memory addresses using pointers:
Program 1:
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40};
int *ptr;
ptr = arr;
printf("Elements of array: \n");
for(int i=0; i<4; i++)
{
printf("Element %d: %d Address: %p\n", i, *ptr, ptr);
ptr++;
}
return 0;
}
Program 2:
#include <stdio.h>
int main()
{
char str
Pointer is a variable that stores the address of another variable. It can be declared by specifying the pointer type followed by an asterisk. Common pointer operations include assigning the address of a variable to a pointer, accessing the value at a pointer's address using unary operator *, and pointer arithmetic. Pointers allow accessing array elements by incrementing/decrementing the pointer. They can also be compared using relational operators and passed as arguments to functions.
The document provides information about pointers in the C programming language. It discusses pointer declaration, definition, initialization, dereferencing, arithmetic operations like incrementing and decrementing, and relationships between arrays and pointers. It also covers dynamic memory allocation functions like malloc(), calloc(), free(), and realloc(). Pointers allow accessing and manipulating data in memory and performing tasks like dynamic memory allocation.
Computers use their memory for storing instructions of the programs and the values of the variables. Memory is a sequential collection of storage cells. Each cell has an address associated with it. Whenever we declare a variable, the system allocates, somewhere in the memory, a memory location and a unique address is assigned to this location.c pointers lecture
This document provides an overview of pointers and dynamic arrays in C++. It discusses pointer variables, memory management, pointer arithmetic, array variables as pointers, functions for manipulating strings like strcpy and strcmp, and advanced pointer notation for multi-dimensional arrays. Code examples are provided to demonstrate concepts like passing pointers to functions, dereferencing pointers, pointer arithmetic on arrays, and using string functions. The overall objective is to introduce pointers and how they enable dynamic memory allocation and manipulation of data structures in 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.
Pointer variables store the memory addresses of other variables. They can be used to access and modify the values stored at those addresses. Pointers allow values to be passed by reference rather than by value, enabling changes made within functions to be reflected back in the calling function. Common pointer operations include dereferencing a pointer to access the value at an address, pointer arithmetic to increment or decrement a pointer to other memory locations, and passing pointers as function arguments to allow modification of variable values.
- A pointer is a variable that stores the address of another variable. Pointers allow dynamic memory allocation and access to the value of the variable being pointed to using the indirection operator (*).
- Pointer variables are declared with a data type followed by an asterisk, such as int *ptr. They can be initialized by using the address-of operator (&) to store the address of another variable.
- Pointers can be used to access elements in an array by using pointer arithmetic. An array name itself is a constant pointer to the first element of the array.
Pointers allow programs to store and manipulate memory addresses in C. A pointer is a variable that contains the address of another variable. Pointers can be used to access array elements, pass arguments by reference, and dynamically allocate memory on the heap. There are different types of pointers like void pointers, constant pointers, and pointers to functions. Pointer arithmetic and pointer declarations are also discussed in the document.
This document discusses pointers in C programming. It defines pointers as a data type that contains the memory address of another variable. Pointers allow dynamically allocating memory and reduce program complexity. The document shows how to declare and initialize pointer variables, use pointers to access values, demonstrates pointers to pointers, and uses pointers with arrays. Examples are provided to illustrate pointers, pointer to pointer, and using pointers in arrays and 2D arrays.
Pointer is a variable that stores the address of another variable. Pointers allow indirect referencing of values and are useful for returning multiple values from functions, dynamic memory allocation, and building complex data structures like linked lists. Pointers are declared with a data type followed by an asterisk and are initialized with the address of another variable. The address-of operator & returns the address of its operand, while the indirection operator * accesses the value at the address stored in the pointer. Pointers can reference values indirectly, be passed by reference to functions to modify caller's variables, and implement call by reference parameter passing in C.
Pointer variables allow programmers to indirectly access and manipulate the memory addresses where variables are stored. Pointers must be declared with a data type and initialized by assigning the address of an existing variable using the address-of operator (&). Pointer variables can then be used to read from and write to the memory location of the variable being pointed to using indirection (*). Pointers enable operations like traversing arrays, passing arguments by reference, and dynamically allocating memory. Key pointer concepts covered include declaration, initialization, dereferencing, arithmetic, comparisons, NULL pointers, and their usage with arrays.
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
Pointers are a data type in C that contain memory addresses as their values. They allow programs to indirectly access and manipulate the data stored at those addresses. Pointers can be used to pass arguments by reference, return values from functions, access array elements, and link data structures like linked lists. Proper initialization of pointers is important to avoid issues like accessing protected memory or going out of array bounds.
The document discusses pointers in C/C++. Some key points:
1) A pointer is a variable that stores the address of another variable. Pointers allow accessing and modifying the data being pointed to.
2) The & operator returns the address of a variable. Dereferencing a pointer using * accesses the data being pointed to.
3) Pointer arithmetic allows treating pointers like arrays - incrementing/decrementing a pointer adjusts its address by the size of the pointed-to type.
The document provides an introduction to pointers in C programming. Some key points:
1. Pointers store the address of another variable of the same data type. They allow accessing and modifying values using their memory location rather than their name.
2. Pointers are declared with a data type followed by an asterisk, like int *ptr. They must be initialized with the address of a variable using the & operator before being accessed.
3. The dereference operator * accesses the value at the address stored in the pointer. Pointer arithmetic and increment/decrement allow iterating through arrays using pointers.
4. Pointers enable passing by reference in functions and dynamic memory allocation. They are useful for handling arrays
The document discusses various topics related to arrays and pointers in C programming. It begins by defining arrays as a data structure that stores homogeneous data in a linear sequence. It describes single dimensional, multi-dimensional, and different types of arrays. It then discusses pointers as variables that store the memory addresses of other variables and data types. It explains pointer declaration, initialization, dereferencing, arithmetic operations, and different types of pointers like void pointers and pointer to pointers.
This document discusses arrays and pointers in C programming. It begins by defining arrays as a collection of homogeneous data stored under a common name. It then covers single dimensional arrays, multidimensional arrays, pointers, void pointers, null pointers, and the relationship between arrays and pointers. It provides examples of array declaration, initialization, and accessing elements. It also discusses pointers, dereferencing, pointer arithmetic, arrays of pointers, and pointers to arrays.
Pointer is a variable that stores the address of another variable. The document discusses the syntax and declaration of pointers, initialization of pointers, pointer operators like * and &, void pointers, NULL pointers, pointers to pointers, and the relationship between arrays and pointers. Pointers allow accessing and modifying the value of the variable to which they point. The address of operator & returns the address of a variable, and the indirection operator * accesses the value of the variable at the address stored in the pointer.
Here are the programs to print elements of array and characters in string along with their memory addresses using pointers:
Program 1:
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40};
int *ptr;
ptr = arr;
printf("Elements of array: \n");
for(int i=0; i<4; i++)
{
printf("Element %d: %d Address: %p\n", i, *ptr, ptr);
ptr++;
}
return 0;
}
Program 2:
#include <stdio.h>
int main()
{
char str
Pointer is a variable that stores the address of another variable. It can be declared by specifying the pointer type followed by an asterisk. Common pointer operations include assigning the address of a variable to a pointer, accessing the value at a pointer's address using unary operator *, and pointer arithmetic. Pointers allow accessing array elements by incrementing/decrementing the pointer. They can also be compared using relational operators and passed as arguments to functions.
The document provides information about pointers in the C programming language. It discusses pointer declaration, definition, initialization, dereferencing, arithmetic operations like incrementing and decrementing, and relationships between arrays and pointers. It also covers dynamic memory allocation functions like malloc(), calloc(), free(), and realloc(). Pointers allow accessing and manipulating data in memory and performing tasks like dynamic memory allocation.
Computers use their memory for storing instructions of the programs and the values of the variables. Memory is a sequential collection of storage cells. Each cell has an address associated with it. Whenever we declare a variable, the system allocates, somewhere in the memory, a memory location and a unique address is assigned to this location.c pointers lecture
This document provides an overview of pointers and dynamic arrays in C++. It discusses pointer variables, memory management, pointer arithmetic, array variables as pointers, functions for manipulating strings like strcpy and strcmp, and advanced pointer notation for multi-dimensional arrays. Code examples are provided to demonstrate concepts like passing pointers to functions, dereferencing pointers, pointer arithmetic on arrays, and using string functions. The overall objective is to introduce pointers and how they enable dynamic memory allocation and manipulation of data structures in C++.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
How to Manage Your Lost Opportunities in Odoo 17 CRMCeline George
Odoo 17 CRM allows us to track why we lose sales opportunities with "Lost Reasons." This helps analyze our sales process and identify areas for improvement. Here's how to configure lost reasons in Odoo 17 CRM
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
How to Setup Warehouse & Location in Odoo 17 InventoryCeline George
In this slide, we'll explore how to set up warehouses and locations in Odoo 17 Inventory. This will help us manage our stock effectively, track inventory levels, and streamline warehouse operations.
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...PECB
Denis is a dynamic and results-driven Chief Information Officer (CIO) with a distinguished career spanning information systems analysis and technical project management. With a proven track record of spearheading the design and delivery of cutting-edge Information Management solutions, he has consistently elevated business operations, streamlined reporting functions, and maximized process efficiency.
Certified as an ISO/IEC 27001: Information Security Management Systems (ISMS) Lead Implementer, Data Protection Officer, and Cyber Risks Analyst, Denis brings a heightened focus on data security, privacy, and cyber resilience to every endeavor.
His expertise extends across a diverse spectrum of reporting, database, and web development applications, underpinned by an exceptional grasp of data storage and virtualization technologies. His proficiency in application testing, database administration, and data cleansing ensures seamless execution of complex projects.
What sets Denis apart is his comprehensive understanding of Business and Systems Analysis technologies, honed through involvement in all phases of the Software Development Lifecycle (SDLC). From meticulous requirements gathering to precise analysis, innovative design, rigorous development, thorough testing, and successful implementation, he has consistently delivered exceptional results.
Throughout his career, he has taken on multifaceted roles, from leading technical project management teams to owning solutions that drive operational excellence. His conscientious and proactive approach is unwavering, whether he is working independently or collaboratively within a team. His ability to connect with colleagues on a personal level underscores his commitment to fostering a harmonious and productive workplace environment.
Date: May 29, 2024
Tags: Information Security, ISO/IEC 27001, ISO/IEC 42001, Artificial Intelligence, GDPR
-------------------------------------------------------------------------------
Find out more about ISO training and certification services
Training: ISO/IEC 27001 Information Security Management System - EN | PECB
ISO/IEC 42001 Artificial Intelligence Management System - EN | PECB
General Data Protection Regulation (GDPR) - Training Courses - EN | PECB
Webinars: https://pecb.com/webinars
Article: https://pecb.com/article
-------------------------------------------------------------------------------
For more information about PECB:
Website: https://pecb.com/
LinkedIn: https://www.linkedin.com/company/pecb/
Facebook: https://www.facebook.com/PECBInternational/
Slideshare: http://www.slideshare.net/PECBCERTIFICATION
How to Fix the Import Error in the Odoo 17Celine George
An import error occurs when a program fails to import a module or library, disrupting its execution. In languages like Python, this issue arises when the specified module cannot be found or accessed, hindering the program's functionality. Resolving import errors is crucial for maintaining smooth software operation and uninterrupted development processes.
The simplified electron and muon model, Oscillating Spacetime: The Foundation...RitikBhardwaj56
Discover the Simplified Electron and Muon Model: A New Wave-Based Approach to Understanding Particles delves into a groundbreaking theory that presents electrons and muons as rotating soliton waves within oscillating spacetime. Geared towards students, researchers, and science buffs, this book breaks down complex ideas into simple explanations. It covers topics such as electron waves, temporal dynamics, and the implications of this model on particle physics. With clear illustrations and easy-to-follow explanations, readers will gain a new outlook on the universe's fundamental nature.
2. Pointers
Pointer declaration:
A pointer is a variable that contains the memory location of
another variable. We start by specifying the type of data stored
in the location identified by the pointer. The asterisk tells the
compiler that we are creating a pointer variable. Finally we
give the name of the variable.
The syntax is as shown below.
type *variable_name
Example:
int *ptr;
float *ptr1;
2
3. Address Operator
Once we declare a pointer variable, we must point it to something
we can do this by assigning to the pointer the address of the
variable you want to point as in the following example:
ptr=#
Another Example
int i=3;
• Reserve space in memory to hold the integer value
• Associate the name i with this memory location.
• Store the value 3 at this location
3
6. Indirection Operator
6
Value at address pointer (*)-Returns the
value stored at a particular address
(indirection operator).
Printing the value of *(&i) is same as printing
the value of i.
Source: Let Us C, Yashvant Kanetkar, BPB Publications
8. Pointers
8
• Pointers are variables that contain
addresses and since addresses are
always whole numbers, pointers always
contain whole numbers.
9. Pointers
9
❖float *s
• The declaration float *s does not mean
that s is going to contain a floating-point
value. It means that s is going to contain
the address of a floating-point value.
12. Jargon of Pointers
12
int a=35;
int *b;
b=&a;
• b contains address of an int
• Value at address contained in b is an int
• b is an int pointer
• b points to an int
• b is a pointer which points in direction of
an int.
13. /* Program to display the contents of the variable their address using pointer
variable*/
#include<stdio.h>
void main()
{
int num, *intptr;
float x, *floptr;
char ch, *cptr;
num=123;
x=12.34;
ch='a';
intptr=#
cptr=&ch;
floptr=&x;
printf("Num %d stored at address %un",*intptr,intptr);
printf("Value %f stored at address %un",*floptr,floptr);
printf("Character %c stored at address %un",*cptr,cptr);
}
14. char,int and float pointer
main()
{
char c,*cc;
int i, *ii;
float a,*aa;
c=‘A’ //ASCII value stored in c
i=54;
a=3.14;
cc=&c;ii=&i;aa=&a;
Printf(“address contained in cc = %u", cc);
Printf(“address contained in ii = %u", ii);
Print(“address contained in aa = %u", aa);
printf(“value of c=%c”, *cc);
} 14
15. char,int and float pointer
15
Source: Let Us C, Yashvant Kanetkar, BPB Publications
16. Like other variables pointer variables can be used in expressions.
For example if p1 and p2 are properly declared and initialized pointers, then
the following statements are valid.
y = *p1 * *p2;
sum = sum + *p1;
z = 5 - *p2/*p1;
*p2 = *p2 + 10;
we can also compare pointers by using relational operators the expressions such
as
p1 >p2 , p1==p2 and p1!=p2 are allowed.
Pointer expressions &
pointer arithmetic
17. /*Program to illustrate the pointer expression and pointer arithmetic*/
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr1,*ptr2;
int a,b,x,y,z;
clrscr();
a=30;b=6;
ptr1=&a;
ptr2=&b;
x=*ptr1+ *ptr2-6; /* 30+6-6 =30 */
y=6* (- *ptr1) / *ptr2 + 30; /* 6*(-30)/6+30=0 */
printf("nAddress of a %u",ptr1);
printf("nAddress of b %u",ptr2);
printf("na=%d, b=%d",a,b);
printf("nx=%d,y=%d",x,y);
ptr1=ptr1 + 70;
printf("na=%d, b=%d",a,b);
printf("nAddress of a %u",ptr1);
getch();
}
18. Passing address to functions
Arguments are generally passed to functions
in one of the two ways
• Sending the value of the arguments
• Sending the addresses of the arguments
18
19. Sending the values of the
arguments
• ‘value’ of each actual argument in the
calling function is copied into
corresponding formal argument of the
called function.
• Changes made to the formal arguments in
the called function have no effect on the
values of actual arguments in the calling
function.
19
20. Call by value
main()
{
int a =10, b=20;
Swap(a,b);
Printf(“na=%d”,a);
Printf(“n b=%d”,b);
}
Swap(int x, int y)
{
int t;
t=x;
x=y;y=t;
printf(“nx=%d“, x);
printf (“ny = %d",y );
} 20
21. Call by reference
• The address of actual arguments in the
calling function are copied into formal
arguments of the called function.
• Using formal arguments in the called
function changes can be made in the
actual arguments of the calling function.
21
23. Call by reference
• Using call by reference function can return
more than one value at a time.
main()
{int radius; float area, perimeter;
Printf(“Enter the radius of a circle”);
scanf(“%d”,&radius);
Areaperi(radius,&area,&perimeter);
printf(%f %f,area,perimeter);
}
Areaperi (int r,float *a,float *p)
{
*a= 3.14*r*r;
*p= 2*3.14*r;
}
23
24. Functions Returning Pointers
main()
{
Int *p;
Int *fun(); // prototype declaration
p=fun();
printf(“n%u”,p);
printf(“n%d”,*p);
}
Int *fun() //function definition
{
int i=20;
return(&i);
}
24
25. *ptr++ and ++*ptr
• *ptr++ increments the pointer not the value
pointed by it.
• ++*ptr increments the value being pointed
to by ptr.
25
30. Pointers and Arrays
• Do not attempt the following operations on
Pointers
Addition of two pointers
Multiplying pointer with a number
Dividing a pointer with a number
int i=4,*j,*k;
j=&i;
j=j+1,j=j-5; //Allowed
30
31. Accessing Array Elements
main()
{
int num[]={24,34,12,44,56,17};
int i=0;
while(i<=5)
{
printf(“n address is %u”,&num[i]);
printf(“element=%d”,num[i]);
i++;
}
}
31
32. Accessing Array Elements-
using pointer
main()
{
int num[]={24,34,12,44,56,17};
int i=0, *j;
j=&num[0];
while(i<=5)
{
printf(“n address is %u”,&num[i]);
printf(“element=%d”,*j);
i++;
j++; // increment pointer to the next location
}
}
32
33. Passing an Entire Array to a
function
main()
{
int num[]={24,34,12,44,56,17};
display(&num[0],6); // or display(num,6);
}
display( int *j, int n)
{ int i=1;
While(i<=n)
{
printf(“element=%d”,*j);
i++;
j++; // increment pointer to point the next location
}
33
34. Accessing array element in
different ways
34
Source: Let Us C, Yashvant Kanetkar, BPB Publications
36. 2-D Array using Pointer
36
Source: Let Us C, Yashvant Kanetkar, BPB Publications
37. Pointer to an array
• As we have pointer to int,float and char we
can have pointer to an array
• For example int (*q)[4] means that q is a
pointer to an array of 4 integers
37
38. Pointer to an array
38
Source: Let Us C, Yashvant Kanetkar, BPB Publications
39. Passing 2-D array to a function
39
Source: Let Us C, Yashvant Kanetkar, BPB Publications
40. Passing 2-D array to a function
40
Source: Let Us C, Yashvant Kanetkar, BPB Publications
41. Returning Array from Function
• A pointer to an integer
• A pointer to the zeroth 1-D array
• A pointer to the 2-D array
41
42. Array of Pointers
• A pointer variable always contains an
address, an array of pointers would be
nothing but collection of addresses.
Main()
{
Static int a[]={0,1,2,34};
Static int *p[]={a,a+1,a+2,a+3,a+4};
printf(“n%u%u%d”,p,*p,*(*p));
}
42
43. Dynamic Menory Allocation
• If we want to allocate memory at the time of
excecution then it can be done using standard library
functions calloc() and malloc() and are known as
dynamic memory allocation functions.
• In C, malloc is a memory allocation function that
dynamically allocates a large block of memory with a
specified size. It's useful when you don't know the
amount of memory needed during compile time.
• The calloc() function in C is used to allocate multiple
blocks of memory that are the same size. It's a
dynamic memory allocation function that sets each
block to zero. calloc() stands for "contiguous
allocation". 43
44. Example
int main()
{
int n,avg,i,*p,sum=0;
printf("nEnter the number of students");
scanf("%d",&n);
p=(int *)malloc(n*2);
if(p==NULL)
{
printf("Memory allocation unsuccessful");
exit(0);
}
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
{
sum=sum+*(p+i);
}
avg=sum/n;
printf("Avarage marks=%d",avg);
return 0;
} 44
46. References
• Programming in ANSI C, E. Balagurusamy. McGrawHill
• Let Us C, Yashvant Kanetkar, BPB Publications
• Programming in C, Reema Thareja, Oxford University Press
• https://www.dotnettricks.com
46