|
-
April 11th, 2009, 04:42 AM
#1
subtraction of two pointers..
what is the meaning of subtraction of two pointers??
return str-temp
Code:
#include <stdio.h>
#include <string.h>
int what(char *str, char ch){
char *temp;
for( temp = str; *str; str++)
if(*str != ch) *temp++ = *str;
*temp = ‘\0’;
return str-temp;
}
void main(){
char string[ ]= "xbxbabsxdx";
printf(“result = %d\n”, what(string, ‘x’));
puts(string);
}
-
April 11th, 2009, 05:02 AM
#2
Re: subtraction of two pointers..
First this is a horrible thing to do. But very common c programming (not c++)
In the above code you have temp pointing to a string lets say "foo"
str then points to foo but a couple characters past it lets say temp+2 = "o"
then str-temp = 2
So I think that function is just finding the location of the element.
I didn't compile this and check so I could be wrong, but this is why this style of programming is bad it takes some thought to figure out exactly what is going on.
Hope that helps
-
April 11th, 2009, 05:12 AM
#3
Re: subtraction of two pointers..
what is the meaning of subtraction of two pointers??
It is a distance between them
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|