0% found this document useful (0 votes)
47 views4 pages

UNIT 2 C Prog

This document provides an overview of pointers in programming, defining them as variables that store memory addresses of other variables. It covers the declaration, initialization, and usage of pointers, including the indirect and address-of operators, pointer arithmetic, dynamic memory allocation, and their relationship with arrays and functions. Examples are provided to illustrate each concept, demonstrating how pointers can be used to manipulate data in memory.

Uploaded by

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

UNIT 2 C Prog

This document provides an overview of pointers in programming, defining them as variables that store memory addresses of other variables. It covers the declaration, initialization, and usage of pointers, including the indirect and address-of operators, pointer arithmetic, dynamic memory allocation, and their relationship with arrays and functions. Examples are provided to illustrate each concept, demonstrating how pointers can be used to manipulate data in memory.

Uploaded by

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

Unit – II: Pointers

1. Defini on and Declara on of Pointers

What is a Pointer?

A pointer is a special variable that stores the memory address of another variable.

 It "points to" the loca on in memory where a value is stored.

Declara on of Pointer

To declare a pointer, use the * symbol before the pointer name.

Syntax:

data_type *pointer_name;

Example:

int *p; // p is a pointer to an integer

Here, p is a pointer that can store the address of an integer variable.

2. Ini aliza on of Pointers

Once a pointer is declared, you can assign the address of a variable to it using the & (address of)
operator.

Example:

int x = 10;

int *p;

p = &x; // p now holds the address of variable x

Now, p points to x.

3. Indirec on Operator (*)

The indirec on operator * is used to access the value stored at the memory loca on a pointer is
poin ng to.

Example:

int x = 20;

int *p = &x;
prin ("%d", *p); // Output: 20

Here, *p means “value at address stored in p” (which is the value of x).

4. Address of Operator (&)

The address-of operator & is used to get the memory address of a variable.

Example:

int x = 15;

prin ("%p", &x); // Output: memory address of x

This &x gives the address of variable x.

5. Pointer Arithme c

You can perform arithme c opera ons on pointers such as:

 p++ (move to next memory loca on)

 p-- (move to previous memory loca on)

 p + n (jump n elements forward)

 p - n (jump n elements backward)

Example:

int arr[3] = {10, 20, 30};

int *p = arr;

prin ("%d", *(p + 1)); // Output: 20

Pointer arithme c works based on the data type size.

6. Dynamic Memory Alloca on

Dynamic memory alloca on allows you to allocate memory at run me using pointers.

In C:

 malloc() – allocate memory

 calloc() – allocate and ini alize memory

 realloc() – resize memory

 free() – free memory


Example:

int *p;

p = (int *)malloc(sizeof(int)); // allocate memory for one int

*p = 50;

prin ("%d", *p); // Output: 50

free(p); // release memory

7. Arrays and Pointers

An array name is actually a pointer to the first element.

Example:

int arr[3] = {5, 10, 15};

int *p = arr;

prin ("%d", *(p + 1)); // Output: 10

Here, arr and p both point to the same memory.

8. Func ons and Pointers

You can pass pointers to func ons for:

 Call by reference (so the original value can be modified)

 Dynamic memory use

Example: Passing pointer to func on

void update(int *p) {

*p = *p + 5;

int main() {

int x = 10;

update(&x);

prin ("%d", x); // Output: 15

You might also like