zizi21
asked on
strcmp
hi,
i have a string like this:
char *original="helloworld":
char *str;
str=&original[0];
while(*str!='0')
{
printf("%c", *str);
str++;
}
it prints helloworld
what i am trying to do is to say: only loop the first 3 chars...One way is to use a count in the loop which
i understand..i am learning pointer arithmetic , so, i am trying pointer arithmetic way like this but it is not doing what i am trying which is : if you move 3 steps arithmetically ..thanks
while(*str!='0'&&str < 3)
{
printf("%c", *str);
str++;
}
i have a string like this:
char *original="helloworld":
char *str;
str=&original[0];
while(*str!='0')
{
printf("%c", *str);
str++;
}
it prints helloworld
what i am trying to do is to say: only loop the first 3 chars...One way is to use a count in the loop which
i understand..i am learning pointer arithmetic , so, i am trying pointer arithmetic way like this but it is not doing what i am trying which is : if you move 3 steps arithmetically ..thanks
while(*str!='0'&&str < 3)
{
printf("%c", *str);
str++;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why? We know you're not going to run beyind the end of the buffer, it's a pointless test.
>> That won't work. Something like this would :
>>
>> while(*str!='0' && str - original < 3)
I just noticed that I made a copy-paste error here - it should of course have been :
while(*str != '\0' && str - original < 3)
>>
>> while(*str!='0' && str - original < 3)
I just noticed that I made a copy-paste error here - it should of course have been :
while(*str != '\0' && str - original < 3)
so, do you really think this will be the unique string that the author will evaluate?
you are missing the fact that this is just an example and should have a practical use further.
for safe C programming, the coder must always will aware about string overflow.
you are missing the fact that this is just an example and should have a practical use further.
for safe C programming, the coder must always will aware about string overflow.
>> you are missing the fact that this is just an example
The point of my example was to demonstate iterator semantics and not testing for null, which has already been covered. It is unnecessary to test for null if the end point has already been bounds checked.
The point of my example was to demonstate iterator semantics and not testing for null, which has already been covered. It is unnecessary to test for null if the end point has already been bounds checked.
#include <stdio.h>
#include <string.h>
int main()
{
char *original="helloworld";
size_t len = strlen(original);
char *str = original;
char *end = original + (3 < len ? 3 : len); // Bounds checked end point
while(str!=end)
{
printf("%c", *str);
++str;
}
return 0;
}
interesting, that will force the string to be iterated twice. Nice performance example.
It wasn't meant as a performance example jaime. The point I am making is the end point should already be defined within the bounds of the string. Using strlen to do this is just an example... as well you know.
Yeah, but it is a bad example IMHO, just to support your position. This is my point.
Every iteration over a C string should include null-terminator evaluation, as explained in thousands of "safe C programming practices" books and articles.
Every iteration over a C string should include null-terminator evaluation, as explained in thousands of "safe C programming practices" books and articles.
The OP already knows how to test for null, this wasn't the point of the simple example; however, for the same of peace and quite I wish I'd just added the test now!
ASKER
hi,just saw this..thanks a million...
i didn't want to use counter because..i am learning pointer aritmetric...trying to code what i learn..
i didn't want to use counter because..i am learning pointer aritmetric...trying to code what i learn..
I would use:
while(*str && str!=end)