Arrays and Pointers
Hey,
I have read this same tutorial on Arrays 2 times and I do not understand them at all! I don't know, maybe it is because I don't understand pointers yet?(pointers are confusing! I forget them everytime and can't figure out when to use them)Maybe it is because this tutorail isn't going over them good or something but all I know is that I don't understand them.
so if someone could point me in the right direction to a tutorial about them then that would rock!
Also,
If someone could point me to a good online book then that would be good also.
Chad
Here's a book.
It's a good one, enjoy.
Here's a tutorial that looks decent and is specifically about pointers.
I found it by googling "c++ pointer tutorial".
It's a good one, enjoy.
Here's a tutorial that looks decent and is specifically about pointers.
I found it by googling "c++ pointer tutorial".
i just happen to have a couple links handy...
http://www.cplusplus.com/doc/tutorial/
http://www.cprogramming.com/tutorial.html
http://www.parashift.com/c++-faq-lite/index.html
http://www.cplusplus.com/doc/tutorial/
http://www.cprogramming.com/tutorial.html
http://www.parashift.com/c++-faq-lite/index.html
I have yet to find a tutorial - or a book, for that matter - that did an even remotely good job of explaining pointers. Many try and say they 'point' to a position in memory. That is perhaps why they are so confusing. So I will tell you what a pointer really is: An integer. A pointer is just an integer. You can envision the entire computers memory as a single gigantic array; a pointer is just an index into it. The different syntax is needed to distinquish between when you want the value of the pointer, and the value of whatever is at the address in the pointer. It is the same as the difference between 'i' and 'somearray'. There is nothing at all special about the syntax. It is completly arbitrary.
Arrays. Here:
Array's are just variable with multiple values. To declare a variable an array, let's say..an integer! You could do this:
int Nums[3];
That declares an integer variable with three indexes. You can also use zero, which makes it four indexes. You can do this then:
DON'T STEAL THIS SMALL TUTORIAL AND CALL IT YOUR OWN, THIS IS GOING INTO A SMALL MS WORD DOC THAT WILL TEACH THE BASICS OF C++!
#include <iostream>using namespace std;int main(){ char Hello[5]; // Character variable named Hello with array index of 5 char World[5]; // Character variable named World with array index of 5 Hello[1] = 'H'; // Set a specific index to a specific value Hello[2] = 'e'; // Set a specific index to a specific value Hello[3] = 'l'; // Set a specific index to a specific value Hello[4] = 'l'; // Set a specific index to a specific value Hello[5] = 'o'; // Set a specific index to a specific value World[1] = 'W'; // Set a specific index to a specific value World[2] = 'o'; // Set a specific index to a specific value World[3] = 'r'; // Set a specific index to a specific value World[4] = 'l'; // Set a specific index to a specific value World[5] = 'd'; // Set a specific index to a specific value cout<<Hello[1]<<Hello[2]<<Hello[3]<<Hello[4]<<Hello[5]<<" "; cout<<World[1]<<World[2]<<World[3]<<World[4]<<World[5]; cin.get();}
Array's are just variable with multiple values. To declare a variable an array, let's say..an integer! You could do this:
int Nums[3];
That declares an integer variable with three indexes. You can also use zero, which makes it four indexes. You can do this then:
#include <iostream>using namespace std;int main(){ cout<<"Input 1: "; cin>>Nums[0]; // Input index 0 cout<<"\nInput 2: "; cin>>Nums[1]; // Input index 1 cout<<"\nInput 3: "; cin>>Nums[2]; // Input index 2 Nums[3] = Nums[0] + Nums[1] + Nums[2]; // Index 3 is the sum cout<<Nums[0]<<" + "<<Nums[1]<<" + "<<Nums[2]<<" = "<<Nums[3]; cin.get(); return 0;}
DON'T STEAL THIS SMALL TUTORIAL AND CALL IT YOUR OWN, THIS IS GOING INTO A SMALL MS WORD DOC THAT WILL TEACH THE BASICS OF C++!
Thanks everyone very much.
DUH! I mean to google that but for somereason I just didn't. lol.
Thnanks again.
Chad
DUH! I mean to google that but for somereason I just didn't. lol.
Thnanks again.
Chad
char Hello[5]; // Character variable named Hello with array index of 5 char World[5]; // Character variable named World with array index of 5 Hello[1] = 'H'; // Set a specific index to a specific value Hello[2] = 'e'; // Set a specific index to a specific value Hello[3] = 'l'; // Set a specific index to a specific value Hello[4] = 'l'; // Set a specific index to a specific value Hello[5] = 'o'; // Set a specific index to a specific value
Way to buffer overrun. A character array with 5 elements has elements 0, 1, 2, 3, and 4, it has no element 5.
Quote:
DON'T STEAL THIS SMALL TUTORIAL AND CALL IT YOUR OWN, THIS IS GOING INTO A SMALL MS WORD DOC THAT WILL TEACH THE BASICS OF C++!
Quote:
Oh. Hehehe...never trust finding info on google, even if you put it into your own words.
So you can take someone elses "tutorial" and retype it "in your own words" but you don't want someone to do that to you? ;P
To whom it may concern: a couple minutes ago I had a small post in there that stated that you couldn't index Nums[3] in orcfan's example; it would cause an error, because of C/C++ zero-based indexing, yadda yadda yadda. HOWEVER...out of curiosity, I stuck it in GCC to see what would happen, and it ran without an error of any sort. That DOESN'T mean that you can always index element [N] in an array declared of length N -- in fact, you should never do it. Just, in this case, the memory leak went undetected.
Weird, huh?
Weird, huh?
Not really, if you access the memory, it will change something out there, however depending on how the memory was allocated, you could be writing over the first element of one of the other character arrays or over the top of something else. The only time you will get an access violation will be when you try to reference memory not currently claimed by the application.
So for instance:
In this example, on most compilers test2[0] will equal 'C', however it is wrong, and you should never, ever, ever even consider doing it. If you do you should poke yourself in the eye or something.
So for instance:
struct test{char test[3];char test2[3];}test Test1;Test1.test[3] = 'C';
In this example, on most compilers test2[0] will equal 'C', however it is wrong, and you should never, ever, ever even consider doing it. If you do you should poke yourself in the eye or something.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement