•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 361,565 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,046 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4095 | Replies: 19 | Solved
![]() |
•
•
Join Date: Dec 2004
Location: Hiawassee, Georgia
Posts: 129
Reputation:
Rep Power: 4
Solved Threads: 1
I have been reading about pointers, which IS confusing. I'm sure there is a purpose, but I don't know why I need them yet. For example
I think I might know what pointers do. I don't, however, know what the practical use of a pointer is. From what I've learned about pointes, is that they directly give the value of whats at that location in memory.
My question is: what is the practical use of pointers?
Examples && Explanations = *Welcomed :cheesy:
var1 = 23; // assigning 23 to var1 var2 = var1; // assigning the value of var1 to var2 var3 = &var1; // assigning the address of var1 to var3
I think I might know what pointers do. I don't, however, know what the practical use of a pointer is. From what I've learned about pointes, is that they directly give the value of whats at that location in memory.
My question is: what is the practical use of pointers?
Examples && Explanations = *Welcomed :cheesy:
If in doubt, reach into the trash can and remove the user guide.
•
•
Join Date: Nov 2004
Posts: 13
Reputation:
Rep Power: 4
Solved Threads: 1
i havent slept in days and i hope i dont say something completely stupid here:
pointers contain the memory location of a variable. one of the reasons that this is useful concerns memory efficiency. if you're passing a variable to a function then the function has to make another copy of that variable and if that variable is an object of type NUCLEAR_REACTOR_PLAN, then you might run out of memory mighty fast. but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
pointers contain the memory location of a variable. one of the reasons that this is useful concerns memory efficiency. if you're passing a variable to a function then the function has to make another copy of that variable and if that variable is an object of type NUCLEAR_REACTOR_PLAN, then you might run out of memory mighty fast. but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
•
•
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 1
I got the similar problem with you, Geek-Master. What kaiser<lucy> said is still a little bit abstract for me.
So Geek-Master have you tried the dynamic memory allocation using new and delete operator? They could only deal with pointers.
Now my own problem comes. Books say we can use new operator to reserve memory for array of unknown size like this:
<code>
int* ptr = new int[100]; //the array ptr has interger type and 100 elements
</code>
Now another version:
<code>
int n;
int ptr[n];
cin >> n; //ask user to enter size of array, now array has size
</code>
So what is the difference between the two versions of array declaration?
In both cases the array is declared but doesn't have defined-size unless it is determined during runtime.
So Geek-Master have you tried the dynamic memory allocation using new and delete operator? They could only deal with pointers.
Now my own problem comes. Books say we can use new operator to reserve memory for array of unknown size like this:
<code>
int* ptr = new int[100]; //the array ptr has interger type and 100 elements
</code>
Now another version:
<code>
int n;
int ptr[n];
cin >> n; //ask user to enter size of array, now array has size
</code>
So what is the difference between the two versions of array declaration?
In both cases the array is declared but doesn't have defined-size unless it is determined during runtime.
•
•
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 1
•
•
Join Date: Dec 2004
Location: Hong Kong, SAR
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 1
Let me quote the lecture notes from my school:
The above example they call it 'dynamic array'. The value of n is essentially unknown during compilation time. Am I right? Now look at this:
The size of n is also not known until the programme is run. Then what is the point to use pointer and new operator to declare the array in the first case?
int main(void)
{
cout << “How many students? “;
cin >> n;
//Declare an array to hold students' information
int *grades = new int[n];
. . . . . .
. . . . . .
. . . . . .
}int main(void)
{
cout << “How many students? “;
cin >> n;
//Declare an array to hold students' information
int grade[n];
. . . . . .
. . . . . .
. . . . . .
} >Then what is the point to use pointer and new operator to declare the array in the first case?
Because the second case is ill-formed and thus, illegal in C++. Array sizes in C++ must be constant integral values. If it works when you try it then that's because your compiler allows it as an extension. Only the first case is guaranteed to be correct (assuming n is already defined).
Because the second case is ill-formed and thus, illegal in C++. Array sizes in C++ must be constant integral values. If it works when you try it then that's because your compiler allows it as an extension. Only the first case is guaranteed to be correct (assuming n is already defined).
Member of: Beautiful Code Club.
•
•
Join Date: Dec 2004
Location: Hiawassee, Georgia
Posts: 129
Reputation:
Rep Power: 4
Solved Threads: 1
Pointers are still a vast subject that I am still working on to learn. I still need time to look over all of the technical writing of it all. I just needed to grasp the purpose of them.
by: kaiser<lucy>
I can kinda see the purpose now, dealing with memory usage. Thanks :cheesy:
•
•
•
•
but if you pass a variable which points to the NUCblahblahblah variable's address, then you dont have to make the copy, you can just work with the original. Another potential use of pointers is in the implementation of a template class.
I can kinda see the purpose now, dealing with memory usage. Thanks :cheesy:
If in doubt, reach into the trash can and remove the user guide.
pointers are c++ most useful things and is what sets it aside from other languages. Just to name a few uses:
Dynamic arrays
Linked lists
Memory management (eg. passing pointers rather than objects, alloc'ing memory ect..)
Binary Trees and Heaps (great for gaming)
Speed improvement (you can simplify code in some cases with pointers/function ptrs)
the last few being especially important in game programming.
If you still arent sure they are useful then try this: make a list class (or a set of functions) that can grow and shrink in size BUT not lose the contents (so it is NOT dynamic arrays...) without using STL (which, I believe, is a steam based linked list anyway). TIP: You need a pointer to the next and previous list item for it to work!
Dynamic arrays
Linked lists
Memory management (eg. passing pointers rather than objects, alloc'ing memory ect..)
Binary Trees and Heaps (great for gaming)
Speed improvement (you can simplify code in some cases with pointers/function ptrs)
the last few being especially important in game programming.
If you still arent sure they are useful then try this: make a list class (or a set of functions) that can grow and shrink in size BUT not lose the contents (so it is NOT dynamic arrays...) without using STL (which, I believe, is a steam based linked list anyway). TIP: You need a pointer to the next and previous list item for it to work!
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: passing an array of structs from a member function
- Next Thread: Editor Question



Linear Mode