Pointers (In Arrays)

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/21

flashcard set

Earn XP

Description and Tags

Information About c++

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

22 Terms

1
New cards

What is a pointer?

A pointer stores the memory address of another variable.

2
New cards

What must be specified when declaring a pointer? 

When declaring a pointer, we must
specify what type of value it will be
pointing to.

3
New cards

A pointer to an integer is different
than a pointer to a double, etc.
WHY?

  • They point to different data sizes.

  • They point to different types, int* points to iptr and double* points to dptr

4
New cards

How do you declare a pointer? 

  • To declare a pointer, follow the data
    type the pointer will be pointing to
    with an asterisk.
    Example
    int * intPointer; // intPointer will hold
    // the address of an
    // integer

5
New cards

What must you do where several pointers are declared?

You must precede each
pointer name with an asterisk.


int* x, y;

// x is a pointer to an
integer, y is an integer


int w, * z;

// w is a pointer to an
integer, z is a pointer
to an integer

6
New cards

What will the reference operator “&” return?

The reference operator, &, will return
the address of a variable.

7
New cards

What will the dereference operator * do?

The dereference operator,*, will
dereference a pointer. Which means to go to the address and get the value.

Example:
int x = 3;
int *xptr = &x;
*
xptr += 5;
cout << x << endl;

Output will be 3 sine the value remains unchanged.

8
New cards

& and * 

  • These operators are on the same
    precedence level as the pre-increment
    and decrement operators.
    • These operators commute from right to left.

9
New cards

Can you assign a pointer of one type to a pointer of a different type? Yes or no, if no why not?

  • We can not assign a pointer of one
    type to a pointer of a different type.

  • Example:

int *xtpr = &x

double *yptr = &y;

xptr = yptr

//error

10
New cards

What happens when you add to a pointer?

Adding an integer value to a pointer
changes the address that the pointer
is holding, thus changing the memory
location that the pointer is pointing to.


• Ex the ++ operator would move the
pointer to the next memory location.

11
New cards

Why do would we use pointers as
function parameters if we already
have call by reference capabilities?

Pointers are more versatile. 

12
New cards
<p><span>Should the pointer parameter for Func have any const qualifiers? </span></p>

Should the pointer parameter for Func have any const qualifiers?

No const qualifier should be added to the pointer parameter because Func modifies both the values pointed to and the pointer itself.

13
New cards
<p>What could you do to prevent modifying the values from the picture?&nbsp;</p>

What could you do to prevent modifying the values from the picture? 

If you wanted to prevent modifying the values, you'd use const int* ptr, but that would break the function.

14
New cards

What could you do to prevent modifying the pointer?

If you wanted to prevent modifying the pointer, you'd use int* const ptr, but that would also break the pointer increment.

15
New cards
<p><span>Is the function call on the second line of the main valid?</span></p>

Is the function call on the second line of the main valid?

The function call Func(x); is valid. When you pass an array to a function, the array decays to a pointer to its first element. So x is implicitly converted to int* pointing to x[0].

16
New cards
<p>What will be the output of this code?&nbsp;</p>

What will be the output of this code? 

  1. Will grab 5 elements from the array. (100, 84, 64, 97, 0). The elements in the rest of the array are set to 0.

  2. The while loop shows that if the number is greater than 0 then 5 will be added to the value.

  3. So, x[0]= 100+5=105. x[1]= 84+5=89. x[2]=64+5=69. x[3]=97+5=103. x[4]=0.

  4. Loop will stop at index 4. So the final output will be. 105 89 69 103 0 0 0 0 0 0 

17
New cards

What can change using variables declared each of the following ways?

  1. type * variable

  2. const type * variable

  3. type * const variable

  4. const type * const variable

  1. The pointers target and the value being pointed to.

  2. The pointers target but cannot change the value being pointed to.

  3. The value being pointed to but cannot change the pointers target.

  4. You cant change anything. (The value being pointed to or the target)

18
New cards

What does “new” stand for in c++?

It is used to allocate memory dynamically at runtime.

19
New cards

What does “delete” stand for in c++?

It is used to free dynamically allocated memory.

*Use “delete” if you use “new”.

20
New cards

When to use “new” and “delete”. With or without [].

  • When you use “new” use “delete”

  • When you use “new[]” use “delete[]”

21
New cards

What is direct memory allocation?

When memory is reserved at run-time (as
opposed to compile-time).

22
New cards

What is a memory leak?


Its when memory is reserved for the program but we’ve lost track of where it is / aren’t using it.

Explore top flashcards

respiratory system
Updated 695d ago
flashcards Flashcards (22)
Exam 2 For Dorth
Updated 229d ago
flashcards Flashcards (110)
The Immune System
Updated 324d ago
flashcards Flashcards (35)
Biology Unit 7
Updated 908d ago
flashcards Flashcards (210)
religion final
Updated 887d ago
flashcards Flashcards (29)
respiratory system
Updated 695d ago
flashcards Flashcards (22)
Exam 2 For Dorth
Updated 229d ago
flashcards Flashcards (110)
The Immune System
Updated 324d ago
flashcards Flashcards (35)
Biology Unit 7
Updated 908d ago
flashcards Flashcards (210)
religion final
Updated 887d ago
flashcards Flashcards (29)