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);
}

You can also try this code with Online C Compiler
Run Code
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);
}

You can also try this code with Online C Compiler
Run Code
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);
}

You can also try this code with Online C Compiler
Run Code
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);
}

You can also try this code with Online C Compiler
Run Code
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 C, Preprocessors in C, Compilation process of a C program, Difference 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 Algorithms, Competitive Programming, JavaScript, System 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!
