Week 4: Functions, Pointers, Structures Flashcards
Explain the difference between passing by value and passing by reference…
Pass by value -> The function argument passed has a copy of it made in the function scope.
Pass by reference -> The argument is a memory address of a variable. Thus, manipulation in scope changes the value out of the function scope.
What parameter type do we use to enable a pass-by-reference argument?
Parameter must be a pointer.
When we pass an array to the function, what are we acrtually passing to the function?
We are passing the first memory address of the array to the function.
Do we need to user the & prefix when passing an array to a function?
No, because we are passing the memory address of the array by default. This is because the array variable points to the memory address of the first byte of the array.
When a function returns an array, what is it actually returning and why?
A function returns a pointer to the first memory address of an array.
This is to prevent the compiler from re-allocating memory and recreating the array.
Are parameters to functions always pass by reference or pass by value?
Pass by value.
What does a struct in C not contain?
Methods. It only has fields;
What is the purpose of typedef? How is it declared?
Typedef enables you to change syntax in regards to what your struct is called.
typedef struct ClassGrades grades;
What determines the memory needed to hold a struct?
This depends on the number of fields needed for each instance of the struct.
Create a struct of a person with a name, sex and age…
struct person { int age; char sex; char name[50]; }
How do you assign a character array that from a struct?
Use strcpy.
What does the memory of a struct depend on?
The cumulative size of the data types contained in the struct.