WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Basic · question 10 of 100

What is the difference between array and pointer in C?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, arrays and pointers are two related concepts that are often confused. Here are the main differences between arrays and pointers in C:

Memory allocation: Arrays in C are static data structures, which means that the memory for the array is allocated at compile-time and cannot be resized at runtime. Pointers, on the other hand, can be used to allocate memory dynamically at runtime using functions like malloc() and realloc().

    int array[10];  // Static allocation of an array with 10 integers
   int *ptr;       // Declaration of a pointer
   ptr = (int *) malloc(10 * sizeof(int));   // Dynamic allocation of an integer array with 10 elements

Assignment: Arrays in C are treated as a single block of memory, and their names refer to the memory address of the first element of the array. Pointers, on the other hand, are variables that store memory addresses.

    int array[5] = {1, 2, 3, 4, 5};    // Declaration of an integer array with 5 elements
    int *ptr = array;                  // Assigning the address of the first element of the array to a pointer

Increment and decrement: When used in expressions, arrays in C are automatically converted to pointers that point to the first element of the array. Pointer arithmetic can be used to increment or decrement the memory address stored in a pointer variable.

    int array[5] = {1, 2, 3, 4, 5};    // Declaration of an integer array with 5 elements
    int *ptr = array;                  // Assigning the address of the first element of the array to a pointer
    ptr++;                             // Incrementing the pointer to point to the second element of the array

Size and bounds checking: Arrays in C have a fixed size that is determined at compile-time, and it is the programmer’s responsibility to ensure that array indices are within the bounds of the array. Pointers do not have a fixed size, and it is the programmer’s responsibility to ensure that they point to valid memory addresses.

    int array[5] = {1, 2, 3, 4, 5};    // Declaration of an integer array with 5 elements
    int *ptr = array;                  // Assigning the address of the first element of the array to a pointer
    ptr[10] = 10;                      // Writing to an out-of-bounds memory location using a pointer can result in undefined behavior

In summary, arrays and pointers are related concepts in C programming language, but they have important differences in terms of memory allocation, assignment, increment and decrement, and size and bounds checking. Understanding the differences between arrays and pointers is essential for writing efficient, effective C programs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics