Pointer with Strings in C

Preview
You must first complete Pointer with Array in C before viewing this Lesson

Pointer with Strings in C

A string variable can be declared using pointer as:

char *name;

name is a string variable that can hold any number of characters in it and there is no wastage of space as size will be automatically adjusted  depending upon the number of characters stored in this string variable.

Program of string with pointer.
#include<stdio.h>
int main()
{
char *name;
name=”Amit”;
printf(“\n%s”,name);
 

return(0);
}

Output
Amit




Pointer and Array of Strings

Array of strings is actually a two dimensional array of char type.
We can declaration array of strings using pointer as:
char *snames[10];
Here snames is an array of strings that can store 10 string values each of which have not limit on size .

Program of strings using pointer.
#include<stdio.h>
int main()
{
char *snames[5]={“Rohit”,”Ajit”,”Sunil”,”Ayush”,”Sohan”};
int i;
printf(“\nValues are:=”);
for(i=0;i<5;i++)
printf(“\n%s”,snames[i]);

 

return(0);
}

Output
Values are:=
Rohit
Ajit
Sunil
Ayush

Sohan

Best Books of C





Lesson tags: array of strings using pointer, handling strings using pointer, program to use string using pointer, string type pointer variable
Back to: C Programming Language