C-Language Reference

Links : Learn     Interview Questions     IDE
            

C Interview Questions - Page 4

< Previous Page
Question: What is the concept of pointers in C and how are they different from regular variables?
Answer: Pointers in C store memory addresses of other variables. They differ from regular variables as they store addresses rather than values.
 int num = 10;
 int *ptr = # // Pointer storing the address of num
 printf("Value of num: %d", *ptr); // Dereferencing pointer to get the value stored at that address

Question: How do you dynamically allocate a two-dimensional array in C?
Answer: A two-dimensional array can be dynamically allocated using a pointer to pointers (double pointer). Example:
 int rows = 3, cols = 2;
 int **arr = (int **)malloc(rows * sizeof(int *));
 for (int i = 0; i < rows; i++)
    arr[i] = (int *)malloc(cols * sizeof(int));

Question: What is the purpose of the 'typedef' keyword in C?
Answer: The 'typedef' keyword is used to create new data type names in C, providing a way to make code more readable and manageable. It simplifies complex declarations. Example:
 // defines an alias of type long as mylong
 typedef long mylong;

 // defines an alias for a structure as str
 typedef struct students {
    int id;
    char name[25];
 } str;

 // defines an alias for a pointer type
 typedef int* Int_ptr;

Question: What is the difference between NULL and '\0' in C?
Answer: NULL is a pointer that points to nothing. Typically NULL is used to represent a pointer that doesn't point to a valid memory location.
Whereas '\0' is the null character, what is used to terminate strings in C.

Question: How do you declare Pointer to a Pointer in C?
Answer: Declaring a Pointer to Pointer is very similar to declaring any pointer in C. The only difference is one has to place additional '*' before the name of the first pointer. Example:
 int value = 5;
 int *ptr = &value;    // storing address of value in pointer ptr
 int **p_ptr = &ptr; // pointer to the pointer is declared

< Previous Page