I have a sample piece of code and I had a question about it. Here is the code. My questions are comments near the bottom of the code and I was wondering if anyone could help out. Getting mixed up here.
#include <stdio.h> #include <string.h> struct car { char make[30]; char model[30]; int year; }; struct car f(struct car c); int main(void) { struct car c1 = {"Ford", "Mustang", 2014}, c2; c2 = c1; printf("%s, %s, %d\n", c1.make, c1.model, c1.year); c2 = f(c1); printf("%s, %s, %d\n", c2.make, c2.model, c2.year); } struct car f(struct car c) /* Is car here referring to the structure at the very top of the program, so it knows what structure to put it? */ { struct car temp = c; /* What exactly is c in this function and why copy it? */ strcpy(temp.model, "Explorer"); temp.year += 5; return temp; } The code prints:
Ford, Mustang, 2014 Ford, Explorer, 2019
struct car cinf()is a copy ofc1frommain().c1is not altered byf(c1).cis a copy of the structure.cis a structure, not a string.struct car temp = c;is pointless, you could just work oncin the function and returnc