I need help in understanding how to use/write a pointer. I am only a junior and i have been told this class should not be taught to HS Students and i am lost, in it, and i can't drop the class, whats a girl to do?
High School
Before College
During College
Post College Education
I need help in understanding how to use/write a pointer. I am only a junior and i have been told this class should not be taught to HS Students and i am lost, in it, and i can't drop the class, whats a girl to do?
some people think, when they they first learn pointers, "what will i ever need this for in c++?". Thats what I first thought. First, you will need them because other concepts taught in c++ have pointers, so they are a must. Second of all, to change the values of variables without creating an extra variable, you need to pass pointers to the function. Pointers are simple to understand. First of all, they of course point to the address of a variable or function mainly. You declare it like this: int* ptr; You need the asterisk after the variable type. Now, if you want to change whats in the address, you write something like this: ptr++. It increments the address by 1. If it was pointing to an array: ptr=array[1]; Then: ptr++; will make it point to the third element of the array, or array[2]. Also you can change the value of what is in the pointer like this: *ptr++; that will increment the value of whats in the pointer by 1. Blah blah, that enough or you need more?
What's a girl to do?
Search the web!!! There are literally thousands of tutorials on this kind of stuff.... search the web first, and post if you have trouble with something afterwards.
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
Just remember, a pointer just stores a memory address. That's all. That seemed to clear everything up for me.
Pointers int c++ are handled in a pleasent way.
They get tricky when used in arrays with three or more dimensions, it's not easy passing such arrays to functions.
class dummy{
int nonsense1;
double nonsense2;
public:
void dononsense(int nonsense1){
nonsense1++; nonsense2=1.0;}
}
dummy XYZ;
int main(){
dummy ABC,*UVW; // nonsense1 is private, but ...
UVW = &ABC;
void *ptr = UVW;
int*pt=(int*)ptr;
*pt=2;
XYZ.dononsense(5);
ABC.dononsense(5);
}
QUESTION: Which values are stored in XYZ.nonsense1, XYZ.nonsense2, ABC.nonsense1, ABC.nonsense2 after the calls to dononsense?
(to do less nonsense you may need to know the this-pointer and its use; however, finding the correct answers would prove you're an expert on pointers.)
Pointers are pretty straightforward until you get into multiple indirection. Then they can become a little harder, but not much.
Pointers do exactly that - point at something. They point at memory addresses - you could think of as though they point to one little mailbox in a huge grid of mailboxes. The value at an address would be what's in the mailbox. The address of the mailbox is where the mailbox is.
This stuff really should be taught in High School in order to prepare students for the everchanging everexpanding world of computers. If I had learned this in high school, I would be much farther along than I am. It is a shame that most have to wait until college to learn C/C++. My opinion is that you will learn more on your own, but school will get you into some very good habits and will teach you good program structure (hopefully).
Create some integer pointers and use printf("%p",yourptr) to see it's address and printf("%d",*yourptr) to see what it's value is or what's in it's mailbox.