Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The difference between char s[] and char *s
3.
Examples
4.
Frequently Asked Questions
4.1.
What does char* [] mean in C?
4.2.
Is char* the same as string?
4.3.
What is the difference between char and char*?
4.4.
What is a character array?
4.5.
What is the difference between Strcat and Strncat?
4.6.
Which argument does the strcat() function take?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

What’s the difference between char s[] and char *s in C

Author yuvatimankar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In C, both char s[] and char *s are used to create strings; now, we want to find the difference between them. 

The char s[] is an array, whereas *s is a pointer. In an array, the total string is stored in the stack section, whereas for the pointer char *s, the pointer variable is stored in the stack section, and the content is stored in the code section. 

The crucial difference between char s[] and char *s that are array and pointer, respectively, is how you interact with them after creating them. The essential difference between char s[] the array and char *s the pointer is that in one *s, you are assigning it to the pointer, whereas in an s[], you are assigning the values to an array.

Also see : C Static Function and Short int in C Programming

The difference between char s[] and char *s

This section will discuss some of the key differences between char s[] and char *s.

char s[10]= “abcd”

char *s= “stuv”

s[] is an array *s is a pointer
sizeof(s) = 10 bytes sizeof(*s) = 4 bytes
‘abcd’ is stored in stack section of memory ‘stuv’ is stored in code section of memory, whereas *s is stored in a stack section of memory

char s[10] = “abcd”

a= “hello”; //  This code is invalid as s is an address and string constant is also an address, so it is impossible.  

char *s = “stuv”;

s=” Ninja”; // This code is valid

 

s++ in invalid in array s++ is valid in pointer

char s[10] = “abcd”;

S[0] = ‘b’ // This code is valid.

char *s = “stuv”;

s[0] =”r” // This code is invalid

 

We can edit the elements in s[] array We cannot edit the elements in *s pointer

You can also read about the dynamic arrays in c, and  Tribonacci Series

Must Read Passing Arrays to Function in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples

This section will discuss some of the examples showing the characteristics of char s[] and char *s.

Code 1

Here in this code we are printing char s[] and char *p.

#include<stdio.h>

int main()
{
    char a[10] = "abcd"; 
    char *p = a; 
    printf("a = %s\n", a);     
    printf("p = %s", p);
}

 

Output:

a = abcd
p = abcd

 

Code 2

Here in the below code, we are printing the values of char s[] and char *p.

#include<stdio.h>

int main()
{
    char a[10] = "abcd"; 
    char *p = "stuv"; 
    printf("s = %s\n", a);     
    printf("p = %s", p);
}

 

Output:

s = abcd
p = stuv

 

Code 3

Here in the below code, we have a char *s in which we have declared the value Hello Ninja! , Now we are trying to add a letter at position 6. As char *s cannot edit the value it will give an error.

#include<stdio.h>

int main() {
    char *s = "Hello Ninja";
    s[6] = 'x'; //tried to edit letter at position 6
    printf("%s", s);
}

 

Error:

Segmentation fault

 

(As pointer cannot edit their elements)

Also Read, loop and while loop

 

Code 4

Here in the below code, we are trying to add the value in char s[] ,

#include<stdio.h>

int main() {
    char s[] = "Hello Ninja";
    s[6] = 'x';  // adding the value in position 6
    printf("%s", s);
}

 

Output:

Hello xinja


(Here edit is successful)
Check out this problem - Longest String Chain

Must Read Decision Making in C

Frequently Asked Questions

What does char* [] mean in C?

In C, char* means a pointer to a character. Strings are an array of characters eliminated by the null character in C.

Is char* the same as string?

char* is a pointer to a character, which can be the beginning of a C-string. char* and char[] are used for C-string and a string object is used for C++ springs. char[] is an array of characters that can be used to store a C-string.

What is the difference between char and char*?

char[] is a character array whereas char* is a pointer reference. char[] is a specific section of memory in which we can do things like indexing, whereas char* is the pointer that points to the memory location. 

What is a character array?

A character array is a sequence of characters, it is the same as a numeric array. A string array contains pieces of text in it.

What is the difference between Strcat and Strncat?

The Strcat() function is used to append the whole second string to the first string, whereas the strncat() function is used to append-only the specific characters of the second string.

Which argument does the strcat() function take?

The strcat() function takes two arguments: src and dest.

Conclusion

This article extensively discussed the difference between char s[] and char *s. We started with the introduction and explained the difference in tabular form, and we have seen some examples to clarify the difference between char s[] and char *s. 

After reading this article, are you not feeling excited to read/explore more articles on C? Don’t worry; Coding Ninjas has covered you. To learn, see Introduction to CPreprocessors in CCompilation process of a C programDifference between c and embedded c, Tokens in C, and Call By Value & Call By Reference in C.

Recommended Reading:

IEnumerable vs IQueryable

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Storage for Strings in C
Next article
String compare in C
Live masterclass