pointer help

This is a discussion on pointer help within the C Programming forums, part of the General Programming Boards category; I'm trying to learn pointers and their values, i know the following (please correct if incorrect) ptr = the value ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    58

    Exclamation pointer help

    I'm trying to learn pointers and their values, i know the following (please correct if incorrect)

    ptr = the value of the variable it points to
    *ptr = the address of the variable it points to
    &ptr = the address of the pointer it self

    but what does *&ptr mean? i see this often and have no clue what it means. can you help me?

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    > ptr = the value of the variable it points to
    > *ptr = the address of the variable it points to
    Actually if ptr is a pointer then
    ptr = address of the variable it points to
    *ptr = the value of the variable it points to

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    so then what would *&ptr be? the value at the address of ptr?
    --== www.NuclearWasteSite.com==--

  4. #4
    Unregistered
    Guest
    * and & are the inverse of one another, so they cancel out, and *&ptr = ptr.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    so what if ptr is a string not a pointer? would i be accessing ptr as a pointer if i did *(long *)&ptr = something;?
    --== www.NuclearWasteSite.com==--

  6. #6
    Unregistered
    Guest
    Not sure if this is what you mean, but you access a string by a pointer to the beginning of the string.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    no i mean if i have the following code
    Code:
    #include <stdio.h>
    
    int main()
    {
    char a_string[1030];
    int i;
    for(i=0;i<=1030;i++)
    {
    printf("The *& value of a_string[%i]:  %x", i , a_string);
    }
    
    };
    what value am i getting there and whats the diffrence between that and
    just normal buffer[i] ?
    --== www.NuclearWasteSite.com==--

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char a_string[1030];
    int i;
    for(i=0;i<=1030;i++)
    This is wrong. <= 1030 will end up pointing PAST the end of your array. Arrays go from zero, to SIZE-1, not from zero to SIZE. This should be "<1030".

    what value am i getting there and whats the diffrence between that and
    just normal buffer[i] ?
    So why don't you add a few more lines to your code and find out for yourself?

    Furthermore, your printf is wrong. It will only ever print the initial string pointer. Your loop doesn't ever change what value it prints.

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

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    well thank you for critisizing my code when it wasnt even in question. thats not code that i've actually tested, and i wrote it on the fly and do expect it to have errors. i did write code to find the diffrence. there is a really wierd diffrence here's the ACTUALL code.


    Code:
    #include <stdio.h>
    
    long get_sp()
    {
    __asm__("movl %esp, %eax");
    }
    int main()
    {
    char string[1030];
    int i;
    long retaddy;
    
    fprintf(stderr, "retaddy: %x\n", retaddy);
    
    retaddy=get_sp();
    
    for(i=0;i<20;i++)
    {
    *(long *)&string[i]=retaddy;
    fprintf(stderr, "*&string[%i]: %x\n",i ,*(long *)&string[i]);
    }
    
    for(i=0;i<20;i++)
    {
    (long *)string[i]=retaddy;
    fprintf(stderr, "(long *)string[%i]: %x\n",i , (long *)string[i]);
    }
    
    };

    this gives me the following:
    Code:
    *&string[0]: bffff4ac
    *&string[1]: bffff4ac
    *&string[2]: bffff4ac
    *&string[3]: bffff4ac
    *&string[4]: bffff4ac
    *&string[5]: bffff4ac
    *&string[6]: bffff4ac
    *&string[7]: bffff4ac
    *&string[8]: bffff4ac
    *&string[9]: bffff4ac
    *&string[10]: bffff4ac
    *&string[11]: bffff4ac
    *&string[12]: bffff4ac
    *&string[13]: bffff4ac
    *&string[14]: bffff4ac
    *&string[15]: bffff4ac
    *&string[16]: bffff4ac
    *&string[17]: bffff4ac
    *&string[18]: bffff4ac
    *&string[19]: bffff4ac
    (long *)string[0]: ffffffac
    (long *)string[1]: ffffffac
    (long *)string[2]: ffffffac
    (long *)string[3]: ffffffac
    (long *)string[4]: ffffffac
    (long *)string[5]: ffffffac
    (long *)string[6]: ffffffac
    (long *)string[7]: ffffffac
    (long *)string[8]: ffffffac
    (long *)string[9]: ffffffac
    (long *)string[10]: ffffffac
    (long *)string[11]: ffffffac
    (long *)string[12]: ffffffac
    (long *)string[13]: ffffffac
    (long *)string[14]: ffffffac
    (long *)string[15]: ffffffac
    (long *)string[16]: ffffffac
    (long *)string[17]: ffffffac
    (long *)string[18]: ffffffac
    (long *)string[19]: ffffffac
    i want to know why they are (almost) completly diffrent.
    --== www.NuclearWasteSite.com==--

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your casting of everything to longs is confusing the issue, I believe. By doing this with the fprintf()'s, the program is printing erroneous data.

    To simplify, I'd suggest you make this:
    >fprintf(stdout, "*&string[%i]: %x\n", i, *(long *)&string[i]);
    into this:
    >fprintf(stdout, "*&string[%i]: %x\n", i, *(&string[i]));

    Here's a sample program to help clarify (hopefully):
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char astring[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	
    	printf ("astring >%s<\n", astring);
    	printf ("Element 11 located @ %p, contains: 0x%02x %c\n", &astring[10], astring[10], astring[10]);
    	printf ("Accessed differently, contains is 0x%02x %c\n", *(&astring[10]),*(&astring[10]));
    	printf ("Accessed incorrectly, contains is 0x%02x %c\n", *(long *)&astring[10],*(long *)&astring[10]);
    	
    	return 0;
    }
    
    /* Output from this program:
    astring >ABCDEFGHIJKLMNOPQRSTUVWXYZ<
    Element 11 located @ 0064FDEE, contains: 0x4b K
    Accessed differently, contains is 0x4b K
    Accessed incorrectly, contains is 0x4e4d4c4b K
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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