Public Group

# c++ array help

This topic is 3776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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]

##### Share on other sites
vector isnt even in the book....

what is a vector

##### Share on other sites
Quote:
 Original post by daviangelGet 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,

##### Share on other sites
Quote:
 Original post by rpstaekwondook, 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]

##### Share on other sites
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.

##### Share on other sites
Quote:
Original post by Darragh
Quote:
 Original post by rpstaekwondook, 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

##### Share on other sites
wait, in printf, what does %i do?

##### Share on other sites
how do i store text into an array?

so many questons from a begginer

##### Share on other sites
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

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631786
• Total Posts
3002350
×