CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    [RESOLVED] Pointers (confused)

    Good day.
    Probably the foolish question ever, but still.

    Lets say I have pointer X*and hold in it address of &Y.
    How do I get the the address of Y from X. (Not the X address but Y from X)

    Share and always try to give back more.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Pointers (confused)

    Quote Originally Posted by ulumulu View Post
    Good day.
    Probably the foolish question ever, but still.

    Lets say I have pointer X*and hold in it address of &Y.
    How do I get the the address of Y from X. (Not the X address but Y from X)

    Code:
    int Y = 0;
    int* X = &Y;
    std::cout << &(*X) << std::endl;
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pointers (confused)

    Wait. What is the address of &Y? &Y may not have an address, as I understand. If you want the address of Y given X, then the answer is the value of X, by definition of pointer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Pointers (confused)

    Quote Originally Posted by Mybowlcut View Post
    Code:
    int Y = 0;
    int* X = &Y;
    std::cout << &(*X) << std::endl;
    Oh ok. I did that and It still shows the content of X. Probably I did a mistake somewhere else. Most likely with Y in uper code. Anyway thankyou.
    Share and always try to give back more.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pointers (confused)

    Quote Originally Posted by ulumulu
    It still shows the content of X.
    The content, as in the value, of X is the address of Y, as you yourself have stated.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Pointers (confused)

    Code:
    char *tempLine;
    .......................
    tempLine = &chunchOfData[0];
    .......................
    chunchOfData[i] = '\0';
    .....................
    well and I want from tempLine to get adress of chunchOfData.
    Share and always try to give back more.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pointers (confused)

    Quote Originally Posted by ulumulu
    I want from tempLine to get adress of chunchOfData.
    Why do you need the address of chunchOfData? That address should coincide with the address of the first element of chunchOfData, which is what tempLine contains, but I would like to know the context of the problem before suggesting a solution.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Pointers (confused)

    Ok the problem is:
    I have some text file. And I am parsing some data of it. So then I found the items i need. I want to hold pointers of it in vector that I could use it many times. That vector with pointers will be used in some different ways...
    Last edited by ulumulu; June 19th, 2009 at 04:13 AM.
    Share and always try to give back more.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pointers (confused)

    I do not see how that links to chunchOfData. Are you trying to store pointers to individual characters in a C-style string? If so, what exactly is the problem?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Pointers (confused)

    well chunchofData is actualy a small pease off data (block) which is parsed at the moment.
    Anyway thanks for your help LaserLight. I think I found the problem.
    Problem: knowledge. I am still learning so I am doing a lot mistakes. And I am getting data address , but output shows the content of this address. so there is no problem in code, only in knowledge.
    Share and always try to give back more.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pointers (confused)

    Quote Originally Posted by ulumulu
    And I am getting data address , but output shows the content of this address. so there is no problem in code, only in knowledge.
    Oh, as in you tried to print the pointer to char, but ended up printing the associated string instead of a numeric address? A simple solution would be to cast the pointer to char to be a pointer to void.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Pointers (confused)

    Yes your are right
    Cheers.
    Share and always try to give back more.

  13. #13
    Join Date
    Apr 2009
    Posts
    598

    Smile Re: Pointers (confused)

    1. Do you learn C++ or C?
    If you program in C++, you will rather use "strings" than "arrays of charaters". Therefore, you will almost never use char foo[10]; which is for an array of characters.
    If you program in C, you will use arrays of characters.

    2. Here is an example showing you that, in the case of an array of characters, you have three ways to have its address, foo, &foo[0], and &foo. But usually, you use the first method
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      // Declarations
      char a;
      char *a_ptr;
      char s[5];
      char *s_ptr1, *s_ptr2, *s_ptr3;
    
      // Initializations
      a = 'A';
      a_ptr = &a;
      strcpy(s, "abcd");
      s_ptr1 = s;
      s_ptr2 = &s[0];
      s_ptr3 = (char *)&s;
    
      // Display content
      printf("a=&#37;c, *a_ptr=%c, a_ptr=%ld.\n", a, *a_ptr, (long int) a_ptr);
      // a=A, *a_ptr=A, a_ptr=7601647.
    
      printf("s=%s, s_ptr1=%ld, s_ptr2=%ld, s_ptr3=%ld.\n", s,
             (long int) s_ptr1, (long int) s_ptr2, (long int) s_ptr3);
      // s=abcd, s_ptr1=7601616, s_ptr2=7601616, s_ptr3=7601616.
    
      system("PAUSE");	
      return 0;
    }

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: [RESOLVED] Pointers (confused)

    Quote Originally Posted by olivthill2
    2. Here is an example showing you that, in the case of an array of characters, you have three ways to have its address, foo, &foo[0], and &foo. But usually, you use the first method
    Only the last would result in a pointer to the array. The other two would result in a pointer to the first element of the array (except in the case of sizeof(foo), in which case the expression is not converted to a pointer).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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