C++ Pointers
3 posters
:: C Languages :: C++ :: Help!
Page 1 of 1
C++ Pointers
Could someone help me understand what they are for? I read about them in the C++ For Dummies book, but didn't understand them. I have nothing to elaborate on. Just help me begin to understand C++ Pointers in general.
Re: C++ Pointers
Spam posts from user, "IAmANerd" removed; archived. (https://programmingforums.forumotion.com/t52-iamanerd-insults)
Re: C++ Pointers
Ugh pointers useful but annoying.
In simple terms a pointer is a variable that is equal to the memory address of another variable.
For example:
int x = 5;//For this example let's say that x's memory address is say 360.(It is a a hexadeciaml in reality though.(I think.))
int *aPntr = &x;
//aPntr would be equal to x's memory loaction or 360
Does that make sense I can go a little more in depth if you like.
In simple terms a pointer is a variable that is equal to the memory address of another variable.
For example:
int x = 5;//For this example let's say that x's memory address is say 360.(It is a a hexadeciaml in reality though.(I think.))
int *aPntr = &x;
//aPntr would be equal to x's memory loaction or 360
Does that make sense I can go a little more in depth if you like.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Pointers
I agree.legolizard wrote:Ugh pointers useful but annoying.
Yes it is. That is one thing I DID understand about pointers.legolizard wrote:...(It is a a hexadeciaml in reality though.(I think.))...
________
Okay. That is great, but what is the point of pointers? (no pun intended)
Re: C++ Pointers
It may seem that pointers are useless, and for all intents and purposes they are for simple data types such as integers or chars etc. However, when you get into more complex data types, pointers are absolutely essential. Would you like me to explain them they are a little more difficult than say the average array. I'm not very fond of them either but if you want I can show you.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Pointers
The Dummies book had a basic example, but I didn't understand it, so if you could give me a basic example that I might understand, please give it to me.legolizard wrote:It may seem that pointers are useless, and for all intents and purposes they are for simple data types such as integers or chars etc. However, when you get into more complex data types, pointers are absolutely essential. Would you like me to explain them they are a little more difficult than say the average array. I'm not very fond of them either but if you want I can show you.
Re: C++ Pointers
To be honest I thought you wanted to know what they are used for not an example but, meh.
You could do this:
You could do this:
- Code:
#include <iostream>
int getValueStored(int *address)
{
return *address;
}
int main()
{
int num1 = 5;
std::cout << "The value of num1 is : "<<num1<<std::endl;
int *num1Address = &num1;
std::cout << "Num1's memory address is : " <<num1Address <<std::endl;
char whatToDo;
std::cout << "Type 'y' to determine what is stored at this address " <<num1Address << "Type 'n' to not."<< std ::endl;
std::cin>>whatToDo;
switch(whatToDo)
{
case 'y':
std::cout<<getValueStored(num1Address)<<std::endl;
case 'n':
//Does nothing...
};
return 0;
}
Last edited by David B on Sat Aug 27, 2011 11:48 pm; edited 1 time in total (Reason for editing : Code Placed In BBCode "[code]" tags.)
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Pointers
Would you mind taking your code, and commenting on the parts which are pointer-related? I found that very confusing.
Re: C++ Pointers
Sorry.
- Code:
#include <iostream>
using namespace std;
//In my above program I did not add this. Makes things a little easier to read :P
//Here we have a function that returns an integer type and takes a pointer as a parameter.
int getValueStored(int *address)
{
return *address;//Here the asterisk is called the dereferecnce operator and does the reverse of what the pointer does...
//Basically finds the value stored in the variable the was "pointed" to.
}
//Our engine of the program. ;D
int main()
{
int num1 = 5;//Here we decalre a variable named num1 and initialize it to 5.
cout << "The value of num1 is : "<<num1<<endl;//Basic cout statment...
int *num1Address = &num1;//Here we use a pointer. Note the asterisk by the new varibale's name and the ampersain by already declared "num1."
cout << "Num1's memory address is : " <<num1Address <<endl;//Basic cout statment...
char whatToDo;//Here we declare a char.
cout << "Type 'y' to determine what is stored at this address '" <<num1Address << "' Type 'n' to not."<< endl;
cin>>whatToDo;//Basic input...
//Here is what is called a switch statment. Have you learned about these? They are very useful when dealing with chars and ints.
//Basically they are if-statment but are only for ints/chars.
switch(whatToDo)
{
case 'y'://If '(y')...
cout<<getValueStored(num1Address)<<endl;//Here we call our function decalred earlier. It will take the parameter 'num1Address' which is a pointer.
case 'n'://If('n')...
cout<<"Good bye!"<<endl;//Basic cout statment...
break;//Breaks out of switch statment.
};
return 0;
}
Last edited by legolizard on Sun Aug 28, 2011 11:54 am; edited 3 times in total (Reason for editing : Forgot break statment. :P)
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Pointers
yeah I havn't found out what pointers are interly useful for yet but I'm sure I will soon XD. Hopefully not too soon though... anyways thenewbostons tutorial on pointers is pretty good you should check it out. Just type in 'Buckys C++ Programming Tutorials Introduction to Pointers'
Nighthawk0973- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: C++ Pointers
Okay so I was digging through some notes from my first C++ class(Yup I still have them. ). My notes say that point ers are actually very useful (I think I put that in there to make the teacher happy though. ) I did not list any uses go figure, but I did say that pointers are more efficent when passing parameters to a function, and I quote:
"Passing name of var.[variable]to func.[function] func. makes copy of var."
"Passing pointer gives func. acess to actual var."
"Comp. uses less memory with pointer."
Don't know if you understand that but basically pointers can be used as a way to make your program more efficent/faster when passing them as a parameter because it gives the function acess(indirectly of course) to the variable being "pointed to" while when you just pass in the variable the function has to go make a copy and then execute stuff. There are other uses such as, in linked lists but they are kind of hard. =\
"Passing name of var.[variable]to func.[function] func. makes copy of var."
"Passing pointer gives func. acess to actual var."
"Comp. uses less memory with pointer."
Don't know if you understand that but basically pointers can be used as a way to make your program more efficent/faster when passing them as a parameter because it gives the function acess(indirectly of course) to the variable being "pointed to" while when you just pass in the variable the function has to go make a copy and then execute stuff. There are other uses such as, in linked lists but they are kind of hard. =\
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Pointers
I have yet to make my own program that requires the use of pointers. I have written examples from my dummies book though.legolizard wrote:Okay so I was digging through some notes from my first C++ class(Yup I still have them. ). My notes say that point ers are actually very useful (I think I put that in there to make the teacher happy though. ) I did not list any uses go figure, but I did say that pointers are more efficent when passing parameters to a function, and I quote:
"Passing name of var.[variable]to func.[function] func. makes copy of var."
"Passing pointer gives func. acess to actual var."
"Comp. uses less memory with pointer."
Don't know if you understand that but basically pointers can be used as a way to make your program more efficent/faster when passing them as a parameter because it gives the function acess(indirectly of course) to the variable being "pointed to" while when you just pass in the variable the function has to go make a copy and then execute stuff. There are other uses such as, in linked lists but they are kind of hard. =\
Re: C++ Pointers
Hey those notes are helpful. A pointer can be passed in a function giving you access to the variable! Wow I didn't even have to read your plain english lol. Anyways thanks for sharing this with me. So lucky, I sit here doing Math, and I don't even get to take a programming class with it? actually now that you think about it I would hate to take a programming class. I prefer video tutorials, they explain things so much better. Who cares what it's really called. The teachers might call it the 'Unary Scope Resolution Operator' but we call it the 'Double Colon' or '::'.
Plus video tutorials are so much more entertaining.
Plus video tutorials are so much more entertaining.
Nighthawk0973- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: C++ Pointers
I like to combine all resources that are available to me.Nighthawk0973 wrote:Hey those notes are helpful. A pointer can be passed in a function giving you access to the variable! Wow I didn't even have to read your plain english lol. Anyways thanks for sharing this with me. So lucky, I sit here doing Math, and I don't even get to take a programming class with it? actually now that you think about it I would hate to take a programming class. I prefer video tutorials, they explain things so much better. Who cares what it's really called. The teachers might call it the 'Unary Scope Resolution Operator' but we call it the 'Double Colon' or '::'.
Plus video tutorials are so much more entertaining.
Re: C++ Pointers
So as I was making a new program it dawned on me why I thought pointers were useful. Dynamic memory allocation. I actually COMPLETELY forgot about it, and so I have made this tutorial : https://programmingforums.forumotion.com/t94-how-todynamic-memory-allocation#502
Last edited by legolizard on Sun Oct 02, 2011 12:30 am; edited 1 time in total
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Pointers
When you finish it, post it. I would love to see it!legolizard wrote:So as I was making a new program it dawned on me why I thought pointers were useful. Dynamic memory allocation. I actually COMPLETELY forgot about it, and so I have made this tutorial : //I have not made it yet
:: C Languages :: C++ :: Help!
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|