Week 4: Functions, Pointers, Structures Flashcards

1
Q

Explain the difference between passing by value and passing by reference…

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What parameter type do we use to enable a pass-by-reference argument?

A

Parameter must be a pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When we pass an array to the function, what are we acrtually passing to the function?

A

We are passing the first memory address of the array to the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Do we need to user the & prefix when passing an array to a function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When a function returns an array, what is it actually returning and why?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are parameters to functions always pass by reference or pass by value?

A

Pass by value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does a struct in C not contain?

A

Methods. It only has fields;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of typedef? How is it declared?

A

Typedef enables you to change syntax in regards to what your struct is called.
typedef struct ClassGrades grades;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What determines the memory needed to hold a struct?

A

This depends on the number of fields needed for each instance of the struct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a struct of a person with a name, sex and age…

A

struct person { int age; char sex; char name[50]; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you assign a character array that from a struct?

A

Use strcpy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the memory of a struct depend on?

A

The cumulative size of the data types contained in the struct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly