0

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]);
}
1
  • 1
    Hey your Question is not clear? Commented Jun 20, 2013 at 12:21

3 Answers 3

2

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] ?

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

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.

5 Comments

I am trying to use your solution but (as I forgot to mention) I have declared char* ptr; so I have an error in the 'table' line 'invalid conversion from 'char*' to 'char''
ok I changed the table[i][5+(i/2)]= *ptr but now I have a segmentation fault
Looking at it again, there is an obvious error here: table[i][5+(i/2)] = ptr, should really be table[0][5+(i/2)] = *ptr. My apologies.
No I think the problem for the segmentation fault is the declaration of i. I think it exceeds the array dimensions.
I think it's not the declaration of i, but the fact, that I declared tables as char and not char *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.