This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 8 years ago.
I'm currently learning C and am having a bit of trouble understanding how to declare a string. I understand that in C, strings are simply arrays of characters. I've seen two different ways for declaring a string:
char[] some_string = "hello world";
and
char *some_string = "hello world";
When should each of these be used? Most code I've seen uses the char *some_string syntax, but I'm sure that there's a reason why. Thanks.
char some_string[] = "foo" declares a local array of size 4 which is initialized at run time with the values 'f', 'o', 'o', and '\0'. That memory is (probably) on the stack of the function. char *some_string = "foo" declares a pointer which is initialized to the location of a string which is initialized at compile time to the same values. The string char [] some_string = "foo" is a syntax error. The most relevant difference between the two is that, on many platforms, the latter is not writable. In other words, some_string[1] = 'a' will generate a segfault on some platforms if some_string was declared as a pointer rather than an array.
Related
This question already has an answer here:
Why can I not modify a string literal in c?
(1 answer)
Closed 4 months ago.
for example:
char stringer[]="hello";
stringer[2]='A';
The above works to change 'l' to 'A'. But if I do the following:
char *stringer="hello";
stringer[2]='A';
This doesn't work, is there a reason for this?
As answered by Some programmer dude in comments:
Literal strings are really non-modifiable arrays of characters. With char *stringer="hello"; you make stringer point to the first character of such an array. Attempting to modify its contents leads to undefined behavior.
That's why you should always use const char * when pointing to literal strings.
Strings can be modified when using pointer, if the pointer is pointing to something you're allowed to modify. For example
char stringer[] = "hello";
char *pointer = stringer;
pointer[2] = 'A';
This question already has answers here:
Why does writing to a string literal in this C program segfault?
(6 answers)
What's wrong with my strcpy? [closed]
(4 answers)
Closed 9 years ago.
Yesterday I had a little trouble with a homemade "strcpy" function. It works now though but I'm a little confused!
char* a = "Hello, World!"; //Works
char b[] = "Hello, World!"; //Works also
strcpy(a, "Hello!"); //Segmentation fault
strcpy(b, "Haha!!"); //Works..
Where is the difference? Why does the char pointer cause a "Segmentation fault"?
Why does this even work? :
char* a = "Haha"; //works
a = "LOL"; //works..
char* a = "Hello, World!";
gives you a pointer to a string literal. A string literal may exist in read only memory so its contents cannot be changed.
char* a = "Haha"; //works
a = "LOL"; //works..
changes the pointer a to point to a different string literal. It doesn't attempt to modify the contents of either string literal so is safe/correct.
char b[] = "Hello, World!"
declares an array on the stack and initialises it with the contents of a string literal. Stack memory is writeable so its perfectly safe to change the contents of this memory.
In your first example since you are trying to write to a read only memory pointed by a,you will get the segmentation fault.If you want to use pointers then allocate the memory on heap,use and delete it after its use.
Where as b is an array of chars initialized with "Hello, World!".
In the second example you are making the pointer to point to different string literal which should be fine.
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 9 years ago.
In my GCC 32-bit compiler, the following code gives the output
char *str1="United";
printf("%s",str1);
output:
United
then should I consider char *str1="United"; the same as char str1[]="United"?
The two are not the same: char *str1="United" gives you a pointer to a literal string, which must not be modified (else it's undefined behavior). The type of a literal string should really be const but it's non-const for historical reasons. On the other hand, char str1[]="United" gives you a modifiable local string.
char* str = "United"; is read-only. You wouldn't be able to reach inside the string and change parts of it:
*str = 'A';
Will most likely give you a segmentation fault.
On the other hand char str1[] = "United"; is an array and so it can be modified as long as you don't exceed the space allocated for it (arrays cannot be resized). For example this is perfectly legal:
char str[] = "United";
str[0] = 'A';
printf("%s\n", str);
This will print Anited.
See the comp.lang.c.faq, question 1.32. It basically boils down to the fact that declaring the string in array form (char str[] = "foo") is identical to char str[] = {'f','o','o'}, which is identical to char str[] = {102, 111, 111}; that is, it is a normal array on the stack. But when you use a string literal in any other context it becomes "an unnamed, static array of characters, [which] may be stored in read-only memory, and which therefore cannot necessarily be modified." (And trying to modify it results in undefined behavior wherever it happens to be stored, so don't).
This question already has answers here:
Memory Allocation char* and char[]
(3 answers)
Closed 3 years ago.
Can anyone explain me what is a difference between these lines of code
char *p = "String";
char p2[] = "String";
char p3[7] = "String";
In what case should I use each of the above ?
This link should satisfy your curiosity.
Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.
But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.
Strings in C are represented as arrays of characters.
char *p = "String";
You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.
char p2[] = "String";
You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.
char p3[5] = "String";
You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.
char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).
http://c-faq.com/~scs/cclass/notes/sx8.html
You shouldn't use the third one because its wrong. "String" takes 7 bytes, not 5.
The first one is a pointer (can be reassigned to a different address), the other two are declared as arrays, and cannot be reassigned to different memory locations (but their content may change, use const to avoid that).
char *p = "String"; means pointer to a string type variable.
char p3[5] = "String"; means you are pre-defining the size of the array to consist of no more than 5 elements. Note that,for strings the null "\0" is also considered as an element.So,this statement would give an error since the number of elements is 7 so it should be:
char p3[7]= "String";
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Is it possible to modify a string of char in C?
What is the difference between char s[] and char *s in C?
There is something I don't understand about strings and pointers in C.
Suppose I have this declaration:
char str[] = "abc";
Then, if I attempt to modify it this way:
str[0] = 'b';
It will work.
But if I declare the string as a pointer to a char
char* str = "abc"
The attempt above will cause an access violation.
What I'm trying to understand is what exactly is the difference.
Thanks in advance
In The later example the compiler puts the string in the read only data section, so u can't modify it. But in earlier example you are declaring array of character which resides in stack hence you can modify content of this array.