Troubles with Multidimensional char* arrays... help needed.


for a small project i have a char* array like that:

define MAX_Clients 12

char* Clients[MAX_Clients][3]={ {"","Device 1",""}, {"","Device 2",""}, {"","Device 3",""}, {"","Device 4",""}, {"","Device 5",""}, {"","",""}, {"","",""}, {"","",""}, {"","",""}, {"","Device 6",""}, {"","Device 7",""}, {"","Device 8",""} };

its a multidimensional char array for strings.

the entries with the "" are placeholders and resolved at runtime. but when i do something like

EntryNr = 9; //to set the IP of the first dynamic entry sprintf(Clients[EntryNr][0], "%d.%d.%d.%d\0", ResolvedIP[0], ResolvedIP[1], ResolvedIP[2], ResolvedIP[3]);

then not only the Entry for "Device 6" changed, but also the entries for the Device 7 & 8 too. i know, that this has something to do how the compiler handles the strings. because they are the same, they get the same memory. because when i use different placeholders, it works normal.

but, can anyone tell me how to handle such string arrays better?


with best regards, martin

I think I would make a struct to hold the three bits of information and then have a single dimension left for the array.

Something like:

struct clientStruct {

     char ipAddress[16];
     char deviceName[MAX_DEVICE_NAME_SIZE];
     char whateverThatOtherFieldIs[ITS_MAX_SIZE];

clientStruct Clients[MAX_Clients];
char*  Clients[MAX_Clients][3]={
                       {"","Device 1",""},               
                       {"","Device 2",""},
                       {"","Device 3",""},
                       {"","Device 4",""},
                       {"","Device 5",""},

So, Clients[n][m] is a pointer. What is Clients[6][0] pointing to?

sprintf(Clients[EntryNr][0],  "%d.%d.%d.%d\0", ResolvedIP[0], ResolvedIP[1], ResolvedIP[2], ResolvedIP[3]);

Where, EXACTLY, are you storing the data?