0

I have a problem in function let_comment. Function should add comments to movies defined in structures movie and comment.

Compiler show error in line f->comments.text=text; error: (COMMENT*)&f->comments' is a pointer;did you mean to use '->'

  #include <stdio.h>
  #include <stdlib.h>


 typedef struct comment {char text[256], name[64];} COMMENT;
 typedef struct movie {char name[64]; double mark;COMMENT comments[50];} MOVIE;

int let_comment(MOVIE *, char *, char *);

int main()
{
MOVIE *p_movie;
//int koment_true;
char text1[256],name1[64];
char *p_text=text1,*p_name=name1;

p_movie=(MOVIE*)malloc(5*sizeof(MOVIE));

printf ("Inout movie data:\n");

for(int i=0;i<5;i++)
{
printf ("Name: "); scanf("%s",(p_movie+i)->name);
}

for(int i=0;i<5;i++) {let_comment((p_movie+i),p_text,p_name);}

}


int let_comment(MOVIE *f, char *text, char *name)
{
printf("Input comment: ");scanf("%s",text);
printf("Comment editor: ");scanf("%s",name);

f->comments.text=text;
f->comments.name=name; 
}   

  `

I were expecting that line f->comments.text=text; to assign comment to movie.comments.text field in structures.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • 1
    The error is exactly what it says it is: Comments is an array of 50 comments. `f->comments` is a pointer to the array, `f->comments[0]` is the first of those comments, `f->comments[1]` is the second one, etc. Your program also has a different problem: you try to assign strings as if they're values, but they're in fact not. You need to `strcopy` into the text and name fields. Lastly, if you `malloc` memory for the movies (do you need to? Why? What are the alternatives?), you should free that memory at the end of your program. – divinas Feb 27 '23 at 10:54

1 Answers1

0

You should use functions of the strcpy family because comment.text is an array. The assignment operator (=) cannot be used on arrays. Have a look at this question

Luc Techer
  • 21
  • 3