Pointer with Strings in C Language | String pointer in C Language

You must first complete for statement in c language | Nesting of for in c language | for loop in C before viewing this Lesson

Pointer with Strings in C Language | String pointer in C Language

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 in C Language

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



 

Spread the love
Lesson tags: array of strings using pointer, handling strings using pointer, Pointer and Array of Strings in C Language, program to use string using pointer, string type pointer variable
Back to: C Programming Language
Spread the love