PDA

View Full Version : i need help with POINTERS IN C++


Bmxpunk86pl
02-04-02, 07:48 PM
Ok i have a book and it trys to explain pointers but i just dont get it!! can anyone please explain to me in detail what they are? Ok i have a example and dont know if its correct:

int pen;
paper*=pen;
pager =&pen;

Bmxpunk86pl
02-04-02, 08:56 PM
Wait never mind. I had __Will__ expain em to me.

Guid0
02-05-02, 07:53 AM
This is in english mostly (http://carbon.cudenver.edu/~tgibson/tutorial/ptr.html)

i bet if your having trouble with pointers is because your over thinking it. the above link seems like it's been writen well and is in english and not jargon.........maybe it will help. to explain pointers to you here would take a page or so. Maybe take a look at some tutorials and if you don't understand something then ask it.

the worst thing is to not ask why..........if you don't get something a specific context and don't ask the rest of what you do will be in vain....For more of a math mind try this place (http://www.cecs.csulb.edu/~cynar/ptrs.html)


This is some ones homework assingment take a look at it (http://www.cs.uregina.ca/links/class-info/170/Pointers/pointers.html)

Bmxpunk86pl
02-08-02, 07:58 PM
ok thanks a lot man, i appreciate it.

RoBo25
02-19-02, 09:01 PM
int pen, *pager;
pen = 4;
pager = &pen;
*pager = 10;
printf("%d", pen);


-----output------
10

Bmxpunk86pl
02-22-02, 11:52 AM
so basically the * just tells something to point to something right?
like:

int var;
int nar;

int var* nar; so basically nar just points to var?

Why do we need pointers? cant we just say the original varibale?

XWRed1
02-22-02, 12:24 PM
Pointers let you do memory management. Specifically, they allow you to keep track of memory that you have dynamically allocated. You can use them to do other tricks too, but thats the main thing they are used for.

When you are working with pointers, after you have declared them, the * is used to dereference them. I.e., if pointer points to some_string, and some_string has a string in it, then "cout << *pointer" would output that string. But "cout << pointer" would output the memory address.

Bmxpunk86pl
02-22-02, 02:26 PM
oh alright. So basically pointers for example if u wanted to access all the data on a harddrive you would just "point" to it, insted of having ur own copy right?

XWRed1
02-22-02, 03:50 PM
Uhh..... I don't think you'd be able to do that. They point to locations in memory. If you wanted to get sutff off the hd, you'd have to go through the apis in the OS, whether they be Win32, Posix, or whatever.

VotTak
02-23-02, 01:58 AM
This book will help to get to the pointers :
Introductory C : Pointers, Functions, and Files
by Richard Petersen. This is just the best book about C.

Bmxpunk86pl
02-23-02, 11:11 AM
I dont know man, the last time i checked i was trying to learn C++, not C. But are the pointers the same in C and C++?

VotTak
02-23-02, 11:39 AM
Pointers and their use is almost the same. A small differences are in C++ when we are talking about member pointers.
You cannot jump to learning C++ without knowledge about pointers. Or let's put it simple... It's useless.
And dont forget that C++ is "enhanced C. So, you gotta know C.
To programm in C++ you have to have a "new" way (object ) of thinking( that is important).

Bmxpunk86pl
02-23-02, 12:36 PM
i dont know, the book im readin says that its pointless to learn C before u learn c++, dont wanna argue with you (unless u wanna,lol) but i find C pretty damn different from C++ because i tried some C too.

XWRed1
02-23-02, 03:52 PM
I learned C++ without knowing C. I still don't know C, beyond the most basic things. The only things I really know about in C are printf, scanf, malloc, and free. I wish I knew C, though.

Bmxpunk86pl
02-23-02, 07:16 PM
so basically the * holds the address of something? For example:

int age =15;
int *myage = &age;

so basically myage is a pointer and a pointer holds the address of something?

VotTak
02-23-02, 07:24 PM
Quoting
>>>>>>>>>>>>>>>>>>>>
that its pointless to learn C before u learn c++,
<<<<<<<<<<<<<<<<<<<<

Whoever said that mean that 2 languages are very different. They require different way of thinking. Thinking in C and thinking in C++ is not the same.
The thing is that when you are coming to the low level of C++ it is nothing else but plain C.

And I believe that you can really easy "programm" in C++ without knowing C by using STL, RW some "homecooked" classes and creating wrappers for them etc.

But that is it. When it comes to the point that you have to create something which is less standard you have to be able to dig in and create methods where you inplementing plain C.

I'm not going to argue about C/C++.
I know that. That is my job. That is what I'm getting paid for.

Stolid
02-23-02, 09:10 PM
I'd like to see someone here program C++ without knowing some C. I'll bet you it's near impossible.
Why?
C++ uses C (without getting overly technical) for:
base variable declaration
structure
preprocessor definitions
etc.

C++ includes new features (i.e. Classes being the most obvious) and new libraries using the new features (i.e. iostream.h).

So if you learn C++ without learning C, you'll have what amounts to... er... nothing. The books that say it's 'pointless' to learn C if you know C++ are refering to the libraries.

And I'd still recommend a 'dedicated' C course/book. Sometimes it's better to use it's libraries/methods than C++'s.

Bmxpunk86pl
02-23-02, 09:22 PM
but when u learn C++ it teaches u thebase variable declaration
structure, preprocessor definitions too. Alright im startin a post in debates about this, see u all there.

Bmxpunk86pl
02-23-02, 09:22 PM
but when u learn C++ it teaches u thebase variable declaration
structure, preprocessor definitions too. Alright im startin a post in debates about this, see u all there. When did this turn form i need help with pointers to u need to know C before C++?

VotTak
02-23-02, 09:25 PM
Thnx, Stolid.

Bmxpunk86pl
02-23-02, 09:29 PM
what the hell, i posted twice sorry. Well see, just meet me in the debates. Now can anyone answer my pointer question?

VotTak
02-23-02, 10:12 PM
Ok I'm gonna start and someone else will continue.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
You write:
int MyVar;

That means: Reserve/create memory space which is able to hold a variable of size int and name it MyVar.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Now let's write something else:
int* MyPtr;

That means: Reserve/create a variable which is able to hold the memory address of another variable/const of datatype int and name it MyPtr.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Another line:
MyVar = 10;
That means: Get to the memory location MyVar and place value 10 there.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Another line:
MyPtr = &MyVar;

That means: Get the address of memory location of MyVar assign it to MyPtr. Now MyPtr holds the address of MyVar.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Now let's say you wanna change the value 10 of variable MyVar to value 11:

1 method: ( I already explained that line )
MyVar = 11;

2 method
*MyPtr = 11;

That means: Get the value of MyPtr which is an address of another variable ( in our case MyVar ) than go to that memory address and place value 11 into that memory place.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

That is it . That's the base.

But I believe that much better results you will get when you start reading and asking questions about an exact topic, sentence, fraze etc ... you didn't get and need an explanation.

XWRed1
02-23-02, 10:23 PM
and if he were to leave the * off from the beginning of the pointer name when he was changing the value to 11, then he would be making the pointer point to whatever was at memory address "11" rather than what is at the memory address of MyVar.

VotTak
02-23-02, 10:53 PM
hehe ... not always desired effect ...huh?

Bmxpunk86pl
02-24-02, 11:33 AM
Alright!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I understand the POINTERS NOW!!!!!!!!!!!!!!!!!!! thanks for ur explanations xwred1, votTak, stolid, guid0, robo25!!.

VotTak
02-24-02, 12:00 PM
I'm glad
:beer: