0% found this document useful (0 votes)
5 views

Pointers

Uploaded by

ramesh.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Pointers

Uploaded by

ramesh.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Pointers

Pointers
• Pointers are one of the most powerful features in C, allowing direct
memory access, manipulation of data, and efficient handling of arrays
and dynamic memory
• Definition: pointer is a variableintthat
*p; stores the memory address of
another variable.
Syntax for declaring a Pointer
int *p;
Here, p is a pointer to an integer.
The * indicates that the variable p is a pointer, not a regular integer.
Declaring and Initializing a
Pointer
• To declare a pointer, use the * symbol to indicate that the variable is a
pointer.
• To assign a value to the pointer, use the address-of operator (&).
int a = 10;
int *p;
p = &a; // p now stores the memory address of 'a‘

&a gets the memory address of the variable a.


Dereferencing a Pointer
• Dereferencing means accessing the value at the memory address
stored in the pointer.
• To dereference a pointer, use the * operator.
• printf("%d", *p); // This prints the value stored at the memory address 'p‘
• Here, *p gives you the value of a (which is 10), because p holds the address of a.
Pointer Arithmetic
• You can perform arithmetic operations on pointers to navigate
through memory.
• For example, incrementing a pointer moves it to the next memory
location of the same type.
int arr[] = {10, 20, 30};
int *p = arr; // p points to arr[0]
p++; // p now points to arr[1]
printf("%d", *p); // This prints 20
Summary of Pointer Operations

• & (address-of) operator: To get the address of a variable.


• * (dereference) operator: To get the value at the address a pointer
is pointing to.
• ++, --, +, -: Pointer arithmetic to navigate through memory.
• NULL: Special value indicating an uninitialized or invalid pointer.
Pointers and Arrays
• In C, arrays and pointers are closely related. The name of an array is
essentially a pointer to its first element.
int arr[] = {1, 2, 3};
int *p = arr; // p points to the first element of arr
printf("%d", *p); // Prints 1
p++;
printf("%d", *p); // Prints 2
Null Pointers
• A pointer can be assigned the value NULL, which means it is not
pointing to any valid memory location.
• Using a NULL pointer in dereferencing will cause a runtime error
(segmentation fault).
• int *p = NULL;// Dereferencing NULL pointer will result in an error://
printf("%d", *p); // Dangerous!
Pointers to Pointers
• A pointer can also point to another pointer, creating a chain of
pointers.
int a = 10;
int *p = &a; // p holds the address of a
int **pp = &p; // pp holds the address of p
printf("%d", **pp); // This prints the value of a (10)
Function Pointers
• A function pointer is a pointer that points to a function instead of a variable. It allows dynamic
function calling.
void greet()
{
printf("Hello, World!");
}

int main()
{
void (*func_ptr)() = greet; // Function pointer initialization
func_ptr(); // Call function using pointer
return 0;
}
Dynamic Memory Allocation
• Pointers are essential for dynamic memory management (e.g., malloc,
calloc, free).
• With pointers, you can allocate memory during runtime and manage
it manually.
int *p = (int*)malloc(sizeof(int)); // Allocating memory dynamically
*p = 10; // Storing a value in dynamically allocated memory
printf("%d", *p); // Prints 10
free(p); // Free allocated memory
Pointers and Structures
You can use pointers to work with structures, making it easier to
manipulate and pass structures to functions.

struct Person {
char name[50];
int age;
};
struct Person *p;
struct Person person = {"John", 25};
p = &person; // p points to person structure
printf("%s is %d years old.", p->name, p->age); // Access fields via pointer
Common Mistakes with Pointers

• Dereferencing uninitialized pointers.


• Dereferencing NULL pointers.
• Memory leaks when dynamically allocated memory is not freed.
• Incorrect pointer arithmetic causing out-of-bounds access.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy