Skip over navigation

Contents

Review of Pointers

Review Test

Table of Contents

Further Reading

In C arrays and pointers can always be used interchangeably.

In C, a string is equivalent to a pointer to a character.

In C, an array with ten elements is indexed from 1 through 10.

If str is a character pointer variable that holds the string "Read SparkNotes", then str[3] refers to the character 'a'.

In C, when an array is passed to a function, only the address of the array is passed to the function.

In C, a parameter is passed by reference when the address of a variable is passed to the function.

The declaration typedef int *i_ptr; creates a variable called i_ptr which can hold the addresses of integers.

The function malloc can be used to dynamically allocate memory for an array.

If rec is a struct variable and number is a field of the structure, then the number field of rec is expressed as rec->number.

Let p be a pointer to an integer and i be an integer variable. Which of the following is not a correct assignment?

In C, the end of a string is marked by

A segmentation fault occurs when:

The expression sizeof(int) refers to

What is the output of the following program?


int main () 
{
	int i, j, *p, *q;
	p = &i;
	q = &j;
	*p = 5;
	*q = *p + i; 
	printf("i = %d, j = %d\n", i, j);
	return 0;
}

What is the output of the following program?


void spark(int A[]) {
	printf("A[0] = %d, A[1] = %d, A[2] = %d\n", A[0], A[1], A[2]);
	A[1] = 0;
}

int main() {
	int A[3] = {1, 2, 3};
	spark(A);
	printf("A[0] = %d, A[1] = %d, A[2] = %d\n", A[0], A[1], A[2]);
	return 0;
}

What is the output of the following program?


void add_10(int a, int *p) {
	*p = a  + 10;
}
      
int main() {
	int x = 2, y = 6;
	add_10 (x, &x) ;
	printf("x = %d, y = %d\n", x, y) ;
	add_10 (x, &y) ;
	printf("x = %d, y = %d\n", x, y) ;
	return 0;
}

In most languages and on most computer systems, NULL is what value?

int *x; and int* x; both have the same effect.

Functions aren't basic data types, so you can't have pointers to them.

A memory leak happens when we allocate memory using malloc() and then free it using free().

What does the following function do?


void mystery(int* a, int* b)
{
	if (*a > *b) {
		int temp = *a;
		*a = *b;
		*b = temp;
	}
}

A dangling pointer is a pointer which points to a thing in memory which has been free'd or has vanished for some other reason. Dangling pointers are bad because you don't know what's at that spot any longer. In the following code, what is the dangling pointer?


int* y = malloc(sizeof(int));
int* z = y;
free(z);

Two or more memory locations can have the same address.

A pointer variable can point to floating-point values only.

An array can be used as a constant pointer.

Pointers can store the addresses of which data structures?

What is the name of the & operator?

Which of the following is not a use of the * operator?

In the following code


int spark;
int *p;
p = &spark;
p += 2;
the variable spark is stored at memory address 0x1234, and sizeof(int) == 4. What is the value of p at the end of this code segment?

Which of the following are equivalent, assuming that arr is an integer array and that ptr is an integer pointer pointing to the start of the array?

What data structure, in words, does the following declaration declare? float * measures[250];

Given the following variable and array declarations


float arr[] = {1.0, 2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7};
float * ptr1, *ptr2;
which of the following assignments is not allowed?

What does the variable i contain after the following code executes?


int i = 17;
int *p = &i;
*p = 98;

What, if anything, is wrong with the following code?


void *p;
p = (float*) malloc(sizeof(float));
*p = 7.89;

After the following code:


int *p; 
int i; 
int k; 
i = 63; 
k = i; 
p = &i; 
which of the following statements will change the value of i to 2105?

After the following code:


int i = 23; 
int j = 72; 
int *p1; 
int *p2; 
p1 = &i; 
p2 = &j; 
*p1 = *p2;
what are the values of i and j, respectively?

After the following code:


int i = 2; 
int k = 4; 
int *p1; 
int *p2; 
p1 = &i; 
p2 = &k; 
p1 = p2; 
*p1 = 6; 
*p2 = 8; 
what is the value of i?

After the following code:


int i = 2; 
int k = 4; 
int *p1; 
int *p2; 
p1 = &i; 
p2 = &k; 
p1 = p2; 
*p1 = 6; 
*p2 = 8; 
what is the value of k?

After the following code:


int i = 2; 
int k = 4; 
int* p1; 
int* p2; 
p1 = &i; 
p2 = &k; 
*p1 = *p2; 
*p1 = 6; 
*p2 = 8; 
what is the value of i?

Given the following array declaration and function definition


void spark(int z[]); 
int x[10];
how would you call the spark function with x as an argument?

Given the following array declaration and function definition


void spark(int z[]); 
int x[10];
suppose that spark() changes the value of z[1]. Is x[1] affected?

After this code executes


int arr[10];
int i;
i = &arr[9] - arr;
what is the value of i?

In a C/C++ program, the main() function is actually defined as int main(int argc, char **argv); What is argv?

What is 0xabcd in binary?

In the following code


int a = 54;
int *p = (int *)malloc(1);
*p = 72;
p = &a;
which of the following is NOT a possible problem?

Follow Us