Pointers

  1. True/False: Is the following legal?
    	int x = 5;
    	float *y;
    	y = &x;
    	
  2. True/False: Is the following legal?
    	int *p;
    	int r;
    	p = r;
    	
  3. True/False: Is the following legal?
      int *p;
      *p = 5;
      printf( "%d \n", *p );
    	
  4. In the following definition, how many variables are pointers and how many are just int data types?
    	int*  ptrA, ptrB, ptrC;
    	
  5. True/False: Is the following legal?
    	int*  ptr = 59;
    	
  6. True/False: Is the following legal?
    	int  val = &y;
    	
  7. Given the following function prototype, write the code to correctly call the function with int x, y values:
    	void  doStuff( int, int* );
    	
  8. True/False: Is the following legal?
      int *p;
      p = NULL;
      *p = 5;
    	
  9. What is the operator used with a pointer to dereference the address contained in the pointer?
  10. Which of the following statements will not add one to a variable pointer named p?
    1. *p++;
    2. *(p++);
    3. (*p)++;
    4. *p = *p + 1;
    5. p = p + 1;
    6. *p += 1;
    7. p += 1;
  11. Given the following:
    	int i;
    	float f;
    	int* pd;
    	float* pf;
    	

    Which of the following can you not do based on the above declarations?
    1. i = 5;
    2. f = 5;
    3. pd = &i;
    4. pf = &f;
    5. pd = pf;
  12. Write a function that receives a floating point number and sends back the integer and fraction parts.
  13. In the following situations, when should you use pointers in the function header?
    1. Function that takes two numbers and returns the higher of the two
    2. Function that takes two numbers and returns the average
    3. Function that takes an array of integers and returns the highest and lowest values
    4. Function that takes a struct that includes a name and GPA, and prints the information
    5. Function that takes a string and a struct that includes a name and GPA, and changes the name in the struct with the string passed in
    6. Function that takes an array of floating point values and sorts the array
    7. Function that takes a string and converts all 't's to 'z'
  14. (Tricky) Consider the following code:
    char *p = "abc", *q = "abc";
    if ( p == q )
    	printf( "The two strings have the same address!\n" );
    else
    	printf( "Addresses differ\n" );
    
    What happens - is it true or false?
    This can be platform-dependent on how string constants are stored!
  15. Write a function that takes a string as a parameter and reverse the characters in the string (function does not return anything). Write a function called swap to help you. Try writing it without using * pointers and write it with * pointers.
  16. T/F: Pointers, both as variables and function parameters, are used to store addresses.
  17. T/F: The expression * (ptr + 5) changes the address stored in ptr.
  18. T/F: The address stored in an array name cannot be changed by an assignment statement.
  19. T/F: The address of an integer variable can be stored in a float variable.
  20. T/F: The value of an integer variable can be stored in an int pointer variable.
  21. Which of the following data types pass arguments to functions by reference instead of by value?
    1. int
    2. float
    3. char
    4. char [ ]
    5. int [ ]
    6. struct
  22. Assume that P and Q are pointers of the same type, and that each has been assigned a value.
    What comparison would determine whether P and Q have targets with the same value?
                                                      4) All of them                               7) 2 and 3 only
          1) &P == &Q
                                                      5) 1 and 2 only                              8) None of these
          2) *P == *Q
                                                      6) 1 and 3 only
          3) P == Q
    
  23. Assume that P and Q are pointers of the same type, and that each has been assigned a value.
    What comparison would determine whether P and Q have the same target?
                                                      4) All of them                               7) 2 and 3 only
          1) &P == &Q
                                                                                                   8) None of these
                                                      5) 1 and 2 only
          2) *P == *Q
                                                      6) 1 and 3 only
          3) P == Q
    
  24. Consider the following prototype:
        function foo (const int * p);
    
    What restriction does the const keyword provide within the implementation of foo?
  25. Draw a picture of memory after these statements:
        int i = 42;
        int k = 80;
        int* p1;
        int* p2;
        p1 = &i;
        p2 = &k;
    
    # Suppose that we execute the statements from the previous question, and then we execute these statements:
        *p1 = *p2;
    
    Draw a picture of memory after this additional statement.
  26. Draw a picture of memory after these statements:
        int i = 42;
        int k = 80;
        int* p1;
        int* p2;
        p1 = &i;
        p2 = &k;
        p1 = p2;
    
  27. Consider the following statements:
        int *p;
        int i;
        int k;
        i = 42;
        k = i;
        p = &i;
    
    After these statements, which of the following statements will change the value of i to 75?
        * A. k = 75;
        * B. *k = 75;
        * C. p = 75;
        * D. *p = 75;
        * E. Two or more of the answers will change i to 75. 
    
  28. Suppose you have the following function prototype and variable declaration:
        void goop(int z[ ]);
        int x[10];
    
    Which is the correct way to call the goop function with x as the argument:
        * A. goop(x);
        * B. goop(x[ ]);
        * C. goop(x[10]);
        * D. goop(&x);
        * E. goop(&x[ ]); 
    
  29. Here is a function declaration:
        void goo(int* x)
        {
            *x = 1;
        }
    
    Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you print *a after the function call goo(a)?
        * A. 0
        * B. 1
        * C. address of a
        * D. address of x
        * E. None of the above 
    
  30.  The C program below (labelled PROGRAM) declares and initializes two identical structures. It
       calls one method to double all the fields in the first structure and a different method to double all
       the fields in the second structure. The purpose of this exercise is to compare the two approaches.
       When completed, the program prints:
                            thing1.x = 20, thing2.x = 20
                            thing1.y = 500, thing2.y = 500
          i. Which function (doubleItA or doubleItB) is more space and time efficient? Why?
         ii. Complete the program below by writing the doubleItA and doubleItB functions as well as
             their prototypes.
       PROGRAM:
       typedef struct {
           int x;
           int y;
       } Thing;
       // Function prototypes
       int main(){
           Thing thing1, thing2;
           thing1.x = thing2.x = 10;
           thing1.y = thing2.y = 250;
           thing1 = doubleItA(thing1);
           doubleItB(&thing2);
           printf("thing1.x = %d, thing2.x = %d\n", thing1.x, thing2.x);
           printf("thing1.y = %d, thing2.y = %d\n", thing1.y, thing2.y);
       }
    
  31. Which symbol is used to declare a pointer?
  32. Which symbol is used to reference a pointer?
  33. True or False: A pointer is a variable that points to a memory address.
  34. When specifying command-line arguments, why is it declared char *argv[ ]? What is argv?
  35. An interview question: Write an implementation of strlen().
    (Solution: Given a char pointer, strlen() determines the number of chars in a string. The first thing that your strlen() implementation ought to do is to check your boundary conditions. Don't forget the case where the pointer you are given is pointing to an empty string. What about the case where the pointer is equal to NULL? This is a case where you should state your assumptions. In many implementations, the real strlen() doesn't check to see if the pointer is NULL, so passing a NULL pointer to strlen() would result in a segmentation fault. Making it clear to your interviewer that you are aware of both of these boundary conditions shows that you understand the problem and that you have thought about its solution carefully. Example 3 shows the correct solution. int strlen (char *ptr) { int i = 0; if (ptr == NULL) { /* the real strlen doesn't handle this */ /* case, but I do! I return -1 */ return (-1); } while (*ptr++) != '\0') { i++; } return i; })
  36. An interview question:
    Illustrate the use of * and & for pointers in C.
    (Solution: char c = 'a'; - contains a single character 'a' char* p = &c; - p holds the address of c char c2 = *p; - c2 is equivalent to 'a')