i) Character and Pointer ii) String and pointers
POINTERS AND STRINGS
A
character pointer is a pointer to the character. It can be declared
char
*pointer_character;
A
character pointer can also be used to access character arrays and string
constants, in the same way as accessing numeric arrays using their respective
pointer variable. A character type pointer variable can be assigned an entire
string as part of a variable declarations whereas a numerical pointer variable
cannot be intialized in the same manner as a numerical array.
A
string can conveniently be represented by either using a one‒dimensional
character array as a character pointer. A string constant is accessed by a
pointer to its first element. If pointer message is declared as
char
*pointers message;
The
statement pointer message = "I am a student". Allocates memory
and stores the character string and then assigns to pointer message a pointer
containing the starting address of the character array. It is important to note
that there is difference between the following two definitions.
char
array message[] = "I am a student”
→ array
char
*pointer message[ ] ="I am a student"
→ Pointer
Array
message is big enough to hold the sequence of character and '\0' that
initializes it. Individual characters within the array can be changed but array
message always refers to the same storage. On the other hand, pointer message
initialized to print to a string constant but the pointer may sub‒sequently be
modified to point elsewhere.The figure shows the how the weekdays are defined.

Thus,
though both array message and pointer message are pointers (store the address),
array message is a constant where as pointer message is a variable pointer.
Example 1: Write a
program to count number of words.
/* Program to count number of words */
#include <stdio.h>
main()
{
char str[100], *ps;
int nword = 0;
printf ("\n
Enter a string");
ps=str;
gets(ps);
while (*ps==32
&& *ps!=Null)
ps++;
if (*ps!=null)
nword++;
for (ps=str;
*ps!=NULL; ps++)
{
if
(*ps!=32 && *(ps‒1)==32)
nword++;
}
printf("%d word(s) in the given
string", nword);
}
OUTPUT
Enter a string
I am a student.
4 word(s) in the given
string.
EXPLANATION:
In the above program str[] declared as a character type array. Variable ps
declared as pointer variable. Read the string ps from the terminal. The while
loop checks the condition *ps=32
&& *ps!=Null if condition satisfies then increment the variable ps
by 1. if statement checks the conditions *ps!=Null
and executes the block of statement then prints the string nword.
Example 2: Program to
demonstrate on strings and pointers.
/* Program to demonstrate strings and pointers */
#include <stdio.h>
#include <conio.h>
void main()
{
/* Local definitions */
char * sl = "abcd";
char s2[] = "efgh";
*Statements */
clrscr();
printf("%s %16lu \n", s1, s1);
printf("%s %16lu \n", s2, s2);
sl = s2;
printf("%s %16lu \n", s1, s1);
printf("%s %16lu \n", s2, s2);
getch();
} /* main */
OUTPUT
abcd 58458287
efgh 58523632
efgh 58523632
efgh 58523632
EXPLANATION:
The above program refers the strings and pointers concept. String s1 and s2 are
declared as character type. The program prints the string as given as output.
Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Pointers and Strings
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation