0% found this document useful (0 votes)
7 views10 pages

Understanding Pointers in C Programming

The document provides an overview of pointers in C programming, explaining their role in storing memory addresses, dereferencing, and their use in function arguments and arrays. It includes examples of pointer usage, address arithmetic, and character pointers for string manipulation. Additionally, it highlights the concept of pass-by-reference and demonstrates string length calculation using character pointers.

Uploaded by

charantejamadiya
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)
7 views10 pages

Understanding Pointers in C Programming

The document provides an overview of pointers in C programming, explaining their role in storing memory addresses, dereferencing, and their use in function arguments and arrays. It includes examples of pointer usage, address arithmetic, and character pointers for string manipulation. Additionally, it highlights the concept of pass-by-reference and demonstrates string length calculation using character pointers.

Uploaded by

charantejamadiya
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

Welcome to

GITAM

Dept of CSE, GIT


Pointers and Addresses
 A pointer is a variable that stores the memory address of another variable. Instead of
holding a direct value like an integer or a character, a pointer "points to" the location in
memory where the actual value resides.
 The address operator & gives the memory address of a variable. When you use & before a
variable, it returns the address where that variable is stored in memory.
 The dereference operator * is used to access the value stored at the address the pointer is
pointing to. When you declare a pointer, * is used to denote that the variable is a pointer,
and when you dereference it, * gives you the value at the address.

Dept of CSE, GIT


Pointers and Addresses
Example:
#include <stdio.h>
int main() {
int num = 42;
int *ptr = &num;
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", (void*)&num);
printf("Value of ptr (address of num): %p\n", (void*)ptr);
printf("Value pointed to by ptr: %d\n", *ptr);
*ptr = 58;
printf("New value of num: %d\n", num);
return 0;
Dept of CSE, GIT
Pointers and Function arguments
 When using pointers as function arguments, functions can directly modify the values of
variables in the calling function by passing the memory addresses of those variables.

 This is often referred to as pass-by-reference, as opposed to pass-by-value, where only a


copy of the value is passed.

 Example: Swapping of two numbers using call by reference

Dept of CSE, GIT


Pointers and Arrays
 An array in C is a collection of elements of the same data type stored in contiguous
memory locations.

 Arrays allow for easy manipulation of multiple values under a single variable name.

Array Declaration: int arr[5]; // Declares an array of 5 integers

1-D Array Initialization (static):


int arr[5] = {1, 2, 3, 4, 5}; // Initializes the array with specific values

NOTE: If you don’t initialize all elements, the remaining elements will be set to zero by
default.

 int arr[5] = {1, 2}; // Initializes arr[0] to 1, arr[1] to 2, and the rest to 0

Dept of CSE, GIT


Pointers and Arrays
Example:

#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Initializing an array
int *ptr = arr; // Pointer to the first element of the array
printf("Accessing array elements using pointer:\n");
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i)); // *(ptr + i) is equivalent to arr[i]
}
return 0;
}

Try with dynamic initialization also


Dept of CSE, GIT
Address Arithmetic
 You can use pointers to access and manipulate elements in an array.
For example:
*(arr + i) is equivalent to arr[i].
arr + i gives the address of the ith element.

 When you increment a pointer, it moves to the next element based on the data type's size.

 For example, if arr is an int array, arr + 1 will point to the next integer in memory.

TASK:

Program to find the sum of the elements of the array using pointers.

Dept of CSE, GIT


Character Pointers and Functions
 In C, character pointers are frequently used with strings and functions to efficiently
manipulate and pass string data.

 A character pointer (char*) can point to a single character or to a string (a sequence of


characters ending in a null character '\0’).

 In C, strings are typically represented as arrays of characters. Instead of passing the


whole array to a function, you can pass a character pointer, which points to the first
character in the string.

 When you pass a char* to a function, the function can modify the original string if it's not
a constant.

 Using const char* indicates that the function should not modify the string, providing
read-only access to the characters.
Dept of CSE, GIT
Character Pointers and Functions
 // String Length Calculation Using a Character Pointer
#include<stdio.h>

int stringLength(const char *str)


{
int length = 0;
while (*str != '\0')
{
length++;
str++;
}
return length;
}

int main() {
const char *message = "Hello, World!";
int length = stringLength(message);
printf("The length of the string \"%s\" is: %d\n", message, length);
return 0;
}
Dept of CSE, GIT
Thank You

Dept of CSE, GIT

You might also like