CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Help in Pointer

  1. #1
    Join Date
    Mar 2003
    Posts
    29

    Help in Pointer

    Hi There,

    int *i,t;

    i=(int *) &t; // What does this statement means.... where we use it.

    Thanks and Regards,
    Abbas

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Help in Pointer

    Originally posted by am_abbas
    Hi There,

    int *i,t;

    i=(int *) &t; // What does this statement means.... where we use it.

    Thanks and Regards,
    Abbas
    That's a type cast. Totally useless because it casts an int * to int *.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Jan 2001
    Posts
    588
    Gabriel is right; the typecast is useless.

    Before the statement, you have an uninitialized pointer to an int, i. After the statement is executed, i points to the memory location occupied by the variable t. Therefore, you can now manipulate the value of t using i.

    Code:
    t = 5;
    i = &t;
    *i = 10; // t is now 10.
    cout << t << endl;
    Pointers are very important in C and C++ programming. If you have any further questions just reply to this message.

  4. #4
    Join Date
    Feb 2002
    Posts
    5,757
    One important concept about pointers is that they point to memory addresses, not the actual values.

    Kuphryn

  5. #5
    Join Date
    Jan 2001
    Posts
    588
    Now that I am rereading my post, I feel I should make a distinction. Pointers are very important in plain "C" programming, and less so in C++ programming using libraries like the STL. Standard classes like std::string and std::vector eliminate many cases where pointers would be necessary, letting the classes do all the work for you. Sometimes you can't avoid it and need the extra control that a pointer will give you, and oftentimes, a certain API function will require the use of pointers. While there are often techniques to interface these with STL classes and containers, knowledge of how pointers work is an important part of a C or C++ education, and for other languages (such as assembly) as well.

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