Assign pointer value [duplicate] - c

This question already has answers here:
Why does writing to a string literal in this C program segfault?
(6 answers)
Why can't I write to a string literal while I *can* write to a string object?
(4 answers)
Closed 8 years ago.
Why do I get exception in the code?
char* first = "first";
char* second = "second";
*first = *second;
Shouldn't it just assign values?
Error message says: access violation

No because they are string literals and are read only
char *first = "first";
char *second = "second";
you can try with arrays instead.
char first[] = "first";
char second[] = "second";
to prevent this kind of error you can do this
const char *first = "first";
const char *second = "second";
when declaring string literals, it will not prevent the problem completely because you still can cast away the const but you should do so conciously.

Related

Why the difference between char* and char[x] matter when using the & operator [duplicate]

This question already has answers here:
Pointer to Integer Array versus Double Pointer to Integer
(3 answers)
Is an array name a pointer?
(8 answers)
Why should I always enable compiler warnings?
(20 answers)
Closed 3 years ago.
The following code will segfault.
char* input = "12.34"; //< this is only to simplify the example.
char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));
char* part1 = strsep(&buffer, ".");
The following code will not.
char* input = "12.34"; //< this is only to simplify the example.
char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));
char* ptr = buffer; //< Only diff.
char* part1 = strsep(&ptr , ".");
When passed by reference (&) as a function argument, why does the difference between char** and char*[30] matter?
If you look at the man for strsep, it needs the double pointer because it tries to assign the pointer.
"char *strsep(char **stringp, const char *delim);
...
and *stringp is updated to point past the token"
Basically if you have an array, you can't just tell the head of the array to be in a new place. You can however make a pointer to any element in that array and then change the value of the pointer (thereby making it point to a different element if you so wish).

Segmentation Error in C Code [duplicate]

This question already has answers here:
Why can't I use this code to overwrite a string?
(4 answers)
Closed 9 years ago.
I am totally new to C and I am about to write a function that reverses a string. My code looks like this:
char *str = "abcdef";
char *ptr;
for(ptr = str; *ptr ; ptr++);
for(; str < --ptr; str++)
{
char c = *str;
*str = *ptr;
*ptr = c;
}
I get a segmentation fault error. And I don't see the mistake (maybe it is too obvious). Any hints?
Cheers
Change
char *str = "abcdef";
to
char str[] = "abcdef";
The first str points to a string literal and string literals are not modifiable in C,
Your string is stored in ROM, so you can't write to it. Depends on your platform though.

char string* vs char string[] [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 10 years ago.
Is there a difference between:
char string = "name";
const char* point = string;
vs
const char string[] = "name";
Will you please explain the difference too?
Yes.
The first simply points to a read only section of memory, the declaration really should be:
const char* string = "name";
The second creates an array long enough to hold the string "name" (so, four characters plus one for the null terminator) and copies the string inside the allocated space.

C : Character swapping [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 10 years ago.
I need to swap two characters by pointers but when I run this code,the program crashes.
int main(){
char *s1 = "string1";
swap(st,(st+1));
/* BUT THIS CODE WORKS - Whats the problem?
* char s1[] = "string1";
* swap(s1,&s1[1]);
*/
return 0;
}
void swap(char * const ptr1, char * const ptr2){
char temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
char *s1 = "string1";
Because s1 points to a string literal and modifying invokes undefined behaviour in C. That's why this doesn't work.
Whereas in this char s1[] = "string1";
s1 is an array and hence it can be modified.
A string literal may not be modified. You try to modify "string1" in your code which is not allowed. Indeed, many compilers place string literals in a special section that may not be written into.
This line of code creates a string literal which cannot be changed. It is only readable.
char *s1 = "string1";
Any attempt to change it will give you an error.
While your commented example :
char s1[] = "string1";
creates an actual array. This can be edited and used normally.

C:strncpy causing problems with pointer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
I want to replace a word in a string. Here is the code
char text[] = "This is a list of lists";
char *find = "list";
char* pos = NULL;
pos = strstr(text,find);
strncpy(pos,"test",4)
This works fine but
char *text = "This is a list of lists";
char *find = "list";
char* pos = NULL;
pos = strstr(text,find);
strncpy(pos,"test",4)
This gives a segmentation fault.
In the first example "text" is an array and the data is just copied at that location. In the 2nd one "text" is a pointer. What is issue?
The difference between
char text[] = "This is a list of lists"; // 1
and
char *text = "This is a list of lists"; // 2
is that, in (1), text is a non-constant array of characters; where as in (2), text points to a string literal, and string literals are are constant. You can't modify constant objects, which you're trying in (2). What you're doing in (2) in actually undefined behaviour.
The problem is the string in the second example is a string literal, which must remain constant. When you try to write on that string you are writing to read-only memory, which (depending on the operating system) is not allowed.

Resources