CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Posts
    156

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

  2. #2
    Join Date
    Jul 2008
    Posts
    70

    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

  3. #3
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    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
  •  





Click Here to Expand Forum to Full Width

Featured