Lecture 10 Flashcards

POINTERS -cont

1
Q

______ alerts compile that the contents of a variable or an array will not be changed by
the program

A

const

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

Can a pointer be declared as a const

A

Yes, A const pointer is a pointer whose address can not be changed after initialization

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

If the pointer variable is always set pointing to c?

char c = ‘X’;
char *charPtr = &c;

A

char * const charPtr = &c;

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

If, the contents within the location pointed to by charPtr will not change through the pointer variable charPtr

char c = ‘X’;
char *charPtr = &c;

A

const char *charPtr = &c;

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

If the pointer variable is always set pointing to c?

char c = ‘X’;
char *charPtr = &c;

A

char * const charPtr = &c;

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

if both the pointer variable itself and the contents of the location it points to will not be changed through the pointer.

char c = ‘X’;
char *charPtr = &c;

A

const char * const charPtr = &c;

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

Write the following statement:
You have an array of 100 integers called values and want to define a pointer variable called valuesPtr, to access the integers contained in this array

A

int values[100];
int *valuesPtr

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

you must designate the pointer as pointing to the type of element that is contained in the array when ___

A

you define a pointer that is used to point to the elements of an array

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

To set valuesPtr to point to the
first element in the values array,
you simply write

A

valuesPtr = values

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

When do you not use the address operator

A

When the name of the
array without an address
operator is a pointer to the first
element of the array

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

When we’re passing arrays to functions, why isn’t a copy being made?

A

because we are actually passing the pointer to the array

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

An equivalent way of producing a pointer to the start of values is to apply the address operator to the first element of the array

A

valuesPtr =&values[0]

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

Are the following statements equivalent:
valuesPtr[i] = 3; to values[i] = 3;

A

Yes, because after the initialization, (valuesPtr) points to the start of the array (values) and therefore, we can sequence through the elements of the array with the pointer variable as we would just as with the array itself

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

Would (values) be considered a Pointer?

A

Yes, because the compiler gives you a const pointer variable called (values) after declaring the int array (values), that is pointing to the address of the first element of the array (the start of the array)

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

Why must we use a pointer to (const char)

A

Because string literals are constants (in the initialized data section)

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

What happens when you add an integer n to a pointer?

A

Adding an integer n to a pointer moves the pointer ahead by n elements of the type it points to. For example, adding n to an int* pointer moves it forward by n integer-sized elements

17
Q

If you have an int* ptr, how many spaces does adding n to the pointer move it?

A

It moves the pointer by n spaces, where each space corresponds to the size of an int. So, if ptr points to an int, ptr + n points to the n-th integer after the one ptr currently points to.

17
Q

What is the equivalent of valuesPtr[3] using pointer arithmetic and dereferencing?

A

The expression valuesPtr[3] is equivalent to *(valuesPtr + 3). Both expressions access the 4th element in the array (since indexing is 0-based).

17
Q

How can you set a pointer to point to the second element of an array using the address-of operator (&)?

A

You can set a pointer to the second element of the array using valuesPtr = &values[1];. This assigns the address of the second element of the array to the pointer.

17
Q

How can you use pointer arithmetic to access the n-th element of an array that a pointer points to?

A

You can access the n-th element by using pointer arithmetic like this: *(ptr + n). This moves the pointer n elements forward and then dereferences it to get the value at that position.

17
Q

What is the alternative way to set a pointer to the second element of an array using pointer arithmetic?

A

Instead of using the address-of operator, you can also use pointer arithmetic: valuesPtr = values + 1;. This moves the pointer from the first element to the second element by adding 1.

18
Q

How does pointer subtraction work, and what effect does it have on the pointer’s location in memory?

A

Pointer subtraction works similarly to addition but in reverse. Subtracting n from a pointer moves it backward by n elements of the type it points to, effectively moving it to earlier positions in the array

19
Q

How can the increment (++) and decrement (–) operators be used with pointers to move through an array?

A

The increment (++) operator moves the pointer to the next element in the array, while the decrement (–) operator moves it to the previous element. For example, ptr++ moves the pointer forward by one element, and ptr– moves it backward.

20
Q

What is the difference between accessing an element using valuesPtr[1] and *(valuesPtr + 1)?

A

There is no difference in functionality. Both valuesPtr[1] and *(valuesPtr + 1) access the second element in the array. The former uses array indexing, while the latter uses pointer arithmetic.

20
Q

How does valuesPtr += 2; affect where the pointer is pointing in an array?

A

It moves valuesPtr to point to the third element after the current one. If it originally pointed to the first element, it will now point to the third element.

21
Q

How can you use valuesPtr = &values[1]; to set the pointer to a specific element in the array?

A

The expression valuesPtr = &values[1]; sets valuesPtr to the address of the second element of the array, meaning it will now point to that element.

22
Q

is this a valid code?

int i1 = 0;
int *ptr = &i1;
ptr += 1

A

This is valid code, even though ptr now points to memory that the program
did not allocate.

23
Q

What would this code return if n=5?
int arraySum(int array[], const int n)
{
int sum = 0;
int *ptr;
int * const arrayEnd = array + n;
for (ptr = array; ptr < arrayEnd; ++ptr) {
sum += *ptr;
}
return sum;

A

15

24
Q

When working with pointers to functions, the C compiler needs to know the
following:

A
  • the pointer variable points to a function,
  • the type of value returned by that function, and
  • the number and types of its arguments
25
Q

What is an in/out parameter?

A

An in/out parameter is a function parameter that serves a dual purpose: it is used to provide input to the function and also to return a value from the function. This is especially useful in C, which allows only a single return value.

26
Q

Why might a function use an in/out parameter instead of just returning a value?

A

Using an in/out parameter can be beneficial when you want the function to indicate success or failure (status indication) through the return value while still modifying additional data or values through parameters

27
Q

What is the main advantage of using pointers for in/out parameters in C?

A

The main advantage is that they allow functions to both modify data and communicate status effectively, facilitating more complex and flexible programming patterns while adhering to C’s single return value limitation.