C Interview Questions on Arrays

We discuss various C Language interview questions on Arrays. We discuss how Arrays are initialized, passed to functions and stored in memory.

Interview Questions

  1. How are arrays initialized?
  2. How are character arrays initialized?
  3. How are arrays stored in memory?
  4. How are arrays and pointers related?
  5. Can you change the address pointed to by an array variable?
  6. What are Character Pointers?
  7. What is the difference between a Character Array and Character Pointer?

How are arrays initialized?

Below example shows how to initialize an array:

#include <stdio.h>

int main()
{
	int marks[] = {99,97,98};
}

How are character arrays initialized?

Below example show 2 of the ways of initializing character arrays. We will look at a third way when we discuss about pointers.

#include <stdio.h>

int main()
{
	char array2[] = { 'A', 'B', 'C', 'D', '\0' };
	char array3[] = "ABCD";
}

How are arrays stored in memory?

Picture below shows how the array declared below is stored in memory. marks would have the address of first (index 0) element of the array. First element of the array (Index:0 Value:35) is stored at 6006. So that address is stored for the variable marks. Rest of the array is to stored in contiguous locations after 6006. (We assume size of integer is 2 bytes)

	int marks[] = {35,45,67};

If pa points to a particular element of an array, then by definition pa+1 points to the next element, pa+i points i elements after pa, and pa-i points i elements before.

How are arrays and pointers related?

An array variable stores the address of where the first element(index 0) of the array is stored. Hence an array variable is actually a pointer. In the above example marks is similar to a pointer.

Program below shows the different pointer operations that can be done on an array.

#include <stdio.h>

int main()
{
	int marks[] = {35,45,67};
	printf("%d\n",*marks);//35
	printf("%d\n",*(marks+1));//45
	printf("%d\n",*(marks+2));//67
	printf("%p\n",marks);//0x7fff57a3baac
}

Can you change the address pointed to by an array variable?

No. That’s actually one of the few differences between a normal pointer and an array. The value of a variable or expression of type array is the address of element zero of the array. An array name is not a variable; constructions like marks=pmarks and marks++ are illegal.

#include <stdio.h>

int main()
{

	int marks2 = {96,97,98};
	int marks[] = {35,45,67};
	int *pmarks;
	pmarks = marks2;
	
	marks = pmarks; //Not Allowed : Compilation Error

}

What are Character Pointers?

Below program shows an example of character pointer.

#include <stdio.h>

int main()
{
	char* text = "Something";
}

What is the difference between a Character Array and Character Pointer?

Below examples shows the two critical differences. textptr is a pointer (size : 8 or 4 based on the memory size of the execution environment) and can be changed. textarr is a constant (value cannot be changed). Doing sizeof(textarr) returns the size of the array (19 in the example below. including 1 character for null character at end of array)

#include <stdio.h>

int main()
{
	char* textptr = "SomethingIsBrewing";
	char textarr[] = "SomethingIsBrewing";

	printf("sizeof(textptr):%d sizeof(textarr):%d",sizeof(textptr),sizeof(textarr));//sizeof(textptr):8 sizeof(textarr):19

	textptr = textarr;//Allowed
	textptr = "SomethingElse";//Allowed
	textarr = textptr;//Nope - NOT ALLOWED. COMPILATION ERROR!!!
}
FREE JAVA INTERVIEW GUIDE

ONE SUBSCRIBER EVERY MONTH WINS 100% DISCOUNT CODE FOR INTERVIEW GUIDE WITH 23 VIDEOS

We Respect Your Privacy