CS 23 Software Design and Implementation

X-Hour Quiz

Pesky Pointers Quiz

Pointers are typically the one thing people coming from Javaland struggle with when encountering C for the first time. If you come bottom up from assemblerland you are OK.

Here is our very pesky pointers quiz. We went over this in x-hour. You will get a marked up copy of your answer sheet back. We will grade it but it will have zero impact on your course grade. This is purely fun in a torturous sense of course!

Slippery Strings

First off stings and pointers. Write down what you think results from the execution of the following C code.

This exercise will offer some understand into how C handles strings. It will help your write correct string processing code.

Assume that:

 char* p = "ABCDE"; 

for all questions.

[1] printf("Print p: %s\n", p); 

Answer: Print p: ABCDE

[2] printf("Print p[1]: %c\n", p[1]); 

Answer: Print p[1]: B

[3] printf("Print *p+2: %c\n", *p+2); 

Answer: Print *p+2: C

Following string questions are not mentioned today, but they are also important.

[4] printf("Print p: %c\n", p); 

Answer: Print p: (STRANGE RANDOM CHARACTERS)

[5] printf("Print (p+1): %s\n", (p+1)); 

Answer: Print (p+1): BCDE

[6] char c[30];  
    int i;  
    for (i = 0; i < 5; i++)  
       c[i] = p[i];  
    printf("%s\n", c);

Answer: SEGMENT FAULT

Arrogant Arrays

Next, arrays and pointers. Write down what you think results from the execution of the following C code.

This exercise will offer some understand into how C handles arrays. It will help your write correct array processing code.

This set of problems ask you to guess the execution results of C code. Assume that:

int B[2][2];  
B[0][0]=1;  
B[0][1]=2;  
B[1][0]=3;  
B[1][1]=4;

for all questions.

[7]  printf("%u\n", B[0][0]); 

Answer: 1

[8] printf("%u\n", B); 

Answer: x (A memory address, let’s say it is x)

[9]  printf("%u\n", &B[0][0]); 

Answer: x

[10]  printf("%u\n", B[1]); 

Answer: (x + 8)

[11]  printf("%u\n", *B[0]); 

Answer: 3

Does this compile?

[12] int D[2][2] = B; 

Answer: No. Compiling Error.

Can we succeed in compiling by writing:

[13]  int D[][] = B; 

Answer: No. Compiling Error.

Does this compile?

[13] int *D = B; 

Answer: Yes.

When you want to declare a array and initialize it at the same time, the only way is as follows:

int D[2][2] = {1, 2, 3, 4};

or:

int D[] = {1, 2, 3, 4};

[14] int *D = B;  
     printf("%u\n", D[2]);

Answer: 3

Does this compile?

[15] int *D = B;  
     printf("%u\n", D[1][1]);

Answer: No. Compiling Error.

The Mysterious Question

[16] int E[2][2][2];  
      printf("%u\n", E+1);

Ideas on this one? Wei is waiting ..

We didn’t have time to go through these in the x-hour quiz but take a look:

[17] int *D = B;  
     printf("%u\n", D+2);

Answer: x + 8

[18] int *D = B;  
     printf("%u\n", *(D+2));

Answer: 3

Petulant Pointers to Pointers

This set of questions ask you to think about the value and equivalent expressions of an expression.

You will need the similar skill in Lab4.

Suppose:

int a = 12;  
int b = &a;  
int **c = &b;

[19] *b 

Answer: a, 12

[20]  c 

Answer: &b

[21]  *c 

Answer: b, &a

The following questions are also important, but they are not mentioned in class.

[22]  **c 

Answer: a, *b, 12

[23] *(*c) 

Answer: a, *b, 12

Congratulations - now you know pointers, right?

There is an excellent tutorial on pointers. There are lots of other pointer topic we will not have time to cover, such as, pointers to functions; curious? Then Google Ted Jensen’s Tutorial on Pointers.

Have fun!