lleto
lleto

Reputation: 684

Fill an array in C

I have a question on filling an array with values in C. I have a string that I want to split into an array of new strings each 14 'sections' long.

int main(int argc , char *argv[])
{
char string[]="50 09 00 00 98 30 e0 b1 0d 01 00 00 00 00 4f 09 00 00 98 30 c6 b1 0d 01 01 01 01 50 09 00 00 98 30 e0 b1 0d 01 00 00 00 00 4f 09 00 00 98 30 c6 b1 0d 01 01 01 01";

char delim[] = " ";
char *result = NULL;
char *strArray[1440] = {0};
int i = 0;
result = strtok(string, " ");

while (result) 
{
 strArray[i] = result;
 result = strtok( NULL, delim );
 i++;
}

// Now I have each 'section' of the original string in strArray[xx]

int z = 1022;
int c;
char arr[5000];
char *finalarr[100] = {0};
char buff[100];
int l = 0;

for(c=0;c<z;++c) 
{
  strcat(arr,strArray[c]);
  if (c % 14 == 13)
  {
     // print the value so far for check, this gives an output of 28 chars 
     puts(arr);
     // copy value of arr to buff
     ret = sprintf(buff,"%s", arr);
     // copy value of buff to instance of finalarr
     finalarr[l] = buff;
     // empty arr
     strcpy(arr," ");
    l++;
  }
}

// both have the same value (last value of arr)
printf("finalarr1 = %s\n",finalarr[1]);
printf("finalarr20 = %s\n",finalarr[20]);
}

Perhaps I'm trying to solve it in a too complex way (I hope). Anyway some directions to help would be highly appreciated.

Upvotes: 1

Views: 15500

Answers (2)

askovpen
askovpen

Reputation: 2494

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

int main(int argc , char *argv[])
{
    int i;
    int fcount=0;
    int acount=0;
    char string[]="50 09 00 00 98 30 e0 b1 0d 01 00 00 00 00 4f 09 00 00 98 30 c6 b1 0d 01 01 01 01 50 09 00 00 98 30 e0 b1 0d 01 00 00 00 00 4f 09 00 00 98 30 c6 b1 0d 01 01 01 01";
    char *finalarr[100];
    char arr[29]="\0";
    for (i=0;i<(strlen(string));i++)
    {
        if ((i+1)%3!=0)
        {
            strncpy(&arr[acount],&string[i],1);
            acount++;
            if (acount==28)
            {
                acount=0;
                arr[29]="\0";
                finalarr[fcount]=strdup(arr);
                fcount++;
            }

        }
    }
    printf("finalarr1 = %s\n",finalarr[0]);
    printf("finalarr1 = %s\n",finalarr[1]);
    printf("finalarr1 = %s\n",finalarr[2]);
    return 0;
}

result:

finalarr1 = 500900009830e0b10d0100000000
finalarr1 = 4f0900009830c6b10d0101010150
finalarr1 = 0900009830e0b10d01000000004f

Upvotes: 1

Nicol&#225;s Ozimica
Nicol&#225;s Ozimica

Reputation: 9748

You should replace the lines:

 // copy value of arr to buff
 ret = sprintf(buff,"%s", arr);
 // copy value of buff to instance of finalarr
 finalarr[l] = buff;

with:

 finalarr[l] = strdup(arr);

because you only need to duplicate the string stored in arr. The variable buff should not be needed.

And the line:

 // empty arr
 strcpy(arr," ");

with:

 *arr = '\0';

Because you only need to "reset" the string arr. With your original line, you only "replaced " former text with a 'lonely' space.

Upvotes: 1

Related Questions