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.

0

3 Answers 3

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.

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

6 Comments

@ 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: 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
@learningpal: nice although I have some doubts if you are correctly reading/writing file, see here what I mean, stackoverflow.com/questions/2767623/…
@ 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: 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: stackoverflow.com/questions/12581165/…, if you don't suffer this issue, then my comment doesn't apply to you.
|
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).

2 Comments

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.
Version 2 has no i declared either. I assume that you wanted to write k here.
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.

1 Comment

Ya thanks much. I got your point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.