c - How to add array in multidimensional array? -


my question simple, still can't manage things right because not used c language.

i have array looks this:

char *itemarr[] = {     "gpgga",     "193914.00",     "5312.983745",     "n",     "00206.32143",     "e",     "4,17",     "0.6",     "43.48",     "m",     "47.46",     "m",     "1.0",     "0000",     "gpgll,4916.45,n,12311.12,w,225444,a,m" }; 

and add itemarr new multidimensional array, when try copy content of itemarr protocols array this:

int index = 0; char *protocols[100][total_items]; memcpy(&protocols[index++], &itemarr, sizeof(itemarr)); 

it copy first item of itemarr , not rest. can see first item following code:

printf("itemarr copy in protocols - index 0: %s\n", protocols[0][0]); 

for example, not work:

printf("itemarr copy in protocols - index 1: %s\n", protocols[0][1]); 

please me better example of how should use memcpy, copy array multidimensional array. thanks!

edit:

i tried best explain code simple possible, guess helps when pasting real 1 below:

void getinputprotocolfromuser(protocol) {  char input[message_size];  {     printf("voer hier een nmea protocol in:");     gets(input, message_size); } while (input[0] == '\*' || input[0] == ' ');  strncpy_s(protocol, message_size, input, message_size); }  void splittoarray(char* protocol, char *newarray[]) {  // strdup copies message string pointer in order create array of items inside message char *string = _strdup(protocol),     *token;  int c = 0;  /* first token */ token = strtok(string, ",");  /* walk through other tokens */ while (token != null) {      char *copy = malloc(sizeof(char) * strlen(token));     strcpy(copy, token);      newarray[c++] = copy;     token = strtok(null, ","); } }  void compareprotocols(char *input, char *protocols[]) {  int index = 0; char *inputarray[total_items+1]; splittoarray(input, inputarray); inputarray[total_items] = input; memcpy(&protocols[index++], &inputarray, 15);  int i; (i = 0; < sizeof(messages) / sizeof(messages[0]); i++) {     char *messagearray[total_items];     splittoarray(messages[i], messagearray);     memcpy(&protocols[index++], &messagearray, 15);      //processprotocol(messagearray, inputarray); } }  int main(void) {  char *protocols[100][total_items]; char *inputprotocol = (char *)malloc(message_size);  getinputprotocolfromuser(inputprotocol); compareprotocols(inputprotocol, protocols);  printf("input index 0: %s\n", protocols[0][1]); free(inputprotocol);  return 0; } 

the original version of post declared itemarr as

char *itemarr = {   "gpgga",   "193914.00",   ... 

i.e. without [] in declarator. crystal ball tells me that's have in actual code.

this not valid declaration in c language, gcc (if use gcc) accepts such declarations reason that's beyond understanding. (the compiler issues warning excessive number of initializers.) declaration translated

char *itemarr = "gpgga"; 

with rest of initializers discarded. behavior of code follows that.

if 1 adds missing [] declaration of itemarr, code begins work intended. gets copied target array.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -