[A89] Weird problem with some character arrays...


[Prev][Next][Index][Thread]

[A89] Weird problem with some character arrays...




I'm having a very *weird* problem with manipulation of character
arrays.

When I change one array, it seesm to alter the other array, though I
can find no code that seems to be doing this.

void decodeGenesis(const char *bitstr, char *addressbits, char
*valuebits) {
	char copy[40];
	
	// make a copy of the bit string
	copy[0] = 0;
	strcat(copy,bitstr);
	
	copy[24] = 0;
	strcat(addressbits,copy+16);
	copy[24] = bitstr[24];
	
	copy[16] = 0;
	strcat(addressbits,copy+8);
	copy[16] = bitstr[16];
	
	copy[40] = 0;
	strcat(addressbits,copy+32);
	
	copy[32] = 0;
	strcat(valuebits,copy+29);
	copy[32] = bitstr[32];
	
	copy[29] = 0;
	strcat(valuebits,copy+24);
	copy[29] = bitstr[29];
	
	copy[9] = 0;
	strcat(valuebits,copy);
}

If you print the values of the valuebits and the addressbits,
valuebits is fine. It works just as it is supposed to. But addressbits
ends up being NULL, or 1.

Both values are declared in another function as follows:

type somefunction(blah) {
  char valuebits[16], addressbits[24];

  valuebits[0] = 0;
  addressbits[0] = 0;
  ... code ...

  decodeGenesis(otherstring,addressbits,valuebits);

  ... code ...
}

The curiosity arises here. Comment out the last strcat, and
addressbits miraculously stays unaffected. Have I altered addressbits
somehow that I'm not seeing? Is there some quirk of character arrays
I'm missing? Maybe a problem with strcat? I tried to change it to
sprintf(valuebits,"%s%s",valuebits,copy), but the result to
addressbits is the same.

This is why I think I'm doing something else without realizing it,
though I cannot see what.

To broaden the scope, here is the entire file:

#include <string.h>
#include <dialogs.h>
#include <stdio.h>
#include <alloc.h>

#include <graph.h>
#include <kbd.h>

#include "ggdecode.h"

short int decodeGenesisToHex(const char digit) {
	short int value;
	
	switch (digit) {
		case 'A': value = 0; break;
		case 'B': value = 1; break;
		case 'C': value = 2; break;
		case 'D': value = 3; break;
		case 'E': value = 4; break;
		case 'F': value = 5; break;
		case 'G': value = 6; break;
		case 'H': value = 7; break;
		case 'J': value = 8; break;
		case 'K': value = 9; break;
		case 'L': value = 10; break;
		case 'M': value = 11; break;
		case 'N': value = 12; break;
		case 'P': value = 13; break;
		case 'R': value = 14; break;
		case 'S': value = 15; break;
		case 'T': value = 16; break;
		case 'V': value = 17; break;
		case 'W': value = 18; break;
		case 'X': value = 19; break;
		case 'Y': value = 20; break;
		case 'Z': value = 21; break;
		case '0': value = 22; break;
		case '1': value = 23; break;
		case '2': value = 24; break;
		case '3': value = 25; break;
		case '4': value = 26; break;
		case '5': value = 27; break;
		case '6': value = 28; break;
		case '7': value = 29; break;
		case '8': value = 30; break;
		case '9': value = 31; break;
		default: value = -1; break;
	}
	
	return value;
}

short int isValidGenesis(unsigned char *ggcode, short int *length,
char *bitstr) {
	short int loop, digit;
	const char *hexbits[] = {
		"00000","00001","00010","00011","00100","00101","00110","00111",
		"01000","01001","01010","01011","01100","01101","01110","01111",
		"10000","10001","10010","10011","10100","10101","10110","10111",
		"11000","11001","11010","11011","11100","11101","11110","11111"
	};
	
	if (*length == 9) {
		if (ggcode[4] != '-' && ggcode[4] != 173) {
			return -1;
		}
		
		for (loop = 4; loop < *length; loop++) {
			ggcode[loop] = ggcode[loop + 1];
		}
		
		ggcode[--(*length)] = 0;		
	} else if (*length != 8) {
		return -1;
	}
	
	for (loop = 0; loop < *length; loop++) {
		if ((digit = decodeGenesisToHex(ggcode[loop])) != -1) {
			strcat(bitstr,hexbits[digit]);
		} else {
			return -2;
		}
	}
	
	return 1;
}

void decodeGenesis(const char *bitstr, char *addressbits, char
*valuebits) {
	char copy[40];
	
	// make a copy of the bit string
	copy[0] = 0;
	strcat(copy,bitstr);
	
	copy[24] = 0;
	strcat(addressbits,copy+16);
	copy[24] = bitstr[24];
	
	copy[16] = 0;
	strcat(addressbits,copy+8);
	copy[16] = bitstr[16];
	
	copy[40] = 0;
	strcat(addressbits,copy+32);
	
	copy[32] = 0;
	strcat(valuebits,copy+29);
	copy[32] = bitstr[32];
	
	copy[29] = 0;
	strcat(valuebits,copy+24);
	copy[29] = bitstr[29];
	
	copy[9] = 0;
	strcat(valuebits,copy);
}

void convertGenesis(char *ggcode) {
	short int length = strlen(ggcode), result;
	char bitstr[40], addressbits[24],valuebits[16],str[30],saveCode[10];
	unsigned long int address;
	unsigned short int value;
	HANDLE dlg;
	
	// preserve the code
	saveCode[0] = 0;
	strcat(saveCode,ggcode);
	
	// erase bit strings
	bitstr[0] = 0;
	addressbits[0] = 0;
	valuebits[0] = 0;
	
	result = isValidGenesis(ggcode,&length,bitstr);
	
	if (result == -1) {
		DlgMessage((char *)errors[INVALID_LENGTH],"Genesis Codes must be 8
characters!",BT_OK,BT_NONE);
	} else if (result == -2) {
		DlgMessage((char *)errors[INVALID_CODE],"Only
ABCDEFGHJKLMNPRSTVWXYZ0123456789 are valid!",BT_OK,BT_NONE);
	} else {
		if ((dlg = DialogNewSimple(125,50)) != H_NULL) {
			decodeGenesis(bitstr,addressbits,valuebits);
			address = binaryToDecimal(addressbits);
			value = (unsigned short int)binaryToDecimal(valuebits);
			
			DialogAddTitle(dlg,titles[GENESIS],BT_OK,BT_NONE);
			
			sprintf(str,ggstr,saveCode);
			DialogAddText(dlg,5,15,str);
			
			sprintf(str,"Value: %04hXh, Address: %06lXh",value,address);
			DialogAddText(dlg,5,25,str);
			
			DialogDo(dlg,CENTER,CENTER,NULL,NULL);
			
			HeapFree(dlg);
		} else {
			DlgMessage((char *)errors[MEMORY_ERROR],(char
*)errors[NO_FREE_MEMORY],BT_OK,BT_NONE);
		}
	}
}

The convertGenesis() function is called with a 10-max character array.
The program works from there. There is no other interaction with any
other values in this section.

Thanks in advance for any assistance...

John David Ratliff
jdratlif@cs.indiana.edu