Description
Lab #2: Debugging using GDB II
[ This document is available on Canvas and course website https://www.comp.nus.edu.sg/~cs2100 ]
Name: ________________________________________ Student No.: ___________________
Lab Group: _____
Special Note for Users Using MacOS on Apple Silicon
The GDB debugger is unfortunately still unavailable for users of MacOS on Apple Silicon (M1/M2 based MacBooks, for example). If you are using MacOS on Apple Silicon, there are two main choices for you:
https://lldb.llvm.org/use/tutorial.html
C Arrays
Array is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1β¦ and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], β¦, numbers[99] to represent individual variables. A specific element in an array is accessed by an index which starts from 0.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
C Functions and Arrays
In C programming, a single array element or an entire array can be passed to a function. A single value will be passed by value, whereas when passing the whole array, it is always passed as a pointer to the first element of the array.
Objective:
You will learn how to use arrays and functions in C.
Preparation (before the lab):
Please refer to lab#1 if you have not yet installed gdb on your system.
Procedure:
1. Retrieve the lab2a.c and lab2b.c programs.
2. Compile lab2a.c with gcc using the following command: gcc βo lab2a lab2a.c
3. What is the output of the program? Can you change it to β2β?
Note: The output should be related to the ageArray such as an element in ageArray.
4. What is the purpose of the operator sizeof? What datatype will sizeof always give β1β value for on all architectures?
5. Can you get the number of elements in ageArray? To produce the following output:
2
Size of the array is 4
Modify the main function, write it below and show the output to your labTA.
Note: The output β2β and size of array (i.e., 4 (four)) should be related to ageArray such as an element in ageArray and the number of elements in ageArray.
6. Compile lab2b.c with gcc using the following command: gcc βo lab2b lab2b.c
7. Can you give 2 ways of displaying the stored value and address value of the first element of an array?
8. Can you define the function hexToDecimal(char hex[], size_t size) in the lab2b.c using pointers to traverse the array? Write your function below and show your labTA the output.
Note: You are not allowed to use strtoul, strtol, or other functions from stdlib.h. Hint: Reading the hexadecimal numbers backwards might be easier. Furthermore, you are already given the function hexVal(char hex) to simplify your work.
9. Why do we pass the size of the array to the hexToDecimal function in lab2b.c? Can we calculate the size of the array inside the function?
10. What is the format specifier to print a variable of datatype size_t?
Marking Scheme: Report β 5 marks; correct output β 5 marks; Total: 10 marks.
Program lab2a.c
#include <stdio.h>
void display(int);
int main() {
int ageArray[] = { 2, 15, 4, 23 }; display(ageArray[2]); return 0;
}
void display(int age) { printf(β%d β, age);
}
Program lab2b.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int hexToDecimal(char[], size_t); int hexVal(char);
int main(void) {
// As a basic requirement, translate just the first two-digit // hex number. As an extra exercise translate all digits.
char hex[10];
size_t len;
printf(βEnter up to 8 hexadecimal digits (e.g. 091A2C, etc): β); fgets(hex, 10, stdin);
len = strlen(hex);
/* End-of-Line Check */ if(hex[len-1] == β β) { len = len β 1;
hex[len] = β;
}
printf(βYou entered: %s β, hex);
printf(βThe value in decimal is: %d β, hexToDecimal(hex, len));
return 0;
} int hexVal(char hex) {
switch(toupper(hex)) { case β0β: return 0; case β1β: return 1; case β2β: return 2; case β3β: return 3; case β4β: return 4; case β5β: return 5; case β6β: return 6; case β7β: return 7; case β8β: return 8; case β9β: return 9; case βAβ: return 10; case βBβ: return 11; case βCβ: return 12; case βDβ: return 13; case βEβ: return 14; case βFβ: return 15;
}
return 0;
}
int hexToDecimal(char hex[], size_t size) {
// complete the function body
return 0;
}
Reviews
There are no reviews yet.