dali1985
dali1985

Reputation: 3313

put pointer values in a array

I have the following part of the code:

         i = 0;
         while (ptr != NULL)
         {
          if (i == 0)
             strcat(machine, ptr); 
          if (i == 2)
             strcat(number, ptr);
          if (i == 4)
             strcat(hr, ptr); 
          if (i == 6)
             strcat(dw, ptr); 
          if (i == 8)
             strcat(vcc, ptr);
          i++;
         }
         printf("Final: %s, %s, %s, %s, %s\n", machine, number, hr, dw, vcc);

And i have these results:

Final: 3, 34, 56, 67, 56

How can I save them in a 10 position array in the positions 5-9? To be like that:

0 0 0 0 0 3 34 56 67 56

I wrote the following code but it is uncompleted because I do not know how to pass &machine, &number, &hr, &dw, &vcc in the table

FILE *ft = fopen("Desktop/mytext.txt","a+");
struct tm *tp;
time_t t;
char s[80];

t = time(NULL);
tp = localtime(&t);
strftime(s, 80, "%d/%m/%Y  %H:%M:%S", tp);
char table1[1][10];
for(int i = 0; i<1; i++)
{
    fprintf(ft,"%s ",s);
    for(int j = 0; j<10; j++)
    fprintf(ft,"%d ",table1[i][j]);
}

Upvotes: 0

Views: 163

Answers (3)

tike
tike

Reputation: 2294

given that you are able to manipulate the first piece of code, a possible way would be:

   i = 0;
   int offset = 5;
   char* table[1][10];       

   while (ptr != NULL)
     {
      if (i == 0)
         strcat(machine, ptr);
      if (i == 2)
         strcat(number, ptr);
      if (i == 4)
         strcat(hr, ptr); 
      if (i == 6)
         strcat(dw, ptr); 
      if (i == 8)
         strcat(vcc, ptr);
      table[0][5+(i/2)] = ptr;   
      i++;
     }
  printf("Final: %s, %s, %s, %s, %s\n", machine, number, hr, dw, vcc);

in the second piece of code I would get rid of the outer for loop and just write:

   for(int j = 0; j<10; j++)
      fprintf(ft,"%d ",table1[0][j]); 

given that you do really have only one such array as your declaration suggests.

please note, that the above solution would only work locally within the function, since returning local variables doesn't work. In order to be able to globally use the table structure, you might want to malloc() and strcpy() the values into the array.

Upvotes: 0

Alexis
Alexis

Reputation: 2179

Let's say you have already your values into "machine, number, hr, dw, vcc" (who are char*)

You can't store them into your char table1[1][10] because it's a table of array who can contain only one array of 10 char.

so you neeed a char ** looking like:

char *table1[10] = {0};

table1[5] = machine; 
table1[6] = number;
table1[7] = hr; 
table1[8] = dw; 
table1[9] = vcc;

but to display it you are going to get few problems but you can always do something like:

for (int i = 0; i < 10; i++)
{
 if (table1[i] == NULL)
   printf("0 ");
else
   printf("%s ", table1[i]);
}
printf("\n");

But in your case why you don't simply use a int[10] ?

Upvotes: 2

Dayal rai
Dayal rai

Reputation: 6606

It is not clear what exactly you want but just giving a shot

char table1[1][10]={0};    
    table1[0][5]= machine;
    table1[0][6]=number;
    table1[0][7]=hr;
    table1[0][8]=dw;
    table1[0][9]=vcc;

Upvotes: 0

Related Questions