I know I can use pointers to get every character from a particular string such as
but I was wondering if there was another way to do thisCode:char * string = "hello"; char * i; i = string; for(;*i != '\0'; i++) { printf("%c",*i); }
I know I can use pointers to get every character from a particular string such as
but I was wondering if there was another way to do thisCode:char * string = "hello"; char * i; i = string; for(;*i != '\0'; i++) { printf("%c",*i); }
What's wrong with the pointer way? As long as we don't know that, we can't know in what way it should be 'better', can we? Because it won't be better, it will just be more suitable in certain situations. For instance, you could use "string[3]" to get the 4th character...
And why do this:
when the obvious loop would be:Code:i = string; for(;*i != '\0'; i++)
--Code:for(i = string;*i != '\0'; i++)
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.
I was just wondering if there was a standard function I could use
well lets say your given a string that is not null terminated would it still be possible with pointer arithmetic
It would not be a string any more, but if you know the length of the char array, you can of course use pointer arithmetic safely.Originally Posted by Albinoswordfish
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why do you want to use *char = "string" ??
This way as you said, you are creating a read only string. So leave apart pointer arthimetic and changing '\0' character, you won't be able to change byte...
better use char[] = "string"
and then you don't need to worry about anything ...
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.
Because by definition, in C, "a string is a contiguous sequence of characters terminated by and including the first null character".Originally Posted by EVOEx
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hmmm ok. Then I can somewhat agree to what you said . But you can still call it a string as well if you want. Wikipedia: "a string is an ordered sequence of symbols". And my lecturers taught me the same thing.
So, while it may not be a string according to the C standard (so not a C String), it is still a string according to many other definitions.
Of course, but if you do not use the standard definition, then you are running the risk of miscommunication. For example, the definition that you quoted allows us to call arrays of non-character integers strings, and indeed they are strings of integers. We could even zero terminate them and thus talk about "0-terminated strings".Originally Posted by EVOEx
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)