Functions, Pointers, & Arrays Flashcards

(28 cards)

1
Q

Function

A

When called, memory is allocated to the stack for its value parameters, local variables, and name (if not void). It calls statements until a return statement or at closing brace, then returns control to where it was called.

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

What must happen before a function is called?

A

The compiler must process either the function protype or the function definition.

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

Function Declaration

A

Identifier that names a function and specifies the number of parameters, type of each parameter, and type of return value.

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

What is the basic syntax for a function declaration?

A

Return_type function_name(parameter declarations);
(also called the function prototype).

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

What is the basic syntax for a function defenition?

A

Return_type function_name(parameter declarations) {
statements
}

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

Formal Parameter

A

Dummy variable listed in the
subprogram header and used in the subprogram.

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

Actual Parameter

A

Represents a value or address used in the subprogram call statement.

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

Pass-By-Value

A

Pass a copy of the parameter value.

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

Pass-By-Reference

A

Pass a reference to the parameter value (using a pointer or address).

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

Default Parameters

A

Simple types, structs, and classes are value parameters by default but can be made reference parameters by using ‘&’ after their type. Arrays are always reference parameters.

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

Reference Parameters

A

Used when the function is to assign or change the value of a variable from the calling block.

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

Function Overloading

A

Functions with the same name but different number/type of parameters.

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

Array

A

A group of elements of the same type in contiguous memory locations.

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

Array Indexing

A

Accessing member x[i] requires calculation from x[0], the first element in the array.

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

Characteristics of C/C++ Arrays

A

Do not behave like first-class objects; they cannot be copied using ‘=’. They do not remember how many items it can store and do not check for index validity.

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

Array Indexing in C/C++

A

Creating an array creates a pointer to the first element in the array. For example, int x[10] creates an integer array with 10 elements and assigns x a constant pointer type (int*) with value &x[0].

17
Q

C-Style String

A

An array of characters ending with the null character ‘\0’. Can convert from C++ string to C-style using c_str().

18
Q

Pointer

A

A variable that contains a memory address. They can be used to access memory stored in the heap. Pointers must be explicitly declared and they must be initialized.

19
Q

Null Pointer

A

A special pointer value that does not refer to any valid memory location. It is given the ‘nullptr’ keyword.

20
Q

Pointer Assignment

A

One of two fundamental pointer operations. Used to set a pointer variable’s value to some useful
address. The ‘*’ operator is used for struct/class names while the ‘->’ operator is used for struct/class variables.

21
Q

Pointer Dereferencing

A

One of two fundamental pointer operations. Yields the value stored at the location represented by
the pointer’s value. Can be explicit or implicit.

22
Q

Dereferencing an Uninitialized/Null Pointer

A

Causes undefined behavior. There could be a crash, a core dump, or a segmentation fault.

23
Q

C/C++ Valid Pointer Operators

A

The operators ‘=’, ‘*’, and ‘->’ for pointers and the relational operators ‘==’ and ‘!=’.

24
Q

C/C++ Pointer Arithmetic

A

Pointers can be added to or subtracted from. Take the lines:
float stuff[100]
float *p;
p = stuff;
for example. *(p + 5) would point to stuff[5].

25
References
Similar to a constant pointer; it contains a memory address and cannot be changed. It needs to be initialized when declared (compile error) and it cannot be dereferenced.
26
Intermixing Pointers and Const
There are two ways pointers and constants can be mixed: 1. Constant pointer that cannot point to a different memory address, but the value it points to can be changed. Must be initialized upon declaration. 2. Pointer to a const variable, where the value of variable cannot be changed through the pointer but can be changed independently.
27
Passing Arrays as Arguments
When an array is passed as an argument, the memory address of the first element in the array is called to the function (as a const pointer in C/C++). Parameters declared as int *ap and int ap[] work the same.
28
C/C++ Main Function
Can take either no arguments or two arguments: int main() int main(int argc, char *argv[]) where argc is the number of C-type strings and argv is an array of pointers to C-style strings. Whether or not command-line arguments are passed, the first array entry will be the name of the program.