im new here

Hi all,

I'm new here.
Let me introduce myself.

I am 27f, part time model.
I recently started on C++ with no knowledge.

After seeing this forum, I am glad to see everyone is helpful, and therefore I also created an account to join this family.

Believing that basics are important, I would not jump to the next topic before I can fully understand the current..and I am in a bad situation now.

Currently I am stuck at the topic pointers. Where in the tutorial provided by cplusplus.com, I am stuck for 2 days at the pointers topic and is still unable to understand.

Can anyone guide me on how to better understand that topic?
closed account (Lv0f92yv)
Welcome to the forums.

I assume you have read through the topic on this site, and have understood the basics up until that point.

Every variable has an 'address' in memory when it is declared. Primitive types are allocated at runtime to the stack (assuming they are declared inside a function).

int a;
allocates space for an integer on the stack, the name we use to get at this space is 'a' (in our program known as variable).

cout << &a;

Do this in your code, and run it. You will see an address printed to the screen (normally in hexadecimal format - though the take away here is that a variable name with an ampersand in front is read as 'the address of').

If I can write to this address, (change the contents of this address), I can change the contents of the variable. (This is because the variable refers to that address in memory, which holds contents that are garbage until initialized with:

1
2
int a;
a = 5;


so now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int a;
	a = 5;
	int* ptrA = &a; //int* declares a variable to point to the address of a.
	cout << ptrA << "\n"; //address of a
	cout << &a << "\n"; //we see the address of a,
// not the contents (which would be 5)
	cout << *ptrA << "\n"; //'dereference' the address 
//held in ptrA (which we said is the address of variable a). 
//This will get us to the contents at the address a (which is 5)
}


Play around with the above code, using and removing & and * and see what you get (compiler will complain if you try to dereference (*) a non-pointer variable.

Edit: Pointers can be thought of an alternative way to access variables. A pointer variable holds the address of some other variable of the appropriate type, and when dereferenced, pointer variables can be used to access the contents at whatever address they point to.
Last edited on
1. The best way to learn pointers is to write code and see if it behaves the way you expect.
2. When you run into problems you don't understand, post specific examples or questions here for help.
3. Goto 1. until you feel comfortable that you understand pointers.
Last edited on
If you are really starting to learn C++ with no prior programing knowledge or experience, then I would suggest you skip over the topic of pointers. There is SOOOOO much more to learn and frequently use than pointers. Learn about variables, their types, arrays, vectors, for loops, while loops, input, output, structures, classes, constructors, destructors, graphics, etc. Don't bother with pointers yet.
Welcome to the forum. We hope you have as miserable a time as possible and that you suffer throughout your learning of C++ a most pleasant stay.

I agree with nathan10 on this point. Pointers are important to know so that you can recognize them, but unless you're insane like a few of us here you can do without using them. If after Desh and the tutorial you have trouble understanding them... well, you can try this:
http://www.augustcouncil.com/~tgibson/tutorial/ptr.html
And if still not, skip them.

-Albatross
Last edited on
Actually, I agree with nathan10 and Albatross.

Pointers are central to C, but less crucial in C++.
There really is a ton of other stuff to learn in C++.

BTW, do you have experience with other programming languages?

C++ is a bit tough as a first language (some would say it's tough as the tenth language).
The following is a well-known video that has helped many people to understand the concept behind pointers:
http://www.cs.stanford.edu/cslibrary/PointerFunCppBig.avi
closed account (Lv0f92yv)
It is true that pointers are not something most people start with when using cplusplus. It is also true that they are not as mystic as they seem. And I have watched that video Athar posted above - very good video. Forgot about that one...

Maybe I'll do a more comprehensive tutorial (than the one I had above) and post it back... I have thought about it for a while. The tut on this site is great, but maybe if I try and explain it differently and drive home the basic points people will have an easier time.
Last edited on
closed account (D80DSL3A)
Pointers may be a confusing subject but I would not suggest that they are irrelevant or pointless (pun intended).
The abstraction being made with pointers allows elegant solution methods to tough problems. Skipping pointers is like skipping calculus because algebra has always been good enough. You leave a wide range of problems behind.
Besides, how else to take advantage of encapsulation and virtual functions?

Example: In a video game, a number of ships of several different class types move on a variety of path types. All ships are derived from an abstract base ship class, likewise for the paths.
Using pointers to arrays of pointers to the abstract classes involved I can write my game logic as:
1
2
3
4
5
6
7
8
9
for( int j= 0; j< Npaths; j++)
    if( ppPath[j]->inUse )
        for( int k= 0; k< ppPath[j]->Nships; k++)
           if( ppPath[j]->ppShip[k]->inPlay )
          {
               ppPath[j]->move(k);
               ppPath[j]->ppShip[k]->hittest();
               ppPath[j]->ppShip[k]->draw();// maybe this goes in separate render loop
          }


Where move() is virtual in the path classes and hittest() and draw() are virtual in the ship classes.
How else can this be done?
Also, hello to teapotz, 27f part time model.

EDIT: Forgot to mention that above pointers are also being used for the important task of handling the dynamic allocation and deletion of paths and ships as they come into, and go out of, play. Pointers are NEEDED for use with 'new' and 'delete' aren't they?
Last edited on
Something amusing to watch -> http://www.youtube.com/watch?v=i49_SNt4yfk

EDIT: Oh, I just realized this is the same video as the one Athar posted...

PS: I wonder if the OP would get that much response if he she hadn't stated this:

teapotz wrote:
I am 27f, part time model.
Last edited on
closed account (D80DSL3A)
@m4ster0shi: I would have thought the 'f' in 27f stands for "female". Is your presumption that the OP is male inside info or sarcasm? Also, no ingenious remarks about the topic (pointers)?
Last edited on
So what if he/she's female? The OP is still human. We're a nice community. Move along now. ;)

EDIT: You lot are so desperate. CS1B is a sausage fest, you're not homosexual, you're the kids or men that everyone dismisses as "uncool" and "nerdy". Yep. I can see the setup.

-Albatross
Last edited on
closed account (Lv0f92yv)
Speaking of OP, are they still following the topic? Is this helping teapot?
@Desh:

If I were you, I'd write a brief tutorial on using pointers and PM it to her. It's not out of the question that she might appreciate it to the point that she gives you her e-mail or fb or something like that. You never know... :)
hello everyone,

thanks alot for all the nice info u all provided me.
still i believe basic are important and i will understand it before moving to the next topic.

anyway, i tot that introducing myself first in the community and in my first post would be good..guess it turns out theres weird comments.
-.- wont do this again.

time to read all the info everyone provided. ^^
closed account (Lv0f92yv)

If I were you, I'd write a brief tutorial on using pointers and PM it to her. It's not out of the question that she might appreciate it to the point that she gives you her e-mail or fb or something like that. You never know... :)


..... Easy now.

I might write something... I have to find the time to sit and write a decent one.

teapotz, introducing yourself was polite, I think these guys are kidding.

Let us know if there's something programming specific we can help you with.

Topic archived. No new replies allowed.