Sunteți pe pagina 1din 5

CS230 – Spring 2018 -- Lab 4

Due to CS230 Blackboard before 11 pm on 02/14 (Wed.)


In Lab4, you will learn how to:
 Use typedef to define data type byte_pointer as a pointer to an object of type “unsigned char.”
 Use sizeof () to determine the number of bytes used;
 Use size_t;
 Use the editor vi to create and edit C code in Linux system

I. Lab4 Description: Write 5 procedures: show_int (), show_float (), show_long(), show_pointer ( ), and
show_bytes () by using the C code from your Textbook Page 45 (Figure 2.4)- See below:

Requirement 1: Your Lab 4 will print the byte representations of C objects of types: int, long, float, and void *,
respectively. Observe that they simply pass show_bytes a pointer &x to their argument x, casting the pointer to
be of type “unsigned char *.” This cast indicates to the compiler that the program should consider the pointer to
be to a sequence of bytes rather than to an object of the original data type. This pointer will then be to the lowest
byte address occupied by the object (little endian machine like our Linux).
Requirement 2: You need to modify show_int (), show_float (), show_long(), show_pointer ( ), and show_bytes
() procedures’ code (see above Figure 2.4) in order to print the variable byte representation and their memory
addresses like below format (attention: your byte representation is different from my machine) :

show_int() function displays the variable byte representation and their memory addresses:
int x memory address is 0x7ffcc829369c
0x7ffcc829369c 0xee
0x7ffcc829369d 0x0a
0x7ffcc829369e 0x00
0x7ffcc829369f 0x00

show_float() function displays the variable byte representation and their memory addresses:
float x memory address is 0x7ffcc829369c
0x7ffcc829369c 0x00
0x7ffcc829369d 0xe0
0x7ffcc829369e 0x2e
0x7ffcc829369f 0x45

show_long() function displays the variable byte representation and their memory addresses:
long x memory address is 0x7ffcc8293698
0x7ffcc8293698 0xee
0x7ffcc8293699 0x0a
0x7ffcc829369a 0x00
0x7ffcc829369b 0x00
0x7ffcc829369c 0x00
0x7ffcc829369d 0x00
0x7ffcc829369e 0x00
0x7ffcc829369f 0x00

show_pointer() function displays the byte representation and their memory addresses:
0x7ffcc8293698 0xc0
0x7ffcc8293699 0x36
0x7ffcc829369a 0x29
0x7ffcc829369b 0xc8
0x7ffcc829369c 0xfc
0x7ffcc829369d 0x7f
0x7ffcc829369e 0x00
0x7ffcc829369f 0x00

II. How to start your Lab4:


Step 1: Long into your Linux account (csmadison.dhcp.bsu.edu);
Step 2: Create a directory called Lab4;
Step 4: Change into Lab4 directory and use vi editor to create Lab4_First_LastName.c source file
Step 5: Create the main procedure by using the below C code:

//CS230 Lab4
//Programmer Name: xxxxx
//Date Completion: xxxx
//Add your Lab4 code by modifying above Figure 2.4 C code at the below:

Step 6: $gcc Lab4_First_LastName.c –o Lab4.exe Hint: to compile your Lab4_First_LastName.c


source code

Step 7: $ ./Lab4.exe Hint: to run the result and you must produce the
similar outputs like below:

Step 8: if your Lab4 works, it should produce the below outputs (attention: your byte representation is different
from my machine):
ival variable's memory address is 0x7fffd99424c0
ival variable's int value is 2798
ival variable's HEX value is aee

fval variable's memory address is 0x7fffd99424c4


fval variable's float value is 2798.000000
fval variable's HEX value=0x452ee000

lval variable's memory address is 0x7fffd99424c8


lval variable's long int value is 2798
lval variable's HEX value=0xaee

show_int() function displays variable byte representation and their memory addresses:
int x memory address is 0x7fffd994249c
0x7fffd994249c 0xee
0x7fffd994249d 0x0a
0x7fffd994249e 0x00
0x7fffd994249f 0x00

show_float() function displays variable byte representation and their memory addresses:
float x memory address is 0x7fffd994249c
0x7fffd994249c 0x00
0x7fffd994249d 0xe0
0x7fffd994249e 0x2e
0x7fffd994249f 0x45

show_long() function displays variable byte representation and their memory addresses:
long x memory address is 0x7fffd9942498
0x7fffd9942498 0xee
0x7fffd9942499 0x0a
0x7fffd994249a 0x00
0x7fffd994249b 0x00
0x7fffd994249c 0x00
0x7fffd994249d 0x00
0x7fffd994249e 0x00
0x7fffd994249f 0x00

show_pointer() function displays byte representation and their memory addresses:


0x7fffd9942498 0xc0
0x7fffd9942499 0x24
0x7fffd994249a 0x94
0x7fffd994249b 0xd9
0x7fffd994249c 0xff
0x7fffd994249d 0x7f
0x7fffd994249e 0x00
0x7fffd994249f 0x00

Step 9: Attention: you will receive zero credit if your Lab4_First_LastName.c would not compile in our Linux
system.

Step 10: How to submit your Lab4_First_LastName.c into CS230 Blackboard


1. Download your Lab4_First_LastName.c from csmadison Linux into your laptop Desktop by following the
below steps (A, B and C):

A. In your current working directory (username@csmadison:~$) in Linux:


cp ./Lab4/ Lab4_First_LastName.c public_html/

B. Go to Network in your PC, map network drive to the below:


\\csmadison.dhcp.bsu.edu\username

C. Move Lab4_First_LastName.c from csmadision into your Desktop

2. Access to CS230Blackboard, click “Lab Submission” link, double click “Lab4” icon, click “Browse My
Computer” button, select Lab4_First_LastName.c file from your Desktop, and click “Submit” button. It means
that you will upload Lab4_First_LastName.c file into the BSU Blackboard. Attention, when you click “Submit”
button, you need to make sure that you have uploaded Lab4_First_LastName.c file.

Thanks!

Dr. Zhang

S-ar putea să vă placă și