Lab3 - LAB 3 C PROGRAMMING TECHNIQUES: POINTERS, BITWISE...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: LAB 3 C PROGRAMMING TECHNIQUES: POINTERS, BITWISE OPERATORS AND MORE 3.1. LAB OBJECTIVE The objective of this lab is to develop more advanced C programming skills. Specifically, the objectives include More programming techniques Pointers Bitwise operators Debugging tools 3.2. BACKGROUND This lab covers pointers, bitwise operators, and debugging tools as well as some commonly used programming commands. By the end of this lab you should be reasonably well equipped for some serious C programming. Remember to go back to Chapter 17 of Bolton and/or the online C tutorial for additional details if necessary, and don’t forget about the online help that can be obtained while programming in Developer Studio. By the end of this lab, you should be familiar with all of the following sections of the online tutorial: Introduction Declaring Variables Preprocessor Statements Arithmetic Operators Programming Style Keyboard Input Relational Operators For and while loops If, if else, relational operators Conditional expression operator Functions Formatters for printf() and scanf(), bit operations Stuctures Pointers 3.2.1. Pointers A pointer is a variable that represents the location or address (rather than the value) of a data item, such as a variable or an array element. Refer to Section 17.5 (p. 396) in the text and/or the section on pointers in the online tutorial for more details about pointers. #include <stdio.h> main() { int u = 5; int* pu; pu = &u; /* declaration of integer variable u */ /* decl. of pointer to an integer variable /* assigning the address of variable u to the pointer variable */ printf(“\npu = 0x%x, &u = 0x%x\n”, pu, &u); printf(“\n(*pu) = %d, u = %d”\n, *pu, u); } Note: The name of an array is itself a pointer to its first element. For example, for an array declared by the statement “int A[10];”, the address of the first element of A can be obtained by either “A” or “&A[0]”. Pointers can be powerful C tools. There are several important applications where it is useful to use pointer variables, such as to pass multiple data items to and from a function as an alternate way to access individual array elements to define multi-dimensional arrays for dynamic allocation of memory when the array size is not known beforehand 3.2.2. Passing Values to Functions There are two ways in which data can be passed to a function: By value: Here the value of the variable is passed to the function. By reference: Here the address of the variable is passed to the function. Note that the subroutine call and the subroutine must know how the variable is being sent, so when using library subroutines one must know beforehand whether the subroutine expects the value of the variable or the address of the variable. (Remember the difference in how arguments are passed to scanf and printf?) The following example illustrates the two methods of passing variables. #include <stdio.h> void fun1(int u) { u = 0; printf(“\nWithin fun1: u = %d”, u); return; } void fun2(int *pu) { *pu = 0; printf(“\nWithin fun2: u = %d”, *pu); return; } int u = 3; printf(“\nBefore calling fun1: u = %d”, u); fun1(u); /* the value of u is passed to the function*/ printf(“\nAfter calling fun1: u = %d”, u); u = 4; printf(“\nBefore calling fun2: u = %d”, u); fun2(&u); /* the address of the variable is passed */ printf(“\nAfter calling fun2: u = %d”, u); } 3.2.3. Bitwise Operators Bitwise operation Symbol AND & OR | EXCLUSIVE-OR ^ NOT ~ Shift right >> Shift left << These operators are frequently used for manipulating data. For situations in which these operators are useful refer to Section 17.7.1 (Example programs, p. 398) in the textbook. We will be using these operators while reading encoder signals, so they will be useful later. Also take a look at the section entitled “Formatters for printf() and scanf(), bit operations” in the online tutorial. “<<” Left shift operator: Assume A is a short int, i.e. a 16bit integer. A = 0xa234 = 1010 0010 0011 0100 A<<4 = 0x2340 = 0010 0011 0100 0000 A<<2 = 0x88d0 = 1000 1000 1101 0000 Vacancies in the right most bits are filled with zeroes. “>>” Right shift operator: A = 0xa234 = 1010 0010 0011 0100 A>>4 = 0x0a23 = 0000 1010 0010 0011 “&” AND Operator: A = 0xa2; B = 0x34; C = A&B; A= B 1010 0010 0011 0100 = = 0xa2 0x34 0010 0000 = 0x20 “|” OR operator: A= 1010 0010 = 0xa2 B 0011 0100 = 0x34 1011 0110 = 0xb6 = A&B = = A|B = 3.3. PRELAB Write C programs for the following: 1. Modify the sorting program you wrote in Lab 1 so that it is modular. Write a function called sort that accepts an integer array, sorts it, and returns the sorted array to the main function. Call this function from the main program. In addition, write a subroutine called print_array that accepts an integer array and prints it out in this format: The array is [0 1 2 3 4 5 6 7 8 9] Use this function to print out the array before and after sorting. 2. Write a subroutine called bit_merge, that accepts two four digit hexadecimal numbers (16 bits each) as input and combines their bits into a single 32 bit number as follows void main() { unsigned short hn1 = unsigned short hn2 = int result; 0x1234; 0xabcd; result = bit_merge(hn1,hn2); printf(“\nmerging 0x%x and 0x%x results hn1, hn2, result); } The output should look like: merging 0x1234 and 0xabcd results in 0xabcd1234 To do this requires a left shift of hn2 by 16 bits and then a bitwise OR of hn1 with the left-shifted hn2. Remember that hn2 needs to be left shifted into a 32 bit variable, or all the data will be lost when shifted. Be careful of data types (int versus unsigned short), and think about what is going on at a bit level. 3. Write a function that accepts a decimal number between 0 and 255 and prints it out on the screen using 8 binary digits for representation. 4. Read Section 21.3 (Parity and error coding checks, p. 489) from the book. Alternatively, look at the class website under “Reference Materials” and read the page on “Parity Method for Error Detection.” Assume that your data sequence has seven bits and an eighth bit is added to achieve even parity. Write a program that accepts an 8-bit word (or equivalently a 2-digit hex number) and does an even parity check on the input data. If the input data violates even parity, print out an error message. 3.4. LAB PROCEDURE Implement the modular sorting program. You should be able to see the advantage of pointers in this example. We need to pass ten data values to the sort function. This can be achieved simply by passing a single pointer variable to the function. This is one important advantage of using pointers. Note: To open a project that you have previously created, choose FILE -> OPEN WORKSPACE, choose “projectname.dsw,” then click OK. Alternatively, you can open the workspace by double clicking on the “projecctname.dsw” file. Always open the workspace file before opening the C file. Implement the programs you wrote for Questions 1, 2, 3 and 4 in the prelab. Show your working programs to the TA. 3.5. POSTLAB AND LAB REPORT The lab report should contain all the items asked for in Section 3.4. Print out your source code files with full comments for questions 1, 2, 3 and 4. ...
View Full Document

This note was uploaded on 02/07/2012 for the course ME 4231 taught by Professor Staff during the Fall '08 term at Minnesota.

Ask a homework question - tutors are online