We help IT Professionals succeed at work.

strcmp

zizi21
zizi21 asked
on
253 Views
Last Modified: 2010-04-15
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++;                  
      }

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
@evilrix:
I would use:
while(*str && str!=end)
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Why? We know you're not going to run beyind the end of the buffer, it's a pointless test.
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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)
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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.

#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;
}

Open in new window

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
interesting, that will force the string to be iterated twice. Nice performance example.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
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.
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
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!

Author

Commented:
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..
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.