COMP2421 Computer Organization 2019/20 Semester 1
Lab 2 C-Language Revisited
In this lab, we will review some basic concepts in C-language, namely “array” and “pointer”. To run C program in the lab, you may use Dev-C++ which is located in Y:\Apps\Dev-Cpp. Array
An array in C is a variable that holds multiple elements which shares the same data type. In addition, array is a kind of data structure that can store a fixed-size sequential collection of elements of the same type.
Copyright By PowCoder代写 加微信 powcoder
Declaring and initialize an Array
Syntax: type arrayName[arraySize]
Examples: int num[5]; // declaration
int num[5] = {91, 8, 13, 6, 1}; // declaration and initialization int num[] = {91, 8, 13, 6, 1}; // declaration and initialization
Accessing array elements
Note that the index of an array starts from 0, i.e., the first element of the num array is num[0]. The last element is num[size – 1]where size is the number of elements in the num array, i.e., num[4].
The following program shows how to access elements of an array.
#include
int main() {
int num[5] = {91, 8, 13, 6, 1};
for(i = 0; i < 5; i++) {
printf("num[%d] = %d\n", i, num[i]);
COMP2421 Computer Organization 2019/20 Semester 1
A pointer is a special kind of variable. Pointers are designed for storing memory address, i.e., the address of another variable. Declaring a pointer is similar to declaring a normal variable, except that there is an asterisk before the variable identifier.
➢ There are two operators you need to know to work with pointers. The “address of” operator, ‘&’, and the “dereferencing” operator, ‘*’. Both are prefix unary operators.
➢ When you place an ampersand in front of a variable, you will get its address. It can be stored in a pointer variable.
➢ When you place an asterisk in front of a pointer, you will get the value stored in the memory location, with an address stored in that pointer variable.
Read the following example:
#include
int main(void) {
int my_variable = 6, other_variable = 10;
int *my_pointer;
printf(“the address of my_variable is : %p\n”, &my_variable);
printf(“the address of other_variable is : %p\n”, &other_variable);
my_pointer = &my_variable;
printf(“\nafter \”my_pointer = &my_variable\”:\n”);
printf(“\tthe value of my_pointer is %p\n”, my_pointer);
printf(“\tthe value at that address is %d\n”, *my_pointer);
my_pointer = &other_variable;
printf(“\nafter \”my_pointer = &other_variable\”:\n”);
printf(“\tthe value of my_pointer is %p\n”, my_pointer);
printf(“\tthe value at that address is %d\n”, *my_pointer);
[Exercise: Run the program in Dev-C++ and read the output.] [Question: What is the size of an address?]
COMP2421 Computer Organization 2019/20 Semester 1 More explanation
Variable x is address of an int
To assign address of y to x (& is the “address operator”)
To assign value at address in x to z (* is the “dereference operator”)
[Question: Read the following code fragment. What is the value of p?]
int *p, x;
[Question: What would happen if the following statement was added to the end of the code fragment?]
Passing variables to functions
Read the following example:
#include
/* function declaration */
void swap1(int num1, int num2);
void swap2(int *num1, int *num2);
int main () {
int x = 100;
int y = 200;
printf(“Before swap, value of x: %d, y: %d\n”, x, y);
/* Calling swap1 function to swap the values. */
swap1(x, y);
printf(“After swap1, value of x: %d, y: %d\n”, x, y);
Calling swap2 function to swap the values.
&x indicates pointer to x, i.e., address of variable x and
&y indicates pointer to y, i.e., address of variable y.
COMP2421 Computer Organization 2019/20 Semester 1
swap2(&x, &y);
printf(“After swap2, value of x: %d, y: %d\n”, x, y);
/* function definition */
void swap1 (int num1, int num2) {
temp = num1; // save the value of num1
num1 = num2; // put num2 into num1
num2 = temp; // put temp into num2
void swap2 (int *num1, int *num2) {
temp = *num1; // save the value at address num1
*num1 = *num2; // put the value of stored in the address of num2
// into the destination of num1
*num2 = temp; // put temp into the destination of num2
[Question: Why do the functions swap1 and swap2 have different behaviours?] Hint: What are call-by-value and call-by-pointer in function call?
Passing arrays to functions
void myFunction(int *arr) {
Read the following example:
void myFunction(int arr[5]) {
void myFunction(int arr[]) {
#include
/* function declaration */
double getAverage(int arr[], int size);
int main() {
/* an int array with 5 elements */
int num[5] = {91, 18, 25, 17, 50};
double avg;
/* pass a pointer to the array as an argument */
avg = getAverage(num, 5) ;
/* output the returned value */
printf(“Average value is: %f “, avg);
COMP2421 Computer Organization 2019/20 Semester 1
double getAverage(int arr[], int size) {
double avg;
double sum = 0;
for (i = 0; i < size; ++i) {
sum += arr[i];
avg = sum / size;
return avg;
Returning an array from a function
#include
/* function to generate and return random numbers */
int* getRandom() {
static int r[10];
/* set the seed */
srand((unsigned)time(NULL));
for (i = 0; i < 10; ++i) {
r[i] = rand();
printf("r[%d] = %d\n", i, r[i]);
/* main function to call above defined function */
int main() {
/* a pointer to an int */
p = getRandom();
for (i = 0; i < 10; i++) {
printf("*(p + %d): %d\n", i, *(p + i));
Note: C does not recommend returning an address of a local variable. To allow accessing the array data “safely” outside the function, we declare the local array as static.
Pointer to an array
Indeed, an array name is a constant pointer to the first element of the array. For example, int arr[5]; here arr is a pointer to &arr[0], which is the address of the first element of the array arr. Below, we have a code fragment that assigns ptr as the address of the first element of arr.
int arr[5];
COMP2421 Computer Organization 2019/20 Semester 1
The following may help illustrate a bit more:
#include
int main() {
/* an array with 5 elements */
int arr[5] = {89, 73, 105, 6, 47};
ptr = arr;
/* output each array element’s value */
printf(“\nArray values using pointer\n”);
for (i = 0; i < 5; i++) {
printf("*(ptr + %d): %d\n", i, *(ptr + i));
printf("\nArray values using arr as address\n");
for (i = 0; i < 5; i++) {
printf("*(arr + %d): %d\n", i, *(arr + i));
We assign the address of arr to the pointer ptr (ptr = arr;). Similar operations can be performed using these two variables. We try to access to different elements of the array by adjusting the addresses, e.g., *(arr + 2) accesses the data at arr[2].
Pointer to Pointer
It is a form of multiple indirection, or a chain of pointers. In general, a pointer contains an address of a variable. When we declare a pointer to pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value of a variable.
Pointer Pointer Variable
COMP2421 Computer Organization 2019/20 Semester 1
Read the following example:
#include
int main() {
int var = 3000;
int **pptr;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
printf(“Value of var = %d\n”, var);
printf(“Value available at *ptr = %d\n”, *ptr);
printf(“Value available at **pptr = %d\n”, **pptr);
[Question: What would be printed if the statement, “*(*pptr) = 5000;” is added before printing?]
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com