Comp2 (midterm to week 7) Flashcards
What does computer do differently when it sees #include <filename.h> vs #include "filename.h"</filename.h>
The “” causes the preprocessor to begin searching for the file to be included in the same directory as the file being compiled.
The <> causes a search to be performed in an implementation-dependent manner, normally through predesignated compiler and system directories.
define PI = 3.141;
What is the error? Why?
= not allowed.
Everything to the right replaces the symbolic constant.
Can a symbolic constant be re-defined?
No.
What is a macro?
An operation defined as symbols?
What happens when a macro has arguments?
The arguments are substituted in the replacement text. I.e. the macro is called with certain values.
What happens for multiline macros and symbolic constants?
\ must be placed at the end of the line indicating the replacement text continues on the next line.
What must we do if passing c + 2 to the macro #define CIRCLE_AREA(x)(PI * x * x)
The PI and the two x’s must be enclosed in parenthesis to force the correct order of operation.
Another solution is to enclose macro arguments in parenthesis.
Why are macros obselete?
Macros were created to replace function calls with inline code to eliminate the function-call overhead.
For the macro #define CIRCLE_AREA(x)((PI) * (x) * (x)) By passing r++ explain why this macro is unsafe?
because the ++ operation would be executed twice which is not allowed in C so the second r++ would be undefined.
How do you specify that a function takes a variable number of arguments of any type?
Make … one of the parameters.
What library is used for variable length arguments? Describe the syntax requirements within the variable length argument function average which takes in the number of arguments and any number of numbers.
stdarg.h
double total = 0
va_list ap; // stores information needed by va_start and va_end;
va_start(ap, i); //i is the rightmost argument before the ellipsis
for (int j = 1; j <= i; j++){
total += va_arg(ap, double); //Adds the arguments of type double to total
}
va_end(ap); //clean-ip variable-length argument list
return total / i;
Correct format for a struct. Can a struct contain a struct of the same type? Workaround?
Name for said workaround?
struct name{
data
} typedef Name;
No but can contain a pointer to a struct of the same type. This is called a self-referential structure.
Can u compare structs? Why?
No because struct members are not necessarily stored in consecutive bytes of memory.
Syntax for using structure member operator and structure pointer operator.
struct card aCard;
struct card *cardPtr;
aCard.suit =
cardPtr->suit =
What is typedef Card equivalent to? Stylistic Tradition for typedef?
struct card
Capitilise fist letter of name after typedef
How do you access members in a nested anonymous struct?
Accessed the same way as a normal member, struct is treated as if it didn’t exist.
Define an array of integers of size 5 using malloc.
What function is used to free the memory?
int *arr;
arr = (int *)malloc(5 * sizeof(int));
free(arr);
Define an array of integers of size 5 using calloc.
int *arr;
arr = (int *)calloc(5, sizeof(int));
What is returned by malloc and calloc if no memory is available? If succesful?
A null pointer
If succesful return a pointer to the allocated memory.
What is the difference between malloc and calloc?
The memory assigned to malloc is not initialised but the memory assigned to calloc is, so calloc clears the memory it allocates.
What does realloc do?
-Realloc changes the size of an object allocated by a previous malloc, calloc or realloc.
-Content is not modified if the new amount of memory allocated is larger than the amount allocated previously. Otherwise the contents are unchanged up to the new size.
-Takes in a ptr to the original object and a new size.
-If the ptr is null, works like malloc.
-If ptr is not null and size is greater than zero, realloc tries to allocate a new block of memory for the object.
Difference between a union and a structure.
Members share the same storage space. Only one member can be referenced at a time.
How is a union declared?
A union can only be assigned at declaration with a value of the same type as its first member.
How much storage does a union require?
As much storage as is required by its largest member.