What is the difference between those two things [duplicate] - c

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 8 years ago.
I want to know why I have a compilation error when I try this :
char *name = "some_string";
And with this I don't have any problem :
const char*name = "some_string";
or
char name[] = "some_string";

When you say
char *name = "some_string";
you are declaring a pointer to "some_string" and pointers are used to point already existing data and the existing data here is "some_string" which is placed under read only memory.
So const keyword is important.
const char*name = "some_string"; // this is proper way
and modifying the "some_string" after this declaration is illegal and causes undefined behavior...
When you say char name[] = "some_string";, "some_string" will be placed under read only memory and the same is copied to name[] array. later you can modify the content of name[].
For more info https://stackoverflow.com/a/18479996/1814023

Related

Why can't I change a string by index when it is declared by pointer notation? [duplicate]

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';

In C, what is the difference between char* and char[]? [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
String literals causing small memory leak?
(4 answers)
Should I free char* initialized using string-literals?
(2 answers)
Closed 4 years ago.
I know that many people asked this but I still have some questions about it.
I read that writing:
char *string = "mystring";
Makes it a read only array of characters, if I were trying to do:
string[0] = 'l';
I would get an error.
When I write:
char string[] = "mystring";
it is saved on the stack, just on the current scope.
what about the char*? is it saved on the heap? or on the stack?
And when I tried writing:
char *string = "mystring";
And then:
string = "mystring2";
it worked, but what happened to the old "mystring" array? is there a memory leak caused by doing this?
what about the char*? is it saved on the heap? or on the stack?
The char* is saved on the stack. But that's just one pointer. The actual string data will be stored in your program's executable (this happens when the program is compiled, it's not the char *string = "mystring"; that puts it there). The assignment to the char* initializes it with the address of "mystring" in your program's binary.
it worked, but what happened to the old "mystring" array? is there a memory leak caused by doing this?
Your executable will contain the content of both the "mystring"; and the "mystring2". When you do the string = "mystring2";, you make that pointer change from pointing to one to pointing to another. There's no memory leak here.

Understanding the below behaviour [duplicate]

This question already has answers here:
Modify a string with pointer [duplicate]
(4 answers)
Closed 6 years ago.
I am getting a seg fault for the below code, is there something wrong?
Here I am trying to shift the bits in a.
Also, I know char * is a read only memory.
So, we have to copy into char a[] and then modify it???
char *str = "abc";
*str = *str << 1;
char* string literal is pointing to read-only memory. You need to use a char array:
char str[] = "Hello";
*str = *str << 1;
See: What is the difference between char s[] and char *s?
What are you trying to do exactly ?
<< is a bitwise operator and *str will not refer to the whole string but only to the first character.
I also recommend you to put parenthesis around your pointer when doing any operations on it to be sure that the bit shift is not done on the address instead of on what is pointed at this address.

Confusion in basic concept of pointers and strings [duplicate]

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).

Declaration of strings in C [duplicate]

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.

Resources