# 03 by cuiliqing

VIEWS: 0 PAGES: 4

• pg 1
```									Data and Communications Networks (219234)                                                 Kasetsart University
st
1 Semester 2009                                                           Software and Knowledge Engineering

In-class exercise 3
Points: ____/_____

Name(s):____________________________________________________________________ Date:__________________

Instructions: You will do this exercise in pairs. Turn this exercise in at the end of class. Only one copy per
team must be turned in. All the questions pertain to the slides on “More Pointers”.

Example 2: (pass by value/reference)

#include <stdio.h>

void Sum_1(int a, int *b);

int main(){

int *x,*y;
int p = 5;
int q = 6;

x = &p;
y = &q;

printf("Before: q is %i \n",q);
printf("Before: y is %u \n",y);

Sum_1(p, y);

printf("After: q is %i \n",q);
printf("After: y is %u \n",y);

return 0;

}

void Sum_1(int a, int *b){

int input;
printf(“Enter an integer:”);
scanf("%d", &input);
*b = a + input;

}

What is the output of this program if the input integer is 15?
Data and Communications Networks (219234)                                             Kasetsart University
st
1 Semester 2009                                                       Software and Knowledge Engineering

Example 3: Passing a struct as a parameter to a function

#include <stdio.h>
struct student {
int age;
float gpa;                                                void print_student_age_1(Student any){
};                                                                printf("Age is %d\n", any.age);
any.gpa = 2.0;
typedef struct student Student;                               }

void print_student_age_1(Student any);
void print_student_age_2(Student * any);
void print_student_age_3(const Student * any);                void print_student_age_2(Student * any){
printf("Age is %d\n", any->age);
int main(){                                                       any->gpa = 2.0;
}
Student john;
john.age = 20;
void print_student_age_3(const Student * any){
john.gpa = 3.5;
printf("Age is %d\n", any->age);
print_student_age_1(john);                                    any->gpa = 2.0;
printf("gpa is %.1f\n", john.gpa);                        }

Student * john_ptr = &john;
print_student_age_2(john_ptr);
printf("gpa is %.1f\n", john.gpa);

return 0;
}

a. Assume that there are no errors in the program, determine the output of this program?

b. There is in fact an error related to function print_student_age_3. Identify it. Would it be a
compile-time error or a run-time error? Show how you would fix it.
Data and Communications Networks (219234)                                             Kasetsart University
st
1 Semester 2009                                                       Software and Knowledge Engineering

Example 4: malloc and structs

a. Fill in the blanks with the correct answers:

#include <stdio.h>
#include <stdlib.h>                                   void print_student_info(Student class[ ], int count){
int i = 0;
struct student {                                          for(i = 0; i < count ; i++){
int age;                                                   print_one_student_1( _____________ );
float gpa;                                            }
};                                                        for(i = 0; i < count ; i++){
typedef struct student Student;                                print_one_student_2(( _______________ );
}
void print_student_info(Student class[ ], int count); }
void print_one_student_1(Student student);
void print_one_student_2(Student * student);

int main(){

Student * many =                                  void print_one_student_1(Student student){
(Student *)malloc(3 * sizeof(Student));             printf(" age is %d gpa is %f \n",_______________________________);
many[0].age = 23;                                 }
many[0].gpa = 3.4;
many[1].age = 22;
many[1].gpa = 3.6;
many[2].age = 21;
many[2].gpa = 4.0;

print_student_info(___________________,3);        void print_one_student_2(Student * student){
}                                                        printf(" age is %d gpa is %f \n", ________________________________);

}

b. Show(draw) what happens when the function print_student_info is called (i.e. when you enter the
function, before the “int i=0” statement). Assume that the following memory allocations have occurred.
Data and Communications Networks (219234)                                          Kasetsart University
st
1 Semester 2009                                                    Software and Knowledge Engineering

c. Show (draw) what happens when the function print_one_student_1 is called when i=0(i.e. when you
enter the function, before the “printf” statement). Assume that the following memory allocations have
occurred and anything else you determined when answering “b”.

d. Show (draw) what happens when the function print_one_student_2 is called when i=1(i.e. when you
enter the function, before the “printf” statement). Assume that the following memory allocations have
occurred and anything else you determined when answering “b”.

```
To top