
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to create a pointer for strings using C language?
Arrays of pointers (to strings)
Array of pointers is an array whose elements are pointers to the base address of the string.
It is declared and initialized as follows −
char *a[3 ] = {"one", "two", "three"}; //Here, a[0] is a ptr to the base add of the string "one" //a[1] is a ptr to the base add of the string "two" //a[2] is a ptr to the base add of the string "three"
Advantages
Unlink the two-dimensional array of characters. In (array of strings), in array of pointers to strings there is no fixed memory size for storage.
The strings occupy as many bytes as required hence, there is no wastage of space.
Example 1
#include<stdio.h> main (){ char *a[5] = {“one”, “two”, “three”, “four”, “five”};//declaring array of pointers to string at compile time int i; printf ( “The strings are:”) for (i=0; i<5; i++) printf (“%s”, a[i]); //printing array of strings getch (); }
Output
The strings are: one two three four five
Example 2
Consider another example on array of pointers for strings −
#include <stdio.h> #include <String.h> int main(){ //initializing the pointer string array char *students[]={"bhanu","ramu","hari","pinky",}; int i,j,a; printf("The names of students are:
"); for(i=0 ;i<4 ;i++ ) printf("%s
",students[i]); return 0; }
Output
The names of students are: bhanu ramu hari pinky
- Related Articles
- How to define pointer to pointer in C language?
- Explain the concept of pointer to pointer and void pointer in C language?
- How to access the pointer to structure in C language?
- Explain the Union to pointer in C language
- Differentiate the NULL pointer with Void pointer in C language
- Strings in C Language
- What is void pointer in C language?
- How to assign a pointer to function using C program?
- What do you mean by pointer to a constant in C language?
- How to access array elements using a pointer in C#?
- How to create a customized atoi() function in C language?
- What are the input and output for strings in C language?
- Explain the concept of pointer accessing in C language
- How to find a leap year using C language?
- Explain the dynamic memory allocation of pointer to structure in C language

Advertisements