EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Strings Array in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Strings Array in C

Strings Array in C

What is an Array of String?

The string is a collection of characters, an array of a string is an array of arrays of characters. Each string is terminated with a null character. An array of a string is one of the most common applications of two-dimensional arrays.

scanf( ) is the input function with %s format specifier to read a string as input from the terminal. But the drawback is it terminates as soon as it encounters the space. To avoid this gets( ) function which can read any number of strings including white spaces.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Sting is an array of characters terminated with the special character known as the null character (“\0”).

Syntax

The syntax for array of string is as follows:

Syntax

datatype name_of_the_array[size_of_elements_in_array];
char str_name[size];

Example

datatype name_of_the_array [ ] = { Elements of array };
char str_name[8] = “Strings”;

Str_name is the string name and the size defines the length of the string (number of characters).

A String can be defined as a one-dimensional array of characters, so an array of strings is two –dimensional array of characters.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Syntax

char str_name[size][max];

Syntax

char str_arr[2][6] = { {‘g’,’o’,’u’,’r’,’i’,’\0’}, {‘r’,’ a’,’ m’,’\0’}};

Alternatively, we can even declare it as

Syntax

char str_arr[2][6] ={“gouri”, ”ram”};

From the given syntax there are two subscripts first one is for how many strings to declare and the second is to define the maximum length of characters that each string can store including the null character. C concept already explains that each character takes 1 byte of data while allocating memory, the above example of syntax occupies 2 * 6 =12 bytes of memory. 

Example 

char str_name[8] = {‘s’,’t’,’r’,’i’,’n’,’g’,’s’,’\0’};
By the rule of initialization of array, the above declaration can be written as
char str_name[] = “Strings”;

0 1 2 3 4 5 6 7 Index

Variables 2000 2001 2002 2003 2004 2005 2006 2007 Address

This is a representation of how strings are allocated in memory for the above-declared string in C.

Each character in the string is having an index and address allocated to each character in the string. In the above representation, the null character (“\0”) is automatically placed by the C compiler at the end of every string when it initializes the above-declared array. Usually, strings are declared using double quotes as per the rules of strings initialization and when the compiler encounters double quotes it automatically appends null character at the end of the string.

From the above example as we know that the name of the array points to the 0th index and address 2000 as we already know the indexing of an array starts from 0. Therefore,

str_name + 0 points to the character “s”
str_name + 1 points to the character “t”

As the above example is for one-dimensional array so the pointer points to each character of the string.

Examples of Array String in C

Following are the examples:

Example:

#include <stdio.h>
int main()
{
char name[10];
printf("Enter the name: ");
fgets(name, sizeof(name), stdin);
printf("Name is : ");
puts(name);
return 0;
}

Output:

string array in c

Now for two-dimensional arrays, we have the following syntax and memory allocation. For this, we can take it as row and column representation (table format).

char str_name[size][max];

In this table representation, each row (first subscript) defines as the number of strings to be stored and column (second subscript) defines the maximum length of the strings.

char str_arr[2][6] = { {‘g’,’o’,’u’,’r’,’i’,’\0’}, {‘r’,’ a’,’ m’,’\0’}};

Alternatively, we can even declare it as

Syntax:

char str_arr[2][8] ={“gouri”, ”ram”};

Index

Rows

0 1 2 3 4 5 6 7
0 g o u r i \0 \0 \0
1 r a m \0 \0 \0 \0 \0

From the above example as we know that the name of the array points to the 0th string. Therefore,

str_name + 0 points to 0th string “gouri”

str_name + 1 points to 1st string “ram”

As the above example is for two-dimensional arrays so the pointer points to each string of the array.

Example:

#include <stdio.h>
int main()
{
int i;
char name[2][8] = {
“gouri”,
“ram”
};
for (i = 0; i < 2; i++)
{
printf(“String = %s \n”, name + i, name + i);
}
return 0;
}

Output:

string array in c

Functions of strings

strcpy(s1,s2); this function copies string s2 innto sting s1.
char s1[10] = “gouri”;
char s2 [10] = “ram”;
char s3 [10] ;
strcpy(s3,s2);
result => strcpy(s3,s2) : ram

strcat(s1,s2); this function concatenates strings s1 and s2 , string s2 is appended at the end of the string s1.
char s1[10] = “gouri”;
char s2 [10] = “ram”;
strcat(s1,s2);
result => strcat(s1,s2) : gouriram

strlen(s1); this function returns the length of the string s1.
char s1[10] = “gouri”;
strlen(s1);
result => 5

strcmp(s1,s2); This function compares both strings s1 and s2.
style="list-style-type: none;">
style="list-style-type: none;">

strchr(s1, ch); these functions find the first occurrence of the given character ch in the string s1 and the pointer points to this character in the string.

strstr(s1,s2); this finds the first occurrence of string s2 in the string s1 and the pointer points to the string s2 in the string s1.

With some invalid operations are str_arr[0] = “gouri”; in this operation pointer of the string is assigned to the constant pointer which is invalid and is not possible, because the name of the array is a constant pointer.

To avoid this we can assign str_arr by using strcpy(str_arr[0],”gouri”).

Conclusion

An array itself defines as a list of strings. From the above introduction, we can conclude that declaration and initialization of strings are different as we saw for every string the compiler appends null character when it reads the string as input. There are many string handling functions a few functions with examples are explained above. Therefore arrays of the string are as easy as arrays.

Recommended Articles

This is a guide to a Strings Array in C. Here we discuss the basics of the Array Strings, Example of Array String in C and Functions of strings. You can also go through our other suggested articles to learn more–

  1. String Array in C#
  2. Multidimensional Array in C
  3. 2D Arrays in C#
  4. String in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More