Pointer confusion

This is a discussion on Pointer confusion within the C++ Programming forums, part of the General Programming Boards category; Say I have a class Pawn with a constructor Pawn(enum colors {white,black} Color). Now, somewhere else in my program, I ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    Pointer confusion

    Say I have a class Pawn with a constructor Pawn(enum colors {white,black} Color).

    Now, somewhere else in my program, I have to make a 2D array of pointers pointing to pawns.

    First of all, how can I create a normal 1D array with 5 elements? Pawn myPawn(black) works, but how does Pawn myPawnArray[5] work? Is there a way around f.e. Pawn array[2] = {Pawn(Pawn::white), Pawn(Pawn::black)}; (I've thought about pointers but how is this done?)

    Second question, how can you again make an array of pointer variables? In this example, an array with pointers to pawns? I need to refresh this again because I haven't worked with it for about a year... I'm pretty sure it's done by **array or *array[], but I fail to see why *array[] works again.

    After that I should manage to figure it for *array[][] which is a 2D array of pointers pointing to pawns...

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Post some code. Consider using a vector of pointers, that is vector<Pawn*>.

    An array of pointers:
    Pawn* arr[5];

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,662
    Consider avoiding pointers. A simple 2D array can be made with vectors:
    std::vector<std::vector<Pawn>> PawnArray;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer + struct w/arrays confusion
    By Viper187 in forum C Programming
    Replies: 1
    Last Post: 07-23-2009, 09:37 AM
  2. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21