Copying specific struct members to another structure <p>so I have a file from which I have to load a number of tracks, find tracks longer than 3:30 and find the average bpm of those tracks. </p> <p>Here's the code I have.</p> <p>Main file:</p> <pre><code>#include &lt;stdio.h&gt; #include "functions.h" int main(void) { Playlist *all, *long_tracks; float avg_bpm; all = load_tracks("tracks.txt"); long_tracks = get_tracks_longer_than(3,30,all); print_playlist(long_tracks); avg_bpm = get_avg_bpm(long_tracks); printf("Average BPM of the playlist is: %.2f\n", avg_bpm); return 0; } </code></pre> <p>Header: </p> <pre><code>#ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct track { char track[250]; float bpm; int mm, ss; } Track; typedef struct playlist { int count; Track **tracks; } Playlist; void print_playlist(Playlist *list); Playlist* load_tracks(char *filename); Playlist* get_tracks_longer_than(int mm, int ss, Playlist* all); float get_avg_bpm(Playlist *list); #endif </code></pre> <p>Functions:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include "functions.h" void print_playlist(Playlist *list) { int i; for (i = 0; i &lt; list-&gt;count; i++) { printf("%s (%d:%d) [%f]\n", list-&gt;tracks[i]-&gt;track, list-&gt;tracks[i]-&gt;mm, list-&gt;tracks[i]-&gt;ss, list-&gt;tracks[i]-&gt;bpm); } } Playlist* load_tracks(char *filename) { int n,i; Playlist *list = (Playlist*) malloc(sizeof(Playlist)); Track *tracks; FILE *f = fopen(filename, "r"); fscanf(f, "%d", &amp;n); printf("Tracks: %d\n", n); tracks = (Track*) malloc(n*sizeof(Track)); list-&gt;tracks = (Track**) malloc(n*sizeof(Track*)); for (i = 0; i &lt; n; i++) { fscanf(f, "%d:%d", &amp;tracks[i].mm, &amp;tracks[i].ss); fscanf(f, "%f", &amp;tracks[i].bpm); fgetc(f); fgets(tracks[i].track, 250, f); tracks[i].track[strlen(tracks[i].track)-1] = '\0'; list-&gt;tracks[i] = &amp;tracks[i]; } list-&gt;count = n; return list; } Playlist* get_tracks_longer_than(int mm, int ss, Playlist* all) { Playlist *longSongs = (Playlist*) malloc(sizeof(Playlist)); Track *tracks; tracks = (Track*) malloc(all-&gt;count*sizeof(Track)); longSongs-&gt;tracks = (Track**) malloc(all-&gt;count*sizeof(Track*)); int i, n = 0; for(i=0;i&lt;all-&gt;count;i++) { if(all-&gt;tracks[i]-&gt;mm&gt;mm) { n++; memcpy ( &amp;longSongs, &amp;all, sizeof(all) ); //I also tried longSongs[i]=all[i]; } else if(all-&gt;tracks[i]-&gt;mm==mm) { if(all-&gt;tracks[i]-&gt;ss&gt;ss) { n++; memcpy ( &amp;longSongs, &amp;all, sizeof(all) ); } } } longSongs-&gt;count=n; return longSongs; } float get_avg_bpm(Playlist *list) { float avg_bpm=0; int i; for(i=0;i&lt;list-&gt;count;i++) { avg_bpm+=list-&gt;tracks[i]-&gt;bpm; } return avg_bpm/=list-&gt;count; } </code></pre> <p>Problem is in my get_tracks_longer_than function, in which I don't really know how to copy the struct member I want from source structure to the destination structure, I only managed to copy all members from one structure to the other. My n counter counts number of tracks correctly, and my function which calculates bpm seems to work, but I don't know how to get the exact data I want. I tried using memcpy function, tried assigning it like this: <code>longSongs[i]=all[i]</code>, and I tried manually assigning values, e.g:</p> <pre><code>longSongs-&gt;tracks[i]-&gt;mm = all-&gt;tracks[i]-&gt;mm; longSongs-&gt;tracks[i]-&gt;ss = all-&gt;tracks[i]-&gt;ss; longSongs-&gt;tracks[i]-&gt;bpm = all-&gt;tracks[i]-&gt;bpm; </code></pre> <p>but nothing works, it always copies all members of the source structure, ignoring my if statements.</p>