Get current position of pointer

This is a discussion on Get current position of pointer within the C Programming forums, part of the General Programming Boards category; Is there a function to print out the current position of the pointer of the pointer? (im not sure how ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    758

    Get current position of pointer

    Is there a function to print out the current position of the pointer of the pointer?
    (im not sure how to put it)
    I mean like ftell() for a file stream.

    Code:
    int main()
    {
        char * response = "hello world";
        long size;
        for(int i=0;i<6;i++) { 
             size=ftell(response); 
             printf("%i\n",size);  
             or 
             printf("%i\n", *response);
             *response++;
        }
    
        return 0;
    }
    Actually i should get the value of i but without knowing what i is.
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ftell is for file position. You can't get the 'cursor' position, because C doesn't concern itself with such things. You'll really need something like the curses library if you want to write to specific spots on the screen.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ducky View Post
    Actually i should get the value of i but without knowing what i is.
    That's nonsensical -- the value of i is what it is. You don't have to get it.

    Are you trying to get the length of the string in response? Use strlen:
    Code:
    int size = strlen(response);
    You can get the offset of one pointer relative to another this way:
    Code:
        char * response = "hello world", *p = &response[6];
        printf("%d", p-response);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    758
    No, no, no thats not i want.

    Ok i try to put it this way: im "walking a pointer" *response with *response++ and i want to know
    where am i. See? Its simple.

    PS i meant it to Quzah thank you though

    @MK27

    Ok i get it, thanks a lot!
    Last edited by Ducky; 06-19-2010 at 10:08 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, it's simple.

    Code:
    int main()
    {
        char * response = "hello world", *p =response;
        int size = strlen(response);
        for(int i=0;i<size;i++) {
    	printf("%d index is: %d\t offset of p: %d\n",*p,i,p-response);
            p++;
        }
    
        return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    758
    Actually that is exactly what i wanted to know i just wanted to go the other way to understand it.

    Why should we substract 'response' from 'p' ?

    I guess thats what they call pointer arithmetic, pretty hard to grasp.

    Maybe because in my head a pointer was a string, which is apparently not.
    Compiler MSVC++ 2010 with Code::Blocks.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A pointer contains a memory address, by incrementing a pointer pointing to the same address as your string you can movie ahead in the string. By subtracting these pointers you get your current relative position.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    758
    By subtracting these pointers you get your current relative position.
    Voila this is what not clear: we incremented 'p'. Did this incremented 'response' too?

    1. if yes then they have the same value so 6-6=0
    2. if no then 'response' is 0. So whats the point substracting 0 ?
    Compiler MSVC++ 2010 with Code::Blocks.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    response and p are two different pointers pointing to the same memory location. Modifying one does not affect where the other is pointing.

    Think of it as a road sign, where two city names : "Strasbourg" and "Paris" both point to the right. Changing one sign to make it point ahead will not modify the other.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    758
    So in this case 'response' remains 0 and we are substracting 0. Am i right?
    Compiler MSVC++ 2010 with Code::Blocks.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ducky View Post
    Maybe because in my head a pointer was a string, which is apparently not.
    To add to what Subsonics et. al. said, a memory address in C contains one byte of information. The are laid out in sequential order, and the compiler guarantees in the case of an array (such as a string) that the array occurs in sequence. So, let's use 2 digit addresses for simplicity:

    Code:
    char *str = "hello";
    printf("%p", str);
    %p gives the value of a pointer, which is an address. Generally, the exact value is not very important, but let's say in our 2 digit addressing system the value is 42 (they are more or less arbitrary and determined at runtime). So the value at memory location 42 is 'h', because each address is one byte. Now:

    43: 'e'
    44: 'l'
    45: 'l'
    46: 'o'
    47: '\0' null terminator

    So from the previous example, if "response" where 42, p is initially 42 as well. 42-42=0. Then it moves up into higher address. Make sense?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Ducky View Post
    Voila this is what not clear: we incremented 'p'. Did this incremented 'response' too?
    No.

    Quote Originally Posted by Ducky View Post
    2. if no then 'response' is 0. So whats the point substracting 0 ?
    It's not 0, it contains a memory address, that you can print BTW with the %p format specifier in printf.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    758
    So from the previous example, if "response" where 42, p is initially 42 as well. 42-42=0. Then it moves up into higher address. Make sense?
    Awesome, now it makes perfect sense!

    Thanks again MK27 and everybody else for your inputs, this has been bugging me for a long time.
    Compiler MSVC++ 2010 with Code::Blocks.

  14. #14
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    Didn't know if this will help but I made a couple changes to MK27's example to show exactly what the memory addresses look like and how subtracting response from p gets the index number.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define FALSE 0
    #define TRUE 23
    
    int main(int argc, char **argv)
    {
    	int i;
        char * response = "hello world", *p =response;
        int size = strlen(response);
        for( i=0;i<size;i++) {
    		
    	printf("%p %c index is:%d\toffset of p:%d %p  \t",response,*p,i,p-response,p);
    	printf("(p-response) = %d\n",p-response);
            p++;
        }
        return 0;
    }
    
    /*
    0x8048590 h index is:0	offset of p:0 0x8048590  	(p-response) = 0
    0x8048590 e index is:1	offset of p:1 0x8048591  	(p-response) = 1
    0x8048590 l index is:2	offset of p:2 0x8048592  	(p-response) = 2
    0x8048590 l index is:3	offset of p:3 0x8048593  	(p-response) = 3
    0x8048590 o index is:4	offset of p:4 0x8048594  	(p-response) = 4
    0x8048590   index is:5	offset of p:5 0x8048595  	(p-response) = 5
    0x8048590 w index is:6	offset of p:6 0x8048596  	(p-response) = 6
    0x8048590 o index is:7	offset of p:7 0x8048597  	(p-response) = 7
    0x8048590 r index is:8	offset of p:8 0x8048598  	(p-response) = 8
    0x8048590 l index is:9	offset of p:9 0x8048599  	(p-response) = 9
    0x8048590 d index is:10	offset of p:10 0x804859a  	(p-response) = 10
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21