7 Replies - 374 Views - Last Post: 29 January 2013 - 05:17 AM Rate Topic: -----

#1 EXCELL3NCE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-January 13

Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 12:18 AM

Can I not use the Condition Operator to assign an array to a pointer...

Now I know there is many ways around my problem, but I'm wandering if its because I'm trying to assign an array to a pointer using the Condition Operator which is giving me problems.



string *ptr = (SIZE == SIZES ? BoardS : (SIZE == SIZEM ? BoardM : BoardL));


//BoardS, BoardM, and BoardL, are all predefined arrays of strings.

The DOS application runs but as soon as I get to what seems to be this branch of the program, the DOS application terminates. Throwing me a message saying that "THIS APPLICATION HAS REQUESTED, THE RUNTIME TO TERMINATE IT IN AN UNUSUAL WAY"

Is This A Good Question/Topic? 0
  • +

Replies To: Can I not use the Condition Operator to assign an array to a pointer?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 12:47 AM

Your gonna need to post more of the code for us to properly suggest an answer. But to answer this question

Quote

Can I not use the Condition Operator to assign an array to a pointer...

Yes you can do this
Was This Post Helpful? 0
  • +
  • -

#3 EXCELL3NCE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-January 13

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 03:06 PM

I separated this code which builds a displayboard for my program. Into a different project so I could test run the code separately before I re-intergrade it into my main project.

Board Class .CPP
#include <iostream>
#include "Board.h"

using namespace std;

const int Board::SIZES = 300; //VariableGraphics.cpp
const int Board::SIZEM = 600;
const int Board::SIZEL = 1000;

const int Board::LENS = 30;
const int Board::LENM = 60;
const int Board::LENL = 100;

int SIZE = 0;
int LEN = 0;

string BoardS[300] = {
    "-","-","-","-","-","-","-","-","-","-", //1
    //***ALL THE WAY TO 300 ELEMENTS***\\

};


string BoardM[600] = {
    "-","-","-","-","-","-","-","-","-","-", //1
    //***ALL THE WAY TO 600 ELEMENTS***\\

};

string BoardL[1200] = {
    "-","-","-","-","-","-","-","-","-","-", //1
   //***ALL THE WAY TO 1200 ELEMENTS**\\
};

Board::Board(){


}

void Board::startup(void){




cout<<"Enter in size (SMALL), (MEDIUM), (LARGE)."<<endl;
cout<<"         ***Or enter (TEST)***"<<endl;
cin>>answer1;

if(answer1 != "TEST"){
if(answer1 == "SMALL")
    SIZE = SIZES;
if(answer1 == "MEDIUM")
    SIZE = SIZEM;
if(answer1 == "LARGE")
    SIZE = SIZEL;
}


}

void Board::SetBoard(void){

string fakeboard[SIZE];

string *ptr;
*ptr = (SIZE == SIZES ? BoardS : (SIZE == SIZEM ? BoardM : BoardL));

for (int x = 0; x < SIZE; x++ )
{
    fakeboard[x] = ptr[x];
}


//------DRAW------

    int z = 0;
    int add = (SIZE == SIZES ? LENS:(SIZE ==  SIZEM ? LENM : LENL));

for( int x = 0; x < SIZE; x++ ){

    if( x == (z * add)){

       cout<<"\n"<<fakeboard[x];

       }

       cout<<fakeboard[x];
}
}

Board::~Board()
{
    //dtor
}


void intf(void){

Board b;


b.startup();
b.SetBoard();


}



I had this working with my main project but I only had 1 board size and that was 250, which it worked fine. But enhancing it to multiple board size options has given me alot of problems thats why I had to move it into a separate project to see whats going on.

Board Class Header

#ifndef BOARD_H
#define BOARD_H


using namespace std;

class Board
{
    public:

        string *displayboard;

        void friend intf(void);

        Board();
        void SetBoard(void);
        void Draw(void);
        void startup(void);
        ~Board();

    protected:

    string BoardS;


    string BoardM;

    string BoardL;

    private:

static const int SIZES; //VariableGraphics.cpp
static const int SIZEM;
static const int SIZEL;

static const int LENS;
static const int LENM;
static const int LENL;

int SIZE;
int LEN;

string answer1;
};

#endif // BOARD_H


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 04:00 PM

What is the purpose of the following?

string BoardS[300] = {
    "-","-","-","-","-","-","-","-","-","-", //1
    //***ALL THE WAY TO 300 ELEMENTS***\\

};


Why are you using an array of string that holds single characters? A vector<char> might be a better choice instead of the array of string.

Next C++ doesn't allow Variable Length Arrays so the following is not allowed since SIZE is not a constant.
void Board::SetBoard(void) {

   string fakeboard[SIZE]; // This is not allowed.


You also need to be careful you have two variables with the name SIZE one in your class and one is a global variable. Which size are you actually trying use in the above snippet?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 07:18 PM

string *ptr;
*ptr = (SIZE == SIZES ? BoardS : (SIZE == SIZEM ? BoardM : BoardL));


There is a difference between a pointer's target and a pointer's target value. To get a pointer's target value you dereference it with the * operator. To assign where a single pointer points, the you omit the deference operator.

I guess what I am trying to say is that you are not assigning where the pointer points to properly, you need to get rid of the dereference operator in the assignment.

string *ptr;
ptr = (SIZE == SIZES ? BoardS : (SIZE == SIZEM ? BoardM : BoardL));


Was This Post Helpful? 0
  • +
  • -

#6 EXCELL3NCE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-January 13

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 08:29 PM

You guys are alot of help, thank you.

The //*** ***\\ is not my actual code, I only used that to summarize the array cause it was so long, for reading purposes.

Also, I didn't know that C++ doesn't allow non-constant variable array length declarations. It does make sence though.

jjl, can you explain the whole pointer to me a little bit better. Because I want to learn and understand.

I'm thinking...

string *ptr;  

2 *ptr = (SIZE == SIZES ? BoardS : (SIZE == SIZEM ? BoardM : BoardL)); 



that (*)ptr grabbing the address location of which ever array structure the condition operator ?: assigns to it, by determining the size of the board desired by the user.

And thier for I can than manipulate the array (board) using the ptr.

(This is my train of thought)

You guys are alot of help, thank you.

The //*** ***\\ is not my actual code, I only used that to summarize the array cause it was so long, for reading purposes.

Also, I didn't know that C++ doesn't allow non-constant variable array length declarations. It does make sence though.

jjl, can you explain the whole pointer to me a little bit better. Because I want to learn and understand.

I'm thinking...

string *ptr;  

2 *ptr = (SIZE == SIZES ? BoardS : (SIZE == SIZEM ? BoardM : BoardL)); 



that (*)ptr grabbing the address location of which ever array structure the condition operator ?: assigns to it, by determining the size of the board desired by the user.

And thier for I can than manipulate the array (board) using the ptr.

(This is my train of thought)
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 28 January 2013 - 08:38 PM

Quote

(*)ptr grabbing the address location

It's actually the opposite. When you dereference a pointer using the * operator, you essentially follow the pointer's arrow to it's corresponding target. The confusing part is when you declare a pointer, it looks like you are dereferencing in it's declaration, but that is not true.

i.e.
//Integer variable, has a memory location and a value
int x = 5;

//Integer pointer, point's to garbage 
int *y;

//Set's the pointer to point to the memory location of x
y = &x;

//Defeferences the pointer, "follows the arrow to the memory" and
// changes it's value to 10. the variable x now contains 10 instead 
// of 5
*y = 10;


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: Can I not use the Condition Operator to assign an array to a pointer?

Posted 29 January 2013 - 05:17 AM

Quote

The //*** ***\\ is not my actual code, I only used that to summarize the array cause it was so long, for reading purposes.

I still recommend using a std::vector instead of the arrays. It will simplify your program and possibly remove the need for your pointer as you can copy vectors with the assignment operator.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1