if at some point ,my string is "aa"
and my pointer points to the first "a"
then i go 2 places fowrward
so it goes "out of band"(java term)
why i dont get an error in a compiler
Printable View
if at some point ,my string is "aa"
and my pointer points to the first "a"
then i go 2 places fowrward
so it goes "out of band"(java term)
why i dont get an error in a compiler
If your string is "aa", then it has a null terminator, so dereferencing a pointer to the char at index 2 is perfectly fine. Also, having a pointer that points one past the last element of an array is also perfectly fine, and is in fact useful for iterating over an array (to keep track of the end point).
EDIT:
Incidentally, "out of band" sounds more like a networking or telecommunications term than a Java term. You are probably thinking about "out of bounds", but that term is not specific to Java.
no \0 char
i defined it as
char* st="aa";
why i dont get out of bounds
??
Then it has a null terminator. Note that you should write it as:Quote:
Originally Posted by transgalactic2
Code:const char* st = "aa";
why const??
so if i write it like this then it adds \0 in the end??
>> (java term)
>> why i dont get an error in a compiler
Because C is not Java. when you program in C you must practice "safe driving" and debug with separate tools like valgrind.
Because the pointer points to the first char of a string literal, and string literals may not be modified.Quote:
Originally Posted by transgalactic2
Yes.Quote:
Originally Posted by transgalactic2
there is no such thing as string in C we are talking about
array of chars and the last char is \0
so what you are saying is if i have
(which could be an array of chars)Code:char *st={'a','c'};
then we cant do
*(st+1)='h';
because its a const??
You are mistaken. In C, a string is "a contiguous sequence of characters terminated by and including the first null character".Quote:
Originally Posted by transgalactic2
That is not a string. That is a pointer to the first element of an array of two chars.Quote:
Originally Posted by transgalactic2
Yes.Quote:
Originally Posted by transgalactic2
EDIT:
Actually, that syntax is wrong. It seems that what it really does is convert 'a' to pointer to char, or something like that.
i ran this code
and it worked fine
i didnt get an error
Code:#include <stdio.h>
#include <string.h>
int main()
{
char str1[2]={'a','b'};
char* str=str1;
*(str+1)='h';
return 0;
}
Why did you think you would get an error? Note that your pointer now points to the first element of an array that is modifiable.Quote:
Originally Posted by transgalactic2
because you said
"pointer points to the first char of a string literal, and string literals may not be modified."
thats what i did
i changed the first char too
and it works fine
Code:#include <stdio.h>
#include <string.h>
int main()
{
char str1[2]={'a','b'};
char* str=str1;
*(str)='h';
return 0;
}
Let me be painstakingly clear.
If you have an array of char likeand you do something like, *(str+1) = 'b'; there will be no error because you are in bounds. Accessing anything greater than the first index is out of bounds and is undefined behavior hence.Code:str = {'a', 'a'};
Likewise when I said that C has no string type in another thread, I only said that out of frustration because you wouldn't allow people to explain the difference between pointers and arrays. There are certainly types that hold strings, but those types are not called "string". Try not to be too literal though. I'm sorry if I caused confusion.
laser light said:
"pointer points to the first char of a string literal, and string literals may not be modified."
so this code works because its not ending with \0
??
?Code:#include <stdio.h>
#include <string.h>
int main()
{
char str1[2]={'a','b'};
char* str=str1;
*(str)='h';
return 0;
}
But in this caseare not part of a string literal.Code:{'a', 'b'}
Also note that not ALL compilers generate code that fail when accessing string literals - the standard only says that string literals mustn't be modified, not what actually happens - that is part of the "undefined land", which as I said in another thread "Makes Alice in Wonderland seem quite normal". It may crash, it probably won't give you a nice error message, and it may appear to work just fine (until you modify the right bit in the right way, e.g. concatenating a string, and then some other string that got overwritten is modified as a result).
--
Mats
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:)