|
-
March 14th, 2003, 04:50 AM
#1
Help in Pointer
Hi There,
int *i,t;
i=(int *) &t; // What does this statement means.... where we use it.
Thanks and Regards,
Abbas
-
March 14th, 2003, 05:00 AM
#2
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 *.
-
March 14th, 2003, 09:27 AM
#3
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.
-
March 17th, 2003, 05:41 PM
#4
One important concept about pointers is that they point to memory addresses, not the actual values.
Kuphryn
-
March 17th, 2003, 09:36 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|