I'm having some issues using itoa I can't seem to figure out. The principle is simple, I am using a 2dimensional char array. For dealing with quantity I use atoi to make it all happy and then I want to use itoa to convert it back into a string because the inventory array is of type char.

It all works until I hit the itoa line. Then it gives me a access violation error. More accurately, this one

Unhandled exception at 0x63ed144c (msvcr90d.dll) in InventSystem.exe: 0xC0000005: Access violation writing location 0x00067768.

I know this means that there's something i'm doing wrong regarding the actual implementation. I found many examples using itoa in single arrays (as strings are all 1d char arrays at heart), perhaps there is something about 2d arrays i'm missing?

I've isolated the snippet of code I think is necessary to run. Any and all help greatly appreciated.

#define SIZE 12

// The 2 dimensional arrays, Inventory is the items you hold, wildItems are the items you find in different zones. The first field is the name, the second field quantity, and third is zone.
	char *Inventory[SIZE][2]= {{"HP Potions", "10"}, {"Mana Potions", "10"}, {"Map","1"},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""}};	
	char *wildItems[][3]={{"HP Potion", "1", "1"}, {"Apple", "2", "1"}, {"Apple", "2", "2"}, {"Rusty Spoon", "1", "3"},{"Rusty Spoon", "1", "1"}};

int arrayIter;
int x;

for (arrayIter=0;arrayIter<SIZE;arrayIter++){
		if (Inventory[arrayIter][0] == wildItems[storeSpots[itemSelect-1]][0]){
				x =atoi(Inventory[arrayIter][1]) + atoi(wildItems[storeSpots[itemSelect-1]][1]);
				itoa(x,Inventory[arrayIter][1],10);
		}
		else if (Inventory[arrayIter][0]==""){
			Inventory[arrayIter][0] = atoi(wildItems[storeSpots[itemSelect-1]][0]);
			x =atoi(Inventory[arrayIter][1]) + atoi(wildItems[storeSpots[itemSelect-1]][1]);
			itoa(x,Inventory[arrayIter][1],10);
			break;		
		}
	}

Recommended Answers

All 8 Replies

Read-only memory is not writable.

i'm sorry I don't quite understand. I don't see how i'm trying to write to read only memory...

String literals.

char *Inventory[SIZE][2]= {{"HP Potions", "10"}, {"Mana Potions", "10"}, {"Map","1"},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""}};

You don't have writable strings.

String literals.

char *Inventory[SIZE][2]= {{"HP Potions", "10"}, {"Mana Potions", "10"}, {"Map","1"},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""},{"", ""}};

You don't have writable strings.

but doesn't "" simply mean blank? Should I put a space in there to make it writable then? Perhaps a return string?

How interesting, I had no idea that it was the array from the start! It was all happy and fine until then.

First, do you understand this difference?

char *a = "non-writeable";
   char b[] = "writeable";

First, do you understand this difference?

char *a = "non-writeable";
   char b[] = "writeable";

if i'm not mistaken

char *a is a pointer which allocates as much memory as it needs to store the string passed onto it.

char b[] is a char array which holds a buffer length of whatever you give it.

Since char *a is a pointer and the name of an array is an address is memory is it possible to attach a pointer to the beginning of an array and iterate through pointer arithmetic.

*p = b (we don't need to say b[0] if we are sure we want to start from the beginning)

that said, if char *a allocates memory as it needs upon start up, then it must not have data stored in sequential manner like b[] and therefore C makes it so you can't mess with it?

Sorry for the rant I just wanted to make sure i'm thinking about these correctly.

I'd say your understanding of char *a needs a little work.

char *a is a pointer which allocates as much memory as it needs to store the string passed onto it.

I would say that a is a pointer that points to an unnamed array of constant characters.

Maybe these will help:
http://c-faq.com/aryptr/aryptr2.html
http://c-faq.com/decl/strlitinit.html
http://c-faq.com/malloc/malloc2.html

Since char *a is a pointer and the name of an array is an address is memory is it possible to attach a pointer to the beginning of an array and iterate through pointer arithmetic.

This sounds too close the "an array is a pointer" for my general comfort. But maybe I'm just jumpy.

And this for your comparison in the original code:
http://c-faq.com/charstring/stringeq.html

commented: thanks a lot for your guidance, this will definitely help iron out those nasty wrinkles. +2

ah, so then "a" points to an unnamed array of constant characters. That's quite interesting and all things considered makes perfect sense. I think with those links and this new knowledge I will be able to solve the problem.

A sincere thanks Dave, you're a life saver.

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.