C++/hi

Advertisement


Question
can you explain in detail arrays and pointers?like examples how to do them

Answer
Hi, Raymond.

I'll start with arrays and then transition to pointers.

An array is a collection of a data type.  You can create arrays of any defined data type simply by using the bracket operator: [].  Often times you will specify the size of the array by placing an integer inside the brackets: [20].  So, to declare an array of 5 integers, you would write:

   int intArray[5];

You can see how first you specify the type, then you create an identifier for the array (variable name), then you use the bracket operator.  Note that with arrays, you must know the size at compile time, and the size cannot change.  This means that the value inside the brackets cannot be a variable, but must be a fixed value.

You can access each index in the array, again, using the bracket operators.  Please note that C and C++ are 0-based-index languages.  This means that the first index in an array is index 0, and the last index is (size - 1):

   intArray[0] = 1;
   intArray[1] = 2;
   intArray[2] = 3;
   intArray[3] = 4;
   intArray[4] = 5;

The above code demonstrates how to write into each index of the array.  Reading from the array is virtually identical:

   cout << intArray[0];
   cout << intArray[1];
   cout << intArray[2];
   cout << intArray[3];
   cout << intArray[4];

Essentially you can treat it as you would any other variable, you just have to specify the index.

You can initialize the array by using the = sign and curly braces:

   int intArray[5] = { 1, 2, 3, 4, 5 };

The above code declares the array and initializes the first 5 indices (in this case, every index).  If the array had more than 5 indices, then every successive index is initialized to 0.  You can even use an initializer list to have the compiler determine the length of the array:

   int intArray[] = { 1, 2, 3, 4, 5 };

The above code declares an integer array and initializes its indices.  Notice that the size of the array is not explicitly stated.  The compiler will use the initializer list to fill in the size.  The most common use of this type of array declaration is for a character string:

   char myString[] = "Hello!";

The above code declares a character array (C-style string) and initializes it with the string "Hello!".

OK, that covers the basics of arrays.  On to pointers.

One use of pointers is actually arrays.  The array variable itself (intArray in most of my examples above) is nothing more than a pointer.  Often times you will find yourself dynamically creating arrays rather than statically creating them because you will not have size information at compile time (a requirement for a basic array).  You will dynamically allocate the memory, assign the address to a pointer, and treat it as if it were a regular array:

   int *intArray = new int[sizeVariable];
   intArray[0] = 1;

The above code demonstrates dynamically allocating an integer array (using a variable called "sizeVariable" to specify the size of the array) and assigning a value to the first index in the array.  This is one way to use pointers.  You can also use pointers for other kinds of dynamic memory.

Often times you will create a class that is rather large, and you will want to dynamically allocate the memory for the class rather than using memory on the stack:

   MyClass *classInst = new MyClass;

The above code dynamically allocates an instance of the class called MyClass.  If you are familiar with classes, then you are familiar with the . operator.  The . operator lets you access member functions and variables of the class.  When using a pointer to a class, you instead use the -> operator to access member functions and variables:

   classInst->myFunc();

Another common use for pointers is extra output for functions.  Consider the following:

   bool findSymbolOnGraph(char _symbol, int* _x, int* _y);

The above function prototype is for a function that searches a 2D graph for a symbol (_symbol) and returns true if the symbol exists on the graph.  The function will also return the location of the symbol by using the pointers passed into the function (_x and _y).  It would look something like:

   *_x = symbolX;
   *_y = symbolY;

Notice that when you want to access the value of a pointer you must use the * operator.  This is called dereferencing the pointer.  In fact, the previously mentioned -> operator is a type of dereferencing operator.  You can actually avoid using the -> operator if you want by dereferencing the pointer and then using the standard . operator:

   (*classInst).myFunc();

I suspect you would be hard pressed to find someone who writes code this way, though, so just stick to the -> operator when using pointers to classes or structs.

OK, I think that covers the basics of pointers and arrays.  Just start working with them and see what you can do.  If you have any further questions, please don't hesitate to ask.  I'm here to help. :)

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.