0

I am trying to write a code where it reads a file, save its content in array of structures and then try to give these variables to another application over an interval of some milliseconds. I can read the data from a file and store its values in "Filedata" structure. Now I am trying to copy the content of this structure into another. I thought two strategies for this. 1. Declare another variable in same structure and then copy the one structure into another.

struct Filedata {
char *time;
int t_diff;
int SN;
}; 
struct Filedata Data1[100];
struct Filedata Data2[100];
/* After reading and storing the content of file in Data1 Variable in main function and continuing in main*/
for (k=0; k<100;k++)
{
Data2 = Data1;
printf("\t%d\n", Data2[k].SN);
Sleep(Data2[k+1].t_diff);
}

2. Or make another structure and then copy the content on first into second.

struct Target
{
char *time;
int SN;
int t_diff;
};

struct Target Data3[100];

for (k=0; k<100;k++)
{
    Data3[k].SN = Data1[i].SN;
    Data3[k+1].t_diff = Data1 [i+1].t_diff;
    printf("\t%d\n", Data3[k].SN);
    Sleep(Data3[k+1].t_diff);
}

I checked the code with debugger but it is not copying the content of first into another and just printing 0 for all values.

I read some post here about copying and used the assignment for copying, But somehow it is not working

Thanks much for your help.

learningpal
  • 309
  • 1
  • 8
  • 17

3 Answers3

0

In general, to copy src[i] to dest[i] you do something like this:

// First, initialize src to some values
src[i].t_diff = 22;
src[i].SN = 33;
src[i].time = malloc(SOMELENGTH);
strcpy(src[i].time, "some text");

// Now, copy src to dest
dest[i].t_diff = src[i].t_diff;
dest[i].SN = src[i].SN;
dest[i].time = malloc(strlen(src[i].time)+1);
strcpy(dest[i].time,src[i].time);

Also don't forget free at proper places. For each dest or src object(which you used), do: free(dest[i].time)

In your first snippet the way you were trying to copy structures using assignments (assuming you provide indexes) will do a shallow copy, which might not be what you want.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • @ Giorgi If you plese look in my post, I did the same what you mentioned by making another structure and then copying the content of one structure into other. This is the same strategy what you mentioned but i do not get result from it. – learningpal Jul 02 '15 at 13:32
  • @learningpal: It is not the same - look closely. I will modify my sample to more match your situation; you are even confusing some indexes in your second example e.g., `i` and `k` – Giorgi Moniava Jul 02 '15 at 13:34
  • @ Giorgi: Thanks much. It works.. :) – learningpal Jul 02 '15 at 13:51
  • @learningpal: nice although I have some doubts if you are correctly reading/writing file, see here what I mean, http://stackoverflow.com/questions/2767623/reading-file-and-populating-struct – Giorgi Moniava Jul 02 '15 at 13:53
  • @ Giorgi: I did not get you exactly what you mean. i read this post what you mentioned. I am using array of structures in my file reading and do not understand what doubts you have? – learningpal Jul 02 '15 at 15:03
  • @learningpal: if your struct has pointer and you directly write that struct to file just some address will be written to file (address which pointer stored), not the contents where pointer points. Same about reading. This is what I mean: http://stackoverflow.com/questions/12581165/freading-and-fwriteing-a-structure-that-contains-pointers, if you don't suffer this issue, then my comment doesn't apply to you. – Giorgi Moniava Jul 02 '15 at 17:09
  • Yes I got what you are trying to say. I copied the *time variable as: dest[i].time = src[i].time;. I read the time variable as string and I am getting the right result in dest structure. – learningpal Jul 03 '15 at 08:53
0

Version 1: Since Data1 and Data2 aren't structures (they are arrays of structures), I think that you mean to use Data2[k] = Data1[k] in your loop.

Version 2: This shouldn't even compile (there is no SNR field).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • In Version 1: Actually I do not have much experience with programming, so I thought that if I use these structure variable to copy simply by Data2 = Data1, the it would copy all content of first structure inot another. Version2: SNR was a typo. – learningpal Jul 02 '15 at 13:26
  • Version 2 has no `i` declared either. I assume that you wanted to write `k` here. – NiBZ Jul 02 '15 at 13:34
0

Data2 = Data1 will make a shallow copy to the arrays. it only makes Data2 point to Data1 which is not what you want. You need to do a deep copy, index by index. C doesn't allow copying arrays but it allows it if they're in structs however doing a shallow copy.

Lukas
  • 3,423
  • 2
  • 14
  • 26