Interview Questions

I heard that structures could be assigned to variables

C Interview Questions and Answers


(Continued from previous question...)

I heard that structures could be assigned to variables

Q: I heard that structures could be assigned to variables and passed to and from functions, but K&R1 says not.

A: What K&R1 said (though this was quite some time ago by now) was that the restrictions on structure operations would be lifted in a forthcoming version of the compiler, and in fact the operations of assigning structures, passing structures as function arguments, and returning structures from functions were fully functional in Ritchie's compiler even as K&R1 was being published. A few ancient compilers may have lacked these operations, but all modern compilers support them, and they are part of the ANSI C standard, so there should be no reluctance to use them.

(Note that when a structure is assigned, passed, or returned, the copying is done monolithically. This means that the copies of any pointer fields will point to the same place as the original. In other words, the data pointed to is not copied.)

(Continued on next question...)

Other Interview Questions