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 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
Last edited by transgalactic2; 03-24-2009 at 12:57 PM.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:Originally Posted by transgalactic2
Code:const char* st = "aa";
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by transgalactic2
Yes.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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".Originally Posted by transgalactic2
That is not a string. That is a pointer to the first element of an array of two chars.Originally Posted by transgalactic2
Yes.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.
Last edited by laserlight; 03-24-2009 at 01:28 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.