c++ array help

Started by
24 comments, last by Zakwayda 16 years ago
ok, i am learning c++ from a book, c++ for dummies, and i am at where you learn about arrays, but the book does a horrible job at explaining it, so what is an array, and how would i code one?
Advertisement
Get a better book since if you are new to C++ you should be learning to use vector and not array!
Just the fact that you are confused by arrays should tell you to avoid them unless necessary!

To net this out, arrays really are evil. You may not think so if you're new to C++. But after you write a big pile of code that uses arrays (especially if you make your code leak-proof and exception-safe), you'll learn — the hard way. Or you'll learn the easy way by believing those who've already done things like that. The choice is yours.


[Edited by - daviangel on March 1, 2008 7:17:51 PM]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
vector isnt even in the book....

what is a vector
Quote:Original post by daviangel
Get a better book since if you are new to C++ you should be learning about vector and not array!

To net this out, arrays really are evil. You may not think so if you're new to C++. But after you write a big pile of code that uses arrays (especially if you make your code leak-proof and exception-safe), you'll learn — the hard way. Or you'll learn the easy way by believing those who've already done things like that. The choice is yours.


No he shouldn't.

If he learns how to use a std::vector and never learns about arrays he will not learn why a vector is a good replacement for an array, what problems there are with arrays.

To the OP:

There are tonnes of links at google, arrays and std::vector. A little searching goes a long way.

Hope that helps,
Quote:Original post by rpstaekwondo
ok, i am learning c++ from a book, c++ for dummies, and i am at where you learn about arrays, but the book does a horrible job at explaining it, so what is an array, and how would i code one?


Pretty simple really. An array is just a series of data elements stored one after the other in memory. You can put anything into arrays, numbers, characters etc..
An array containing 5 integers for example will just have five numbers stored in it, one after the other.

Here's how you would declare, access and modify that array in c++:

#include <cstdio>int main(){  // Declare an array containing five integers  int myArray[5];  // Set the value of each of the five numbers in the array, starting with the first      myArray[0] = 0;   myArray[1] = 5;   myArray[2] = 10;   myArray[3] = 15;   myArray[4] = 20;  // Print the value of each of the five numbers, starting with the first  printf( "1st number is: %i " , myArray[0] )  printf( "2nd number is: %i " , myArray[1] )  printf( "3rd number is: %i " , myArray[2] )  printf( "4th number is: %i " , myArray[3] )  printf( "5th number is: %i " , myArray[4] )}


Quote:Original post by rpstaekwondo

vector isnt even in the book....

what is a vector


A vector is a special C++ data structure that acts much like an array, but with additional benefits. You can resize vectors but you can't resize an array. You can also have checks put into your debug / test code with vectors to make sure you aren't trying to access invalid elements outside of the vector.

I'd stick to learning the basics (Arrays, pointers etc.) before worrying about things like Vectors though. Worry about that stuff later on when you have a leg up in the language!

[Edited by - Darragh on March 1, 2008 6:15:26 PM]
I won't get in the middle of the vector vs. array debate, but you really should ditch that book if it doesn't even mention them.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by Darragh
Quote:Original post by rpstaekwondo
ok, i am learning c++ from a book, c++ for dummies, and i am at where you learn about arrays, but the book does a horrible job at explaining it, so what is an array, and how would i code one?


Pretty simple really. An array is just a series of data elements stored one after the other in memory. You can put anything into arrays, numbers, characters etc..
An array containing 5 integers for example will just have five numbers stored in it, one after the other.

Here's how you would declare, access and modify that array in c++:

*** Source Snippet Removed ***

Quote:Original post by rpstaekwondo

vector isnt even in the book....

what is a vector


A vector is a special C++ data structure that acts much like an array, but with additional benefits. You can resize vectors but you can't resize an array. You can also have checks put into your debug / test code with vectors to make sure you aren't trying to access invalid elements outside of the vector.

I'd stick to learning the basics (Arrays, pointers etc.) before worrying about things like Vectors though. Worry about that stuff later on when you have a leg up in the language!


omg thank you so much, that was exactly the thing i was looking for, thank u
wait, in printf, what does %i do?
how do i store text into an array?


so many questons from a begginer
Well, I would assume that you know your basic data type (like int, double).

One way to store text in an array is to declare the array as type char, like the following:

char city[] = {'D','a','l','l','a','s'};

or as:

char city[] = "Dallas";

Edit: BTW, ask all the questions you want. So many questions from a beginner is not a bad thing. Just proves you want to learn! My .02

This topic is closed to new replies.

Advertisement