#import<stdio.h>
#import<stdlib.h>
typedef struct Video {
char *name;
int unique_views;
} Video;
typedef struct Viewer {
char *username;
Video *watched_videos;
int watched_videos_size;
} Viewer;
int count_views(Viewer **viewers, int viewers_size, char *video_name)
{
Viewer *ptr;
Video *vid;
int count=0;
ptr = *viewers;
for (int i=0;i< viewers_size;i++){
printf("%s ",ptr->username);
vid=ptr->watched_videos;
for (int j=0;j < ptr->watched_videos_size;j++){
printf("%d %d",j,ptr->watched_videos_size);
printf("%s \n",vid->name);
if (vid->name == video_name){
count++;
}
vid++;
}
ptr++;
}
return count;
}
int main()
{
Video videos[] = { {.name = "Soccer", .unique_views = 500},
{.name = "Basketball", .unique_views = 1000} };
Video videos2[] = { {.name = "Soccer", .unique_views = 500} };
Viewer viewer = {.username = "Dave", .watched_videos = videos,
.watched_videos_size = 2} ;
Viewer view = {.username = "Bob", .watched_videos = videos2,
.watched_videos_size = 1};
Viewer *viewers[] = { &viewer, &view };
printf("%d", count_views(viewers, 2, "Soccer")); /* should print 1 */
}
wanted to iterate through multiple viewers to count the number of views but the first iteration is going correctly but on the second iteration ptr is pointing to null is ptr++ not approporiate for this?
how else should i iterate through that array