c assigning string pointer to other string pointer

I have that code:

#include<stdio.h>

int main()
{
    char *str = "aaaaaaaa";
    char *stt = "bbbbbbbb";
    *str = *stt;
    printf("%s\n", str);

return 0;
}

it gives me an error: Access violation writing location, someone can explain me why and how to over come this?

Solutions

You are only assigning the first character, which you shouldn't since string literals are not mutable.

Simply use str = stt; to assign the pointers.

BTW, since they are not supposed to be changed, you'd better use const qualified types as in

char const *str = "aaaaaaaa";

String literals like "aaaaaaaa" are read-only, and your code is trying to overwrite the first character of the string pointed to by str. If that's what you want to do, you'll need to do something like this:

char str[9];
char *stt = "bbbbbbbb";
strcpy(str, "aaaaaaaa");
*str = *stt;
printf("%s\n", str); // prints: baaaaaaa

Of course, it's more likely that what you really want is this:

str = stt;
printf("%s\n", str); // prints: bbbbbbbb

A string literal is un-modifiable, i.e, you can't modify a string literal. This is because it is stored on the read only section.

Try this instead

char str[] = "aaaaaaaa";
char stt[] = "bbbbbbbb";  

char *pstr = str;
char *pstt = stt;

pstr = pstt;  
printf("%s\n", ptr);
Tags: C / String / Pointers

Similar questions

Assigning a string to a char pointer is valid but assigning an integer to int pointer is invalid in C. Why?
A char pointer can be assigned an arbitrary string but an integer pointer cannot be assigned an integer. Since both of them are pointers and contains address. Why is assigning string valid but an integer invalid in C to a pointer before dynamic allocation. Which gives output : What is the reason behind it? Although both of them are invalid in C++.
Is it correct to cast a pointer to pointer to pointer to int to a pointer to pointer to pointer to double?
I have a function which deallocates a cubic matrix of double: Then I have a cubic matrix of integer int ***matrix;. Compiler gives me an error when I call free_matrix(matrix,n) but I don't want to duplicate the previous function which only deallocates elements. Is it correct/good/safe to simply cast it like this?
C
Difference between directly assigning a float variable a hexadecimal integer and assigning through pointer conversion
I was investigating the structure of floating-point numbers, and I've found that most of compilers use IEEE 754 standard to store floating point numbers. And when I tried to do: it produces the result: but if I try: it gives: The second try gave me the right answer. What is it wrong with the first try? And why does the result differ from that when ...
Assignment of read only location error when assigning value of pointer to other pointer?
Do not understand why this error occur when i try to write str[j]=str[j+1]? I'm doing something wrong here please do tell me?
C
When assigning pointer to pointer I get warning: assignment makes integer from pointer
I hate posting this because there are so many of these, but none of them seem to address what I am seeing. The normal issues (undeclared functions, unintentional casts, misunderstanding of basic pointers) don't seem apply here. This is the stripped down version of my code: This fails without the casts, but putting them in makes it even more obvious...
assigning pointer to another pointer, does the second pointer point to the same address as the first one?
in C programming, I am wondering about the following pointer assignment: My question is: does the pointer2 point to the same address as pointer1 points to? can I perform the following using pointer2? For assining pointer1 to pointer2, should I malloc memory for pointer2 first?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.