Google Interview Question
1 Answer
Google Interview Question
| Flag | PURGE
Generally it is preferred to pass pointer to structure to a function in C, in order to avoid copying during function call. This has an unwanted side effect that the called function can modify the elements of the structure inadvertently. What is a good programming practice to avoid such errors without compromising on the efficiency of the function call ?
- shubhamchawla00 October 17, 2014Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
First of whether a variable could be modified inside a function if its passed by reference like in the following function prototype
or by using an actual pointer
In both cases the function SUM could alter the contents of the variables it uses.
In order to preserve the values of such variables but still pass them as pointers we need to instruct the compiler to disallow modifications to them by using the CONST keyword.
the final function protoype would look like
Please note that strictly speaking, a C compiler does not variables to be taken by reference, therefor you can only use the second protoype in a C only compiler.
- RED October 23, 2014