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)
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;
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";
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.
char* pc = newchar ;
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:
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.
Thank you very much guys!
Now I understand my mistake in logic was that in the brackets pc0 has to be pc as a short hand, normally.
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.
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