Whats difference between them ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Whats difference between them ?

char new_string[] = "12345"; char *str = " 12345":

20th Apr 2021, 5:04 PM
[bool left=True;]
[bool left=True;] - avatar
3 Answers
+ 1
the first one as it currently defined is a null-terminated char array. But if you were to define it as char name[5] = "namee"; // not null-terminated. it would still be an array of chars, but not a string char* name = "namee"; // null-terminated the second one is guaranteed to be a null-terminated string you can see the differences below. just comment and uncomment the ones that you want to test. #include <stdio.h> int main() { char name[5] = "namee"; // prints -> nameex`@ //char* name = "namee"; // prints -> namee char c = 'x'; printf("%s", name); return 0; }
20th Apr 2021, 5:30 PM
Flash
+ 1
The first one is declaration of string using array the second one is declaration of string using pointers.
20th Apr 2021, 5:08 PM
Rohit Kh
Rohit Kh - avatar
0
Flash thanks. first one is an array that pointing to first char. And 2nd one a char pointer also pointing first char. In this respect how they are different ?
20th Apr 2021, 10:10 PM
[bool left=True;]
[bool left=True;] - avatar