


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: C and Software Tools; Subject: Computer Science; University: North Carolina State University; Term: Spring 2007;
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Questions Q1 and Q2 carry 20 marks each, all other questions carry 10 marks each. This adds up to 80, but 75 is a perfect score. If you obtain more than 75, your marks will be added up to 75 and will be considered 100%. It should be possible to answer the questions in the space provided. Use the back of the paper where you are specifically asked to. Otherwise, do not use the back of the paper or attach extra sheets unless you absolutely have to. You may consult any books, notes, etc., but no other person. You must not use a cellphone, computer, PDA, or any other information processing device. This is a one hour test.
#include <stdio.h> #include <stdlib.h> #define F_SWITCH 2 int iG1[10] = {11,12,13,14,15,16,17,18,19,20}; int iG2[10] = {5,4,3,2,1}; void print_element (int *p) { printf ("First: %d\n", *p); printf ("Second: %d\n", p[3]); printf ("Third: %d\n", *p+4); printf ("Fourth: %d\n", *(p+4)); printf ("Fifth: %d\n", *(p+(&p[5]-(p+3)))); printf ("Sixth: %d\n", *(p+=7)); printf ("Seventh: %d\n", p[1]); printf ("Eighth: %d\n", p); printf ("Ninth: %d\n", &p[3]); } int main () { int *p_2_global; if (F_SWITCH == 1) { p_2_global = iG1; } else { p_2_global = iG2; } print_element (p_2_global); return (EXIT_SUCCESS); } Printouts:
Q3. Briefly describe what a pointer increment statement such as p++; achieves, where p is a pointer to some particular data type. (Use the facing page to write your answer if you need to .) It updates the variable p so that it is pointing to the next object after the one it was originally pointing to, where object is the type to which p was defined as pointing to ( not the next byte). Q4. Consider the following code: typedef struct struct1 { short countrycode; short areacode; short exchange; short number; } phonenumber; typedef struct struct2 { int height; int weight; phonenumber phone; } person; At some point in the source code, p is a pointer of type person. Write the pointer expression that returns the areacode part of the phone number of that person (assuming the memory for the data structure is currently allocated correctly). Avoid unnecessary de-referencing operators, and unnecessary parentheses. p->phone->areacode Q5. State in one or two sentences what the function tolower() is expected to do. Make sure you describe all cases of the input. (Use the facing page to write your answer if you need to .) See manual page of tolower; make sure you mention what happens when the argument is not an uppercase character. Q6. (a) Consider the code below. State with reasons whether it is possible to predict with certainty what will be printed out by the function func_1; and if it is possible, state what will be printed. (Use the facing page to write your answer.) void func_1 (int i, int j) { printf ("i is %d, j is %d\n", i, j); } /*... / / somewhere in the code, a call to func_1 / int i = 30; func_1 (i, i++); /... */ It is not possible ; the order of evaluation of function parameters is not fixed in C. (b) In a makefile, the following lines are found: some_func.o : some_func.c gcc - ansi - pedantic - Wall - c some_func.c Briefly describe in English what these lines communicate to the make utility. (Use the facing page to write your answer.) The target some_func.o depends on some_func.c (must be re-built if some_func.c was modified after the time this target was last built), and can be built by executing the command gcc - ansi