Pointers in C++

Is it true that pointers are the most difficult to understand concept of C++?
I am having a hard time to understand how char pointers work and I use the #include <array> header in order to make it much clearer, but still, I don't exactly understand how to use them, even though in the book I am reading it clearly states that they use the new and delete alone, there is no need to use pointers with them. (C++11 only)
Is it true that pointers are the most difficult to understand concept of C++?
No. They're very simple. They're just often very, very, very badly taught, with horrific analogies and broken comparisons and ridiculous stories about pigeons or boxes that just don't help.

Here is what I usually say to people about pointers: http://cplusplus.com/articles/EN3hAqkS/
std::array hasn't much to do with pointers at all. It contains a fixed sized array internally and doesn't use new or delete.
Binky Pointer Fun Video
http://cslibrary.stanford.edu/104/
Lol, yeah i saw that video, but the real problem with that is that it makes it more complicated, or better saying it is not clear at least to me.
pointers are simple: The value is an index which represents a certain memory address.

The problem is the global nature of the object pointed to. You might not always know who's manipulating the object.

Another problem is the time when to discard the object since often you can't tell who's still accessing the object. A solution to this is a smart pointer:

http://en.wikipedia.org/wiki/Smart_pointer

in other languages there's the 'garbage collector'
thanks for the replies.
I am sorry i did not make myself very clear
I have problems understanding char pointers and string pointers

I know how to use a pointer:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
int a = 3;
int* p = &a;
cout << *p << endl;
int* pn = new int;
cin >> *pn;
cout << *pn;
delete pn;
}
Last edited on
I must be misunderstanding you. How can you know what a pointer is, but suddenly not know when it points to a char?
I mean, making a char is smth like this:

char* pc = new char [2];

Now how am I supposed to use it?
I don't understand the logic.

is it pc0 = 3; ???
pc1 = 2;
Last edited on
If you're just starting out and haven't started making functions and classes yet then pointers won't make much sense. That is, they seem kind of pointless (pardon the pun).
What a pointer does is reference the address of a variable. So if you "dereference" a pointer it means you are accessing the values stored in the address of the original variable so that you can change the original variable's value.

I made this example real quick to demonstrate the basics;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
int var1 = 100; //we make a regular int variable and set it to equal 100
int *pointvar1 = &var1; //we make a pointer using the * sign and set it equal to the address of var1
cout<<"The address stored in pointvar1 = "<<pointvar1<<endl;
cout<<"The address that holds var1's value = "<<&var1<<endl;
cout<<"The \"Dereferenced value\" of *pointvar1 = "<<*pointvar1<<endl;
cout<<" *pointvar1 now equals var1, see "<<var1<<" = "<<*pointvar1<<endl;
*pointvar1 += 200; //we add 200 to the dereferenced pointer
cout<<"We just added 200 to *pointvar1, so let's see what var1 = "<<var1<<endl;
pointvar1 += 100; //add 100 to the pointer itself (which is the address to var1) and see if it still points to var1
cout<<"we just added 100 to the pointer adress so now it equals" <<pointvar1<<endl;
	*pointvar1 += 10;
cout<<"We've changed the pointer's address...\n so does var1 equal *pointvar1 any more? "<<var1<<" = "<<*pointvar1<<endl;
cout<<"Nope, I guess not.\n"; 
	int wait(0);

	cin>>wait;
	return 0;
}

Like I said, pointless right now. But let's say you want to change the value of a variable in your Main() function by making changes in a new() function. That is where pointers will come in handy because of the domains of functions.

My suggestion for now is to try to understand pointers as best you can and if they are too confusing then pass over them for now and just remember that they are there and will come in handy when you get a good understanding of classes and functions.
Last edited on
char* pc = new char [2];
What this is doing is that it allocates an array of 2 char and pc will be pointing to the first char in that array, so if you use the dereference operator on pc you get the first element in the array *pc.

The elements in an array is stored after each other in memory so if you add one to pc you will get a pointer pointing to the second element in the array pc + 1 that you can dereference if you want *(pc + 1).

There is a short hand syntax for this so instead of writing *(pc + i) you can write pc[i].

So setting the first element to 3 and the second element to 2 can be written as:
1
2
pc[0] = 3;
pc[1] = 2;

Actually I just re-read your prompt and I was a bit off on what you were asking. What you're asking about is dynamic memory storage. A pointer to char can store a sentence in an array like using string. you don't have to include any extra libraries to use an array.
A pointer to a char array can store multiple sentences in the array without you having to enter a pre-defined length for each sentence.

where new and delete come in is when you have an extremely long list of names or sentences and you don't know how many there are, so you need to use the memory heap.

I suggest breezing through this section and leaving a bookmark for future reference. I think I have the same book as you and you might want to get into functions and classes first before mucking about with dynamic memory.

(Ivor Horton's beginning visual C++ 2011)
Thank you very much guys!
Now I understand my mistake in logic was that in the brackets pc0 has to be pc[0] as a short hand, normally.

@newbieg
Thanks for the example!
I am reading Stephen Prata's C++ Primer 6th Edition
I am having a very hard time understanding the char pointers and string pointers, or even pointer arithmetic and i believe it is the author's somewhat bad explanation because i have reread that part like 3 times.
I know functions and somehow i know classes from php, but not yet in C++

Either way, now i understand the chars logic, but not string pointers and how am i going to use them?

I am sure that the author has not invested enough time on this very important topic (string pointers and char pointers, but overall he explains very good, please don't get this wrong) and even though they might seem easy, they are a new concept for someone who has some knowledge on languages which don't use pointers. I am finding the idea of the dynamic memory very important, since I knew before the automatic and static memories and I didn't save any memory during run-time, so this is the first time.

I hope you guys understand and pls someone can help with the logic of string pointers? The author still uses chars even though he includes the string header! This is somewhat not logic, at least for me.
Last edited on
but not string pointers and how am i going to use them?
1
2
3
string x; // This makes a string
string* pointerToString; // This makes a pointer
pointerToString = &x; // And now the pointer is pointing at the string 

This is so simple that, again, I must have misunderstood. Why is using a pointer to a string difficult?


I believe he's confusing C-Style strings with string pointers. Essentially, if he already understands it, he understands "string pointers" and character pointers. It just hasn't clicked for him.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>

int main() {
   char *myString = "Hello, World!"; // C-String
   std::cout << myString << "\n"; // Prints Hello, World!

   for (int i = 0; i < strlen(myString); i ++)
      std::cout << myString[i]; // Also prints Hello, World!

   std::cout << "\n";

   while (*myString != '\0')
      std::cout << *(myString ++); // Also prints Hello, World!

   return 0;
}


Edit: Errr, I had problems with that last statement :/
Last edited on
As far as I understand it, you'd use char and string dynamic memory if you start trying to make a word processor or something similar where you need to keep track of an almost infinite amount of written information. So again, I suggest try to understand it while you're reading the chapter, know that it's there, and bookmark the important pages for if you ever actually need it.
The examples they give aren't going to actually need dynamic memory because they need to keep the length of the examples short enough for you to get through. The author is probably including the <cstring> header for string because he wants to start showing string functions like strlen() and strcmp() that will allow you to compare and work with char arrays.
When you start using string it gets a bit weirder talking about tracking handles which are like C++ pointers but a bit different. again, something to bookmark.

Just a note;
<string> and <Cstring> are very different libraries
Last edited on
Topic archived. No new replies allowed.