hello guys...

my problem is that i cannot seem to save the value of ptr pointer.... every time i reference it the value seems to reset. after the initializatin it should have 2w's and 2b's in it.. but when i try and print it. it comes as empty... the entry is int for rows and cols...ne ideas ??
for white player... it is alphabet and int...

thanks for the help..

tanmay

# include <stdio.h>
# include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>

#define WHITE 'w'
#define BLACK 'b'

static int numRows;
static int numCols;
static int whiteCount=0;
static int blackCount=0;
static int numMoves=0;
static char *ptr, *tmp = NULL;
static char *passer = NULL;
void printMatrix(char * addr,int a, int b);
void initializeMatrix(char * addr, int a, int b);
void putwhite(char * addr, int num);

int main (void)
{
	int r,w,second;
	char first;	

	bool defaultMatrix = false;
	bool incorrectWhiteEntry = false;
	bool incorrectBlackEntry = false;
	bool gameFinish = false;
	bool allOK = false;
	int memCount = 0;
	char str[10];
	int temp;
	printf("Enter the num of rows: ");
	scanf("%d", &numRows);
	printf("Enter the num of cols: ");
	scanf("%d", &numCols);
	
	
	if(defaultMatrix || numRows >19 || numCols >19)
	{
		printf("Matrix Size is: %i x %i \n", numRows, numCols);
		printf("Defaulting the matrix to 10 x 10 \n");
		numRows = 10;
		numCols = 10;
		allOK = true;		
	}	
	else
	{
		//printf("test");		
		printf("Matrix Size is: %i x %i \n", numRows, numCols);
		
		allOK = true;
	}
	
	ptr = malloc(numRows*numCols);
	tmp = memset (ptr, '\0', numRows*numCols);
	//printf("test");
	if(tmp ==  NULL)
	{
		printf("Could not associate Dynamic Memory. Error !! Exiting...\n");
		return 0;		
	}	
	else
	{
		ptr = tmp;
	}
	if(allOK)
	{
		char othelloMatrix[numRows - 1][numCols - 1];
	}
	
	passer = &(*ptr);
	initializeMatrix(passer, numRows,numCols);
	printMatrix(passer, numRows, numCols);
	//printf("test1");
	while(!gameFinish)
	{
		
		
		for(memCount = 0; memCount < numRows*numCols; memCount++)
		{
			if(ptr[memCount] = '\0')
				gameFinish = false;
			else
				gameFinish = true;
		} 
		
		printf("White Player enter your matrix row :");
		scanf("%s", &first);
		printf("White Player enter your matrix col :");
		scanf("%d", &second);
		//printf("%i % i", first ,second);
		if(first - 96 > numRows || second > numCols)
		{
			incorrectWhiteEntry = true;
		}
		else
		{
			incorrectWhiteEntry = false;
		}
		while(incorrectWhiteEntry)
		{
			printf("White Player enter your matrix row :");
			scanf("%s", &first);
			
			printf("White Player enter your matrix col :");
			scanf("%d", &second);
			if(first - 96 > numRows || second > numCols)
			{
				incorrectWhiteEntry = true;
			}
			else
			{
				incorrectWhiteEntry = false;
			}
		}
		//printf("test");
		//printf("White player entered at %cx%i \n",first,(numCols-1)*(first -96 -1) + (second -1)+ *ptr);
		for(temp = 0; temp < numRows*numCols; temp++)
		{
			printf("temp %c", ptr[temp]);
		}
		printf("test %i %i %i",second,  first-96, (numCols -1)*(first -96 ) + (second-1) );
		putwhite(passer, (numCols-1)*(first -96 -1) + (second-1));
		printMatrix(passer, numRows, numCols);
	}	
	int d;
	scanf("%d", &d);
	free(ptr);
	return 1;

}

void putwhite(char *addr, int num)
{
    char * mtrxPtr;
	mtrxPtr = addr;
                   
	mtrxPtr[num] = WHITE;
	whiteCount++;
	numMoves++;
	
}
void initializeMatrix(char *addr, int a, int b)
{
	int firstw,firstb,secondw,secondb = 0;
	char * mtrxPtr;
	mtrxPtr = addr;
    
    //printf("address is:");
	firstw = ((b* (a/2 -1)) + (b/2 -1));
	firstb = ((b* (a/2-1)) + (b/2));
	secondb = ((b* (a/2)) + (b/2-1));
	secondw = ((b* (a/2)) + (b/2));
		
	mtrxPtr[firstw] = WHITE;//white;
    mtrxPtr[firstb] = BLACK;//blacke;
    mtrxPtr[secondw] = WHITE;//white;
    mtrxPtr[secondb] = BLACK;//black;
	//mtrxPtr[test+1] = black;
}
void printMatrix(char *addr, int a, int b)
{
	int i,j = 0;
	char * mtrxPtr;
	mtrxPtr = addr;
	system("clear");
	//printf("rt");
		for(i=0;i<= a; i ++)
		{
			printf(" %i  |",i);
				
		}
		
		printf("\n");
		for(i=0;i<= a; i ++)
		{
			printf("-----");		
		}
		printf("\n");
		i = 1;
		for(j=1; j <= b; j++)
		{
			printf(" %c  |",(j+96));
			for(i = 1; i <=a; i ++)
			{
				if(mtrxPtr[b*(i-1) + (j -1)] == '\0')
					printf("    |");
				else
					printf(" %c  |",mtrxPtr[b*(i -1)+ (j-1)]);
						
			}
			printf("\n");
			for(i=0;i<= a; i ++)
			{
				printf("-----");		
			}
			
			printf("\n");
		}
			
		
	//}	

}
mvmalderen commented: if(post == 1 && code_tags) rep++; :P +9

Several things are wrong with your code starting with this:

ptr = malloc(numRows*numCols);

Errors :
What are you exactly trying to do here. This initialization is wrong not only for memory allocation of a 2dimensional array but also for 1dimensional array.What you have done is nothing but

ptr = malloc (100);

Which has no meaning !!!

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.