i turned into string and it still working??
Code:#include <stdio.h>
#include <string.h>
int main()
{
char str1[3]={'a','b','\0'};
char* str=str1;
*(str)='h';
return 0;
}
Printable View
i turned into string and it still working??
Code:#include <stdio.h>
#include <string.h>
int main()
{
char str1[3]={'a','b','\0'};
char* str=str1;
*(str)='h';
return 0;
}
i made it a string literal
its workingCode:#include <stdio.h>
#include <string.h>
int main()
{
char str1[3]="ab";
char* str=str1;
*(str)='h';
return 0;
}
??
No, it works because you are dealing with an array whose contents can be modified, not a string literal whose contents cannot be modified.Quote:
Originally Posted by transgalactic2
This is correct:
This is wrong:Code:#include <stdio.h>
int main(void)
{
char str[] = "ab";
char *p = str;
*p = 'b';
printf("%s\n", str);
return 0;
}
Spot the difference.Code:#include <stdio.h>
int main(void)
{
char *str = "ab";
char *p = str;
*p = 'b';
printf("%s\n", str);
return 0;
}
You didn't. You declared an array, not a "char *". As I already said, doing it this way causes the string to be copied into the array. The literal is the "ab" in the code, not the array. Had you declared a char *, you would be pointing at this literal, and could not modify it, but that's not what you did.
hooooray i got my error
it weird because by the definitions that i knowCode:#include <stdio.h>
#include <string.h>
int main()
{
char* str="ab";
*(str)='h';
return 0;
}
equalsCode:char* str="ab";
so i cant see why one is working and the other doesntCode:char str1[3]={'a','b','\0'};
char* str=str1;
??
so
this is a literal
and we cant change any of it cellsCode:char* str="ab";
thanks
the other stuff that i showed are arrays of chars
correct??
This is a string literal: "ab"Quote:
Originally Posted by transgalactic2
It is an array of three chars, including the null terminator.
Arrays of chars are always involved where strings are involved.Quote:
Originally Posted by transgalactic2
you say that they both are arrays of chars.
so what is the difference between
??Code:char str[] = "ab";
and
char* str = "ab";
why the first is allowed and the last not
except the fact that the first is not a literal ??
what happens in the compiler that allowes it?
The difference is that in the former, str is an array of chars that is a copy of the string literal "ab", but in the latter, str is a pointer to the first char of the string literal.Quote:
Originally Posted by transgalactic2
This difference is critical when you attempt to change the contents of str, since with the former you safely change the elements of an array under your control, but for the latter you attempt to change a string literal that is stored in an area not under your control.
Do you know difference of the array and pointer?
first is array of 3 chars that is initialized with 'a','b',0
and because it is array - you can modify each element of it
the second is just a pointer that points to some external memory, which could be read-only (it is compiler dependent)
you can modify your pointer but you have no right tomodify contents of the external memory
aaahh the memory is Read only
so in this case char* str = "ab";
it changes the memory
thanks:)