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

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.

Related

Queries on declaring & initialising string and also string memory region [duplicate]

This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
What is the difference between char s[] and char *s?
(14 answers)
Closed 3 years ago.
My question comes in three part.
char str[] = "hello!";
char *str1 = "heello!";
puts(str);
str1[1] = 1
puts(str1);
printf("%s\n", str1);
printf("%p\n", str1);
Ouput of code:
hello!
h1ello!
heello!
0x10735ef9b
1) I understand that when you're declaring a char * type, you're declaring a pointer that points to char type. And usually for pointer, since it contains the address of the variable its pointing to, when you print the pointer itself, you'll get the address stored in the pointer and only when you dereference the pointer, then you'll get the content stored in the variable the pointer is pointing to.
However for a char * type, why is it that it doesn't function like a regular pointer? You can print the content of the variable which the 'char *' pointer is pointing to without dereferencing it and also use it like a string variable(rather than a pointer) by changing the value of the str1[1] as shown above.
2) I was taught that you shouldn't declare a pointer variable as such in the topic of pointer:
char *str1 = "heello!";
because the pointer variable would be pointing to whatever address happens to be in the memory at that time, thus running a risk of changing the value in a memory location that you don't mean to and resulting in segfault.
Hence you should do the following:
char *str1;
char c;
str1 = &c;
*addr = "hello!"
By doing the above, you'll make sure that your pointer is pointing to the right location before dereferencing and writing to the location.
However in the notes on string, it says to declare and initialise as such:
char *str1 = "heello!";
I'm quite confused as to what I should follow now when declaring a char * pointer. Please help.
3) It says in my notes that the location of string is stored in a read only region of the memory called the text region.
Hence doing this:
char *str1 = "hello!";
str[1] = '.';
Will crash your program.
But when you do this:
char str2[7] = "hello!";
str2[1] = '.';
This is okay and will successfully change the second element in the string from 'e' to '.'
The explanation to this was that str1 points to a read only region in the memory, while str2 contains a copy of the string on the stack.
I do not understand how the above 2 different ways of declaring the string variable will make such a big difference when executing the code. Please help.
Thank you!
String literals like "heello!" are really read-only arrays of characters (including the string null-terminator).
With
char *str1 = "heello!";
you make str1 point to the first character of such an array.
And when you do str1[1] = 1 you attempt to modify the read-only array, which is forbidden and leads to undefined behavior (which can but doesn't have to lead to crashes).
That's why you either should use your own arrays for strings, or use const char * to point to literal strings.

What is the difference between those two things [duplicate]

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

Difference between array and pointer [duplicate]

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.

Pointers and Strings causing segmentation fault [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;
char *str = "Hello";
printf("%c",++*str);
This gives segmentation fault on linux with gcc.
The moment the first statement is changes to as
char str[10] = "Hello";
It works. What may be the reason?
It is undefined behaviour to attempt to modify a string literal.
The compiler is free to place it in read-only memory (as it probably does in your case). Attempting to modify read-only memory is what's probably triggering the segfault.
This statement char *str = "Hello"; stores the string "Hello" in RO-section and assigns the address of the area of RO-section(in which "Hello"is stored) to str. The data stored in RO-section cannot be modified thus you are getting a segfault.
char str[10] = "Hello";
is also wrong. You should instead write
char str[10];
strncpy(str,"Hello",sizeof(str));

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