+ 1

Difference between array and string?

3rd May 2020, 12:59 PM
Krishna Pratap Kushwah
Krishna Pratap Kushwah - avatar
5 Respostas
+ 1
The main difference between a string and character array is that a string is null-terminated. For example, most of the string functions in string.h expects a null-terminating byte -> strlen(), strcpy(), strcmp(). The C language is smart enough to append a null-terminator when you assign "strings" within double quotes. If you assign to an array character by character you have to add a null-terminator at the end to make it a valid string. @Jayakrishna - There's a small difference between char *c = "strings" and c[] = "strings". The first one points to a string in read-only memory (i.e it is not modifiable and should for safety reasons be const char *c) while the second version is stored on the stack and can be modified. It's a small difference but it matters. Modifying a pointer to a string in read-only memory results in a crash.
3rd May 2020, 4:54 PM
Damyian G
Damyian G - avatar
+ 2
Tag the language also in which you want.. From your profile.. In c, a string is sequence of characters stored in a charecter array.. C has no separate string type like other languages have.. An array is colloection of similar data types stored in sequence of locations.. It is refferenced by pointers in a same as other arrays are refferenced.
3rd May 2020, 1:11 PM
Jayakrishna 🇮🇳
+ 2
String defined by array type is modifiable while string defined by pointer is unmodifiable.. Like Ex: char c[] ="string" ; c[0] = "S" ; //is valid but char *c="string" ; c[0]= 'S' ; //is not valid, error. Edit: @Damyian G, I got the point, I didnot thought about it.. Not understood question fully.. Tqs..
3rd May 2020, 7:11 PM
Jayakrishna 🇮🇳
+ 1
I told in c only,.. If you still have a doubt about this.. As an run this example Arrays by pointers using in to access elements.. : #include <stdio.h> int main() { char *c="strings"; //meaning to c[] int n[]={1,2,3,4,5,6,7}; for(int i=0;i<7;i++) printf("%c %d\n",*(c+i),*(n+i)); return 0; } Edit: This is about only how it will be a string in c. There are other defferences using by pointers and using by Arrays...if you are asking about that reply..
3rd May 2020, 1:22 PM
Jayakrishna 🇮🇳
0
In C language
3rd May 2020, 1:12 PM
Krishna Pratap Kushwah
Krishna Pratap Kushwah - avatar