I want to change the results of function deal() below which fills out a string array. In the watch window I can change the result of the memory location that the pointer points to, but not the string that begins at that address.

Is it possible to keep the memory location the same but alter the string?

#include <iostream>

using std::cout;
using std::left;
using std::right;

#include <iomanip>

using std::setw;

#include <cstdlib>
#include <ctime>

void shuffle( int[][13]);
void deal(const int [][13], const char *[], const char *[], const char *[][2]);
bool Pair( const char *[][2], int);
bool TwoPair(const char *[][2], int);


int main()
{
	const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};

	const char *face[13] = { "Ace","Deuce","Three","Four",
		                     "Five", "Six", "Seven", "Eight",
							 "Nine", "Ten", "Jack", "Queen", "King"};

	const int Size=5;
	bool pairRes =false;

	int deck[4][13] = {0};
	const char *hand[Size][2];
	


	srand(time(0));

	shuffle( deck );
	deal( deck, face, suit, hand);
	pairRes = Pair(hand, Size);


	return 0;
}

void shuffle(int wDeck[][13])
{
	int row;
	int column;

	for ( int card =1; card <= 52; card++){

		do {
			row = rand() % 4;
			column = rand() % 13;
		} while ( wDeck[row][column] != 0);

		wDeck[row][column] =card;

	}

}

void deal( const int wDeck[][13], const char *wFace[], const char *wSuit[], const char *hand[][2])
{
	for ( int card =1; card <= 5; card++)

		for ( int row = 0; row <=3; row++)

			for ( int column =0; column <=12; column++)
				
				if ( wDeck[row][column] == card) {
					hand[card-1][1] = wSuit[row];
					hand[card-1][0] = wFace[column];
				}
}

bool Pair( const char *hand[][2], int Size)
{
	for ( int i = 0; i < Size; i++)

		for ( int j = i+1; j< Size; j++)

			if ( hand[i][0] == hand[j][0])

				return true;
			
				return false;
}

bool TwoPair( const char *hand[][2], int Size)
{
	int counter =0;
	
	for ( int i = 0; i < Size; i++)

		for ( int j = i+1; j< Size; j++)

			if ( hand[i][0] == hand[j][0])

			{counter++;
			if (counter == 2)
				return true;}
			
				return false;
}

Recommended Answers

All 5 Replies

>>I can change the result of the memory location that the pointer points to, but not the string that begins at that address.
Those strings can not be changed because they string literals which reside in read-only memory.

Thanks I'd totally forgotten I's defined hand as const char*!!

Will try it witout the constant!!

Can I boost your reputation? You certainly deserve it!

doesn't matter -- you still can't change them because they are string literals.

Explains why I still coudn't get it to work!

Is there anything else I can do?

put the strings in read-write memory like this: char suit[4][15] = {"Hearts", "Diamonds", "Clubs", "Spades"};

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.