Would you like to react to this message? Create an account in a few clicks or log in to continue.

C++ Pointers

3 posters

 :: C Languages :: C++ :: Help!

Go down

C++ Pointers Empty C++ Pointers

Post  David B Fri Aug 26, 2011 10:01 pm

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.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Fri Aug 26, 2011 11:48 pm

Spam posts from user, "IAmANerd" removed; archived. (https://programmingforums.forumotion.com/t52-iamanerd-insults)
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  legolizard Sat Aug 27, 2011 12:06 am

Ugh pointers useful but annoying.Sleep Sleep



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
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sat Aug 27, 2011 1:04 am

legolizard wrote:Ugh pointers useful but annoying.Sleep Sleep
I agree.



legolizard wrote:...(It is a a hexadeciaml in reality though.(I think.))...
Yes it is. That is one thing I DID understand about pointers.



________



Okay. That is great, but what is the point of pointers? (no pun intended)
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  legolizard Sat Aug 27, 2011 4:35 pm

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
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sat Aug 27, 2011 5:55 pm

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.
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.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  legolizard Sat Aug 27, 2011 7:36 pm

To be honest I thought you wanted to know what they are used for not an example but, meh.



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
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sat Aug 27, 2011 11:48 pm

Would you mind taking your code, and commenting on the parts which are pointer-related? I found that very confusing.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  legolizard Sun Aug 28, 2011 12:53 am

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
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sun Aug 28, 2011 2:21 am

Thanks! I am going to try compiling it now!
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  Nighthawk0973 Sun Aug 28, 2011 3:46 pm

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... Smile anyways thenewbostons tutorial on pointers is pretty good you should check it out. Just type in 'Buckys C++ Programming Tutorials Introduction to Pointers'
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  legolizard Sun Aug 28, 2011 6:19 pm

Okay so I was digging through some notes from my first C++ class(Yup I still have them.Smile ). My notes say that point ers are actually very useful (I think I put that in there to make the teacher happy though. Razz) 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. =\
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sun Aug 28, 2011 6:28 pm

legolizard wrote:Okay so I was digging through some notes from my first C++ class(Yup I still have them.Smile ). My notes say that point ers are actually very useful (I think I put that in there to make the teacher happy though. Razz) 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. =\
I have yet to make my own program that requires the use of pointers. I have written examples from my dummies book though.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  Nighthawk0973 Mon Aug 29, 2011 3:04 pm

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? Mad 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.
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sat Sep 03, 2011 6:26 pm

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? Mad 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.
I like to combine all resources that are available to me.
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  legolizard Sun Oct 02, 2011 12:03 am

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
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  David B Sun Oct 02, 2011 12:06 am

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 Razz
When you finish it, post it. I would love to see it!
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

C++ Pointers Empty Re: C++ Pointers

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 :: C Languages :: C++ :: Help!

 
Permissions in this forum:
You cannot reply to topics in this forum