c - assigned values to a field for all elements in an array of structs -
i have:
#include <stdio.h> struct dvd { char *movie_title; int minutes; float price; }; void display_struct(struct dvd *ptr); int main() { struct dvd movies[10]; movies[0].movie_title = "i legend"; //don't want } void display_struct(struct dvd *ptr) { printf("%s\n%i\n%f\n", ptr->movie_title, ptr->minutes, ptr->price); } i want assigned 10 movie titles array of structs in single statement. possible? thanks!
in single statement this?
main() { struct dvd movies[10] = { { .movie_title = "i legend", .minutes = 101, .price = 9.99 }, { .movie_title = "hancock", .minutes = 103, .price = 5.99 }, { .movie_title = "mib3", .minutes = 106, .price = 9.49 } }; } if want avoid field names:
main() { struct dvd movies[10] = { { "i legend", 101, 9.99 }, { "hancock", 103, 5.99 }, { "mib3", 106, 9.49 } }; }
Comments
Post a Comment