Lecture 3 - Pointers, Arrays, Strings and Dynamic Memory Allocation Flashcards

1
Q

What is a pointer in C?

A

A pointer is a variable that stores the memory address of another variable. It is declared using the * symbol.

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

How do you declare a pointer to an integer in C?

A
int *ptr;

This declares ptr as a pointer to an int.

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

How do you get the address of a variable in C?

A

The address of a variable can be obtained using the & operator.

int x = 10;
int *ptr = &x;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you dereference a pointer in C?

A

Dereferencing a pointer is done using the * operator to access the value stored at the memory address.

int x = 10;
int *ptr = &x;
int y = *ptr;  // y gets the value 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is pointer arithmetic in C?

A

Pointer arithmetic allows you to increment or decrement pointers. When you increment a pointer, it moves by the size of the data type it points to.

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
ptr++;  // Now points to the second element
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is an array different from a pointer in C?

A

An array is a fixed-size sequence of elements, whereas a pointer can point to any memory location. However, the name of an array acts like a pointer to the first element.

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

What is the relationship between arrays and pointers in C?

A

The name of an array is a pointer to its first element. For example, if arr is an array, arr is equivalent to &arr[0].

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

How do you declare and initialize an array in C?

A
int arr[5] = {1, 2, 3, 4, 5};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a string in C?

A

A string in C is an array of characters terminated by a null character '\0'. Strings are stored as char[] or char*.

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

How do you declare and initialize a string in C?

A
char str[] = "Hello, World!";

This creates a string with the value "Hello, World!" and an implicit null terminator '\0'.

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

What is malloc and how is it used in C?

A

malloc is used to dynamically allocate memory at runtime. It returns a pointer to the allocated memory.

int *arr = (int*)malloc(5 * sizeof(int));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you access individual characters in a string?

A

Strings are arrays of characters, so you can access individual characters using array notation.

char str[] = "Hello";
char first_char = str[0];  // 'H'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is free and why is it used?

A

free is used to release memory that was previously allocated using malloc, calloc, or realloc.

free(arr);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is calloc and how is it different from malloc?

A

calloc allocates memory for an array of elements and initializes all the bytes to zero. Unlike malloc, it takes two arguments (number of elements and size of each element).

int *arr = (int*)calloc(5, sizeof(int));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of realloc in C?

A

realloc is used to resize a previously allocated memory block.

arr = (int*)realloc(arr, 10 * sizeof(int));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you pass an array to a function in C?

A

Arrays are always passed by reference (as pointers) to functions.

void printArray(int arr[], int size);
16
Q

What is a multi-dimensional array in C?

A

A multi-dimensional array is an array of arrays. For example, a two-dimensional array can be declared as:

int arr[3][4];
17
Q

How do you dynamically allocate a 2D array in C?

A

A 2D array can be dynamically allocated by using a pointer to a pointer and malloc or calloc.

int **arr = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
    arr[i] = (int*)malloc(cols * sizeof(int));
}
18
Q

What are the default initialization values for arrays in C?

A

Global arrays: Initialized to zero.
Local arrays: Contain garbage values unless explicitly initialized.

19
Q

How can you create a dynamically allocated string in C?

A

You can use malloc or calloc to allocate memory for a string dynamically.

char *str = (char*)malloc(20 * sizeof(char));