Making an Array to Hold Arrays of Character Arrays in C -


my c little more rusty @ moment, i'm failing create think should pretty basic.

allow me refer character arrays strings post. make things clearer both me , you.

what have array can hold 1 or more strings. instance {"ab", "cd", "ef"}. want make array store multiple versions of array of strings. {{"ab", "cd", "ef"}, {"gh", "ij"}, {"kl"}}.

what have right is:

char *arrayofstrings[50]; // single array hold multiple strings char **arrayofarraysofstrings[10]; // array hold multiple snapshots of arrayofstrings 

the array of strings changes on time , i'm using array of arrays of strings hold historical snapshots of arrayofstrings. problem comes when need access snapshot array data. code have put stuff snapshot array:

arrayofarraysofstrings[input_index] = arrayofstrings; // can assume input_index updated correctly, i've checked that. 

this appears incorrect since when try access , print out contents of snapshot array, prints out information recent arrayofstrings. idea going store address of arrayofchars pointing in entry of snapshot array can access later.

right now, access entry in snapshot array accomplished this:

arrayofarraysofchars[historicalindex][indexoftargetchar] 

there several questions i'm looking answer:

  1. is method outlined appropriate i'm trying or there flaw in overall logic?
  2. what doing wrong current code , how fix it?
  3. is there better way this, , if so, how initialization of arrays, addition arrays, , reading arrays work?

--edit 4/18-- part of problem i'm setting pointers in arrayofarraysofstrings point same thing arrayofstrings pointing to. that's bad since arrayofstrings gets edited. need way duplicate 2d array... preferably allocating new block of memory arrayofstrings point at.

you have one-too-many pointers in both of arrays.

char arrayofchars[50]; // single array of characters char *arrayofarraysofchars[10]; // array hold multiple single arrays of characters 

since arrayofchars being used buffer (new data goes there first), you'll need save copy of string arrayofarrays. posix function strdup should here.

notice & , * opposites, &* , *& absolutely nothing.

you also, make arrayofarrays literally that.

char arrayofchars[50]; // single array of characters char arrayofarraysofchars[10][50]; // array hold multiple single arrays of characters 

with setup, should use strcpy copy data arrayofarrays.


having read edit, think need start real simple. , fwiw variable names wrong kind of hungarian.

for think you're trying do, i'd start single char array. main buffer, hold strings being input , examined.

enum { bufsz = 50 }; char buf[bufsz + 1]; 

then can use fgets or whatever.

fgets(buf, bufsz, infile); 

to save these in array, i'd use strdup automatic trimming. if strings going 2 characters long, don't want 48 bytes being used each one. so, array of char pointers (strings).

enum { strvsz = 40 }; char *strv[strvsz + 1]; int i; = 0; strv[i] = strdup(buf); strv[i+1] = null; // makes "argv-style" null-terminated array of strings ++i; // index of next element, , count of elements added 

each element of strv char pointer. preserve our sanity, we're trying abstract away of distracting detail moment, , treat strings separate data type.

now create lists of these, same thing again. there's no strdup-type function make duplicate of array of pointers, have separate allocation , copying.

enum { strvvsz = 20 }; char **strvv[strvvsz + 1]; int j; j = 0; strvv[j] = calloc(i+1, sizeof *strvv[j]); // assuming count of elements  memcpy(strvv[j], strv, * sizeof *strvv[j]); ++j; // j index of next string-pointer array in array-of-same,      // , count of elements added. 

now, names silly yours, they're shorter!


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

node.js - Bad Request - node js ajax post -

uitableview - Create and use custom prototype table cells in xamarin ios using storyboard -