CREATED BY K. VICTOR BABU
COMPUTATIONAL THINKING FOR
STRUCTURED DESIGN
Topic:
CHARACTER POINTERS
Department of BES-1
Mr. B. ASHOK
Assistant Professor
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
AIM OF THE SESSION
To familiarize the students with Pointers and Character Pointers.
INSTRUCTIONAL OBJECTIVES
This Session is designed to:
1. Know about pointers and their benefits
2. Learn about pointer initialization and memory allocation
3. Create character pointers and access strings by using them
4. Understand the difference between character pointers and character arrays
5. Learn String handling using character pointers
LEARNING OUTCOMES
At the end of this session, you should be able to:
1. Learn how to declare and initialize the pointers
2. Create character pointers
3. Know the difference between character pointers and character arrays
4. Perform string handling by using character pointers
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
Outline
• Understanding Pointers
• Benefits of using pointers
• Create a character pointer to a string
• How to use pointers to store the value of string in C program
• Differences between character pointer and character array
• Use character pointers to perform various string handling functions
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
Understanding Pointers
• Pointer is a variable that holds the memory address of another variable of same type.
• It provides a way to directly access and manipulate memory locations, which can be
useful for tasks such as dynamic memory allocation, efficient data manipulation, and
passing parameters by reference.
Syntax:
datatype *pointer_variable_name;
Example:
int *pnum;
char *pchar;
float *pfloat;
KLEF CTSD BES-1
CREATED BY K. VICTOR BABU
10
x
ptr
2000
Name of the cell(Variable)
Value stored in cell (Data/Information)
Address of Variable x
9000
Name of the cell(Variable)
Value stored in cell (Data/Information)
ptr=&x
Address of Variable ptr
KLEF CTSD BES-1
2000
Understanding Pointers
CREATED BY K. VICTOR BABU
KLEF CTSD BES-1
Benefits of using pointers in C
1.Pointers enable efficient passing of arrays and strings to functions.
2.Pointers allow returning multiple values from a function.
3.Pointers can help simplify and shorten the length of a program.
4.Pointers can improve processing speed.
5.Pointers save memory.
CREATED BY K. VICTOR BABU
• In C language, character pointers are used to store the memory address of a
character or a sequence of characters.
• A character pointer can be declared using the following
Syntax:
char *ptr_var_name;
• A character pointer can also be used to declare and initialize a string literal.
Ex: char *str = "Hello, world!";
KLEF CTSD BES-1
Character Pointers
CREATED BY K. VICTOR BABU
KLEF CTSD BES-1
Initialization of Character Pointer
Variables
A character pointer variables can be initialized in the following ways
1. String Literal Initialization:
char *ptr = "Hello, World!";
2. Array Initialization:
char arr[] = "Hello";
char *ptr = arr;
3. Dynamic Memory Allocation:
char *ptr = (char *)malloc(sizeof(char) * (length + 1));
CREATED BY K. VICTOR BABU
KLEF CTSD BES-1
Creating Character Pointer & Accessing the
String
The below examples demonstrate creating character pointers and accessing strings
using them in C
#include <stdio.h>
int main() {
char *str = "Hello, World!";
printf("%sn", str);
return 0;
}
#include <stdio.h>
int main() {
char *str = "Hello";
while (*str != '0') {
printf("%c ", *str);
str++;
}
return 0;
}
Hello
Hello, World!
CREATED BY K. VICTOR BABU
KLEF CTSD BES-1
Character Pointer Vs Character Array
Character Pointer Character Array
A pointer is a variable that holds the
memory address of a character or a
sequence of characters.
An array is a collection of contiguous
memory locations that hold a sequence of
characters.
Can be dynamically assigned to point to
different strings or memory locations.
Once declared, the size and content of the
array are fixed.
Memory allocation can be dynamic,
allowing flexibility in size and content.
Memory is allocated statically and cannot
be resized.
Can be reassigned to point to different
strings or memory locations.
The content of the array can be modified,
but the size remains constant.
Accessed using pointer arithmetic and
dereferencing.
Accessed using array indexing.
CREATED BY K. VICTOR BABU
KLEF CTSD BES-1
Character Pointer Vs Character Array
#include <stdio.h>
int main() {
char *str = "Hello, World!";
printf("%sn", str);
str = "New String";
printf("%sn", str);
return 0;
}
#include <stdio.h>
int main() {
char arr[] = "Hello, World!";
printf("%sn", arr);
arr[7] = '-';
printf("%sn", arr);
return 0;
}
Hello, World!
New String
Hello, World!
Hello, -orld!
CREATED BY K. VICTOR BABU
KLEF CTSD BES-1
String handling using Character Pointer
• Character pointers can be used to manipulate strings and character arrays.
For example, strcpy() function can be used to copy a string from one character
array to another using pointers:
char source[] = "stay focused";
char destination[50];
char *ptr = source;
strcpy(destination, ptr);
printf("%sn", destination);
stay focused
CREATED BY K. VICTOR BABU
Summary
KLEF CTSD BES-1
1. Character pointers in C are variables that store memory addresses of characters
or strings, enabling efficient string manipulation and memory management.
2. They allow for dynamic memory allocation, providing flexibility in assigning
and reassigning memory locations as needed.
3. Character pointers are accessed using pointer arithmetic and dereferencing,
facilitating efficient traversal and manipulation of strings.
CREATED BY K. VICTOR BABU
THANK YOU
Team – CTSD
KLEF CTSD BES-1