Important Introduction to Programming Short Questions
Chapter: 15
Question 1:
Why should arrays be at least one character space larger than the number of printable characters?
Answer:
Arrays should be at least one character space larger than the number of printable characters to accommodate the null character \0 that terminates the string.
Chapter: 16
Question 2:
What does the statement char myName[] = "Full Name"; do?
Answer:
The statement creates a character array and initializes it with the string "Full Name". In C, character strings are null-terminated, meaning that the last character is '\0'.
Chapter: 16
Question 3:
How is char * myNamePtr = "Full Name"; different from char myName[]?
Answer:
The pointer myNamePtr can be reassigned to point to a different string, whereas myName is a constant pointer and cannot be changed to point to a different memory location.
Chapter: 16
Question 4:
What happens when you add 1 to a pointer pointing to a two-dimensional array?
Answer:
When you add 1 to a pointer pointing to a two-dimensional array, the pointer jumps over an entire row, not just a single element. This is because the pointer increments by the size of the row, which is equal to the number of columns multiplied by the size of each element.
Chapter: 16
Question 5:
How can the element at the second row and third column of a 2D array be accessed?
Answer:
The element at the second row and third column can be accessed using multi[1][2] or by using pointer dereferencing as *(*(multi + 1) + 2).
Chapter: 16
Question 6:
What does the **multi expression represent for a two-dimensional array?
Answer:
The expression **multi dereferences the pointer twice and gives the value of the first element of the first row in the two-dimensional array.
Chapter: 16
Question 7:
How can the fourth row and fourth column of a 2D array be accessed using pointers?
Answer:
The fourth row and fourth column can be accessed using the expression *(*(multi+3)+3), which dereferences the pointer to access the specific element.
Chapter: 16
Question 8:
What is an array of pointers and why is it useful?
Answer:
An array of pointers is an array that stores addresses of variables. It is useful because it allows for variable-length storage of data, such as strings of different lengths, which helps avoid wasting memory when using fixed-length arrays.
Chapter: 16
Question 9:
How is an array of pointers declared for storing character strings?
Answer:
An array of pointers for storing character strings can be declared as char * myarray[] = {"Amir", "Jehangir"};. This array holds pointers to different strings of varying lengths.
Chapter: 16
Question 10:
What are the advantages of using an array of pointers over a 2D array?
Answer:
An array of pointers allows for variable-length data storage, which avoids wasting memory that occurs in a 2D array where all rows must have the same number of columns, even if not all rows need that much space.