Pointers Flashcards

1
Q

Are pointer variables passed by value or by reference to functions?

A

Pointer variables are passed by value to functions

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

What is the difference between & and *?

A

&: Given a variable of type X, it gives X* ( the appropriate pointer ) the address of the variable

*: Given a variable of type X*, show what x points to

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

What is a pointer?

A

A pointer is a variable that contains the address of another variable

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

what does the array variable point to?

A

The array variable points to the first element in the array

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

Can you use the square brackets [] with a pointer to an array

A

yes, the [] can be used interchangeably with an array as well as a pointer

Array indexing and pointer arithemtic works identical

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

What is the size of a pointer?

A

The size of a pointer is the width of the processor

sizeof(int*) = 4 bytes in a 32 bit system

sizeof(int*) = 8 in a 64 bit system

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

What is the difference between an array and a pointer to an array?

or

How to distinguish between these 2?

A

The size of the array computer using the sizeof operator will be the sum of the sizes of all the elements inside the array but the sizeof the pointer will always be the width of the system.

Also, variable assignments are invalid in the case of arrays.

int array[100];

array = (some other variable) // invalid because there are not variables associated with an array

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

In what chunks do pointers get incremented?

A

The pointer gets incremented in chunks of the size of the type of the pointer.

So, if the pointer is an int*, then adding 1 to the pointer will add sizeof(int) usually 4, to the address.

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

What are dangling pointers?

A

Dangling pointers are pointers that point to variable which was created on the stack which no longer is present in that memory location in the stack.

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

Why is the string.h library important?

A

it contains functions for working with strings.

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

What is the prototype for strcpy and what should you watch out for?

A

The prototype for strcpy is char* strcpy ( char* str1, const char* str2 );

You need to make sure that str1 has enough memory to store str2

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

Prototype and use of gets function in string.h library?

A

Prototype: char* gets( char* s);

reads a input into s until an EOF or \n is encountered

It could overflow the character array s.

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

Prototype and use of puts function in string.h library?

A

Prototype: puts int puts( const char* s)

Don’t know what it returns lol

it prints out the string s and also appends a newline.

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

Prototype and use of sprintf() function in string.h library?

A

Prototype:

int printf( char* s, const char* format, ….)

Works like the printf function but instead of outputting the string to the console, the string is stored written into a variable s.

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

Prototype and use of sscanf function in string.h library?

A

Prototype: int sscanf(char* s, char* format, …..)

It works like scanf, but instead of reading the input from the stdin, it reads in the input from a variable and the programmer has to specify the input specifiers in the format string and the input variables ( how many ever there are ) after that.

17
Q

How does the strcmp function work?

A

The strcmp function takes in 2 strings and returns

  • 0 if they are equal
  • > 0 if the first one is greater than the second one
  • < 0 if the first one is smaller than the second one
18
Q

What is the void pointer used for?

A

The void pointer is a polymorphic pointer that can point to any data of any type.

Void* cannot be dereferenced and it cannot be used for pointer arithmetic

It can only be used to point to data and not to functions

19
Q

What is the null pointer?

A

Null is a generic pointer that doesn’t point to anything and is represented as (void*)0

it cannot be dereferenced

It is possible to turn a null pointer into any type of pointer.

20
Q

How do you access a member for a struct given a pointer s to a struct? 2 ways

A
  • (*s).m
  • s -> m
21
Q

What is a polymorphic pointer?

A

A polymorphic pointer is a pointer that can point to any type.

22
Q
  1. Pointer can be cast from one type to any type
  2. Implicit pointer casting leads to a warning
  3. You can pass function pointers to other functions and have them execute the function specified by the pointers.
A
23
Q

What are pointers to functions used for?

A

They are used to refer to functions that have been already created and can be used to pass around functions and assign them to variables.

24
Q

How do you create function pointers?

A

<return> *(<name>)(<arguments>) = function name;</arguments></name></return>

int addNumbers(int a, int b) { return a + b;}

int (*fun)(int a, int b) = addNumbers;

25
Q

How do you typedef a function pointer?

A

in order to define a type of function, use the following syntax

typedef <return> (*<name>)(argument list without the names of the variables)</name></return>

typedef int(*fun_ptr) (int, int); creates a type fun_ptr that can point to a function which takes in 2 integer arguments and returns an integer.

26
Q
A
27
Q

What are asserts and how could it be used?

A

They are functions that can be used for internal checks within the program

It should be used to check for bugs within the code and not to check the user input.

assert(x) aborts the program if x is false.

28
Q

How would you disable assert?

A

define NDEBUG

Assert can be disabled by using

29
Q
A