**Pointer to a Dynamic Array of Pointers

This is a discussion on **Pointer to a Dynamic Array of Pointers within the C++ Programming forums, part of the General Programming Boards category; Been playing around with this for hours. Code is based on this google search. Basically, all I want to do ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    **Pointer to a Dynamic Array of Pointers

    Been playing around with this for hours. Code is based on this google search. Basically, all I want to do is declare a pointer to an array of CARDSTRUCT pointers.

    Here is the complete code.

    Here is the offending code:
    Code:
    void CardClass::shuffle_deck()
    {
    	int randnum;
    	int randoms[52]; 
    	CARDSTRUCT **nodes; 
    	CARDSTRUCT *temp;
    
    	nodes = new (CARDSTRUCT*)[52];
    
    	for(int c=0;c<52;c++)
    
    		nodes[c] = new (CARDSTRUCT*);

    Here are the specific compiler errors:


    --------------------Configuration: texcalc - Win32 Debug--------------------
    Compiling...
    texcalc.cpp
    F:\c++ source\texcalc.cpp(151) : error C2143: syntax error : missing ';' before '['
    F:\c++ source\texcalc.cpp(155) : error C2440: '=' : cannot convert from 'struct CARDSTRUCT ** ' to 'struct CARDSTRUCT *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.

    texcalc.exe - 2 error(s), 0 warning(s)



    Please help a fellow programmer from being driven to the point of insanity.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >all I want to do is declare a pointer to an array of CARDSTRUCT pointers
    Be careful what you ask for.
    Code:
    CARDSTRUCT *(*p)[52];
    
    for ( int i = 0; i < 52; i++ )
      (*p)[i] = new CARDSTRUCT;
    
    for ( int i = 0; i < 52; i++ )
      std::cout << *(*p)[i] << '\n';
    >nodes = new (CARDSTRUCT*)[52];
    Whomever told you that you could put parens around anything and it would still work, lied to you. Try this:
    Code:
    CARDSTRUCT **p;
    
    p = new CARDSTRUCT*[52];
    
    for ( int i = 0; i < 52; i++ )
      p[i] = new CARDSTRUCT;
    Last edited by Prelude; 06-03-2005 at 08:00 PM.
    My best code is written with the delete key.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up

    Code:
    void CardClass::shuffle_deck()
    {
    	int randnum;
    	int randoms[52]; 
    	CARDSTRUCT **nodes; 
    	CARDSTRUCT *temp = head_ptr;
    
    	nodes = new CARDSTRUCT*[52];
    
    	for(int c=0;c<52;c++)
    
    		nodes[c] = new CARDSTRUCT;

    --------------------Configuration: texcalc - Win32 Debug--------------------
    Compiling...
    texcalc.cpp
    Linking...

    texcalc.exe - 0 error(s), 0 warning(s)



    *thank you very much ms. prelude
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 12:45 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 09:21 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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