 #### 联系方式

• QQ：99515681
• 邮箱：99515681@qq.com
• 工作时间：8:00-23:00
• 微信：codehelp #### 您当前位置：首页 >> Java程序Java程序

###### 日期：2020-11-24 08:58

BIT 1400 Tutorial 9:
The Sizeof Arrays
This tutorial will examine arrays, memory and the sizeof operator.
For task 1 you will make a couple dynamic and static arrays, use the sizeof operator and print out the
sizeof values you get.
1. Make a static array of ints: int a_staticNums; What is sizeof(a_staticNums) ? Why?
2. What is sizeof(a_staticNums); ? Why?
3. Make a dynamic array of 20 characters char* a_dynChars using malloc.
4. Make a dynamic array of 40 integers int* a_dynNums using calloc.
5. What is sizeof(a_dynChars) ? What is sizeof(a_dynChars) ? Notice the values are not 20?
6. What is sizeof(a_ dynNums) ? What is sizeof(a_ dynNums ) ?
7. What is sizeof(char*) ? sizeof(int*)?
8. What does sizeof calculate? Can you use it to determine the size of an array? (short answer: no)
9. Write a function that prints (printf) the contents of any array of integers….besides the array of
integers, what other parameter does the function need? Why do we NOT need that function for
a character string? Is there an equivalent to ‘\0’ for integers? (short answer: no)
10. Set the 10th element of a_dynNums to be 41 without using the square parentheses ([]). This will
require pointer arithmetic.
11. Make sure you call free on a_dynChars and a_dynNums when you are finished with the arrays.
Hopefully it is now clear why you can’t use sizeof to determine the size of the array. C is so old that you
need to pass the array size into your functions too.
You will now write 3 different functions, all of which use a student struct
1. Create a “student” struct which contains a char array name which has 50 elements, an unsigned
int ID and an unsigned int age (so 3 variables in the struct).
2. In main, create a student struct named testStudent and set the values to whatever you like.
Recall that for the name you should use the strcpy function to set the name.
3. Make a new function named peasantEdit which takes a student struct s and returns void. Your
job is to edit s in the function.
a. Call peasantEdit(testStudent) and confirm testStudent hasn’t been changed despite
what you did in the function. A printf statement will show testStudent didn’t change.
b. peasantEdit makes a new struct and copies the values from testStudent. Hence
testStudent doesn’t change.
4. Write a new function named ninjaEdit which takes a student struct pointer ps (student* ps). Edit
the values of the struct ps points to (ps->ID = 1234; would change the ID for example.
a. Call ninjaEdit(testStudent) and confirm testStudent DOES change.
5. Extra Challenge: This is not C code (it’s C++) but you can “pass by reference” to change a
variable without using a pointer. Write a function ninjaEdit2 that takes a student struct
reference: void ninjaEdit2(student& s). Change some values in s. For example s.ID = 1234;
a. Call ninjaEdit2(testStudent) and confirm testStudent has changed yet again.
b. Notice that pass by reference doesn’t hint to the person calling the function that their
variable could be changed by the function?? That’s the danger of pass by reference.
HINT:
const int MAX_NAME_LEN = 50;
struct student
{
char name[MAX_NAME_LEN];
//………………..
};
// Passing in a student actually makes a copy of the structure
// thus the original is unchanged
void peasantEdit(student s);
// Passing in a pointer means you can edit the original structure
void ninjaEdit(student* s);
// Pass by reference means you edit the original structure...and thus this
// approach should be done with caution.
void ninjaEdit2(student& s);
Task 3 (if you have time)
? Make an array of floats, make a dynamic array of floats, and make an array of float pointers. All
3 arrays should be the same size. Use sizeof for each. Write the sizes as a COMMENT your code.
float a_staticF = { 1.0f, 2.3f, 2.0f, 4.0f, 17.0f,
-23.9f, -8.0f, 0.0f, 927.4f, 0.0f };
? Make each float in your dynamic array equal the value in the static array.
? Make each pointer in your float pointer array point to values in your dynamic float array.
? Print the values at each index for each array. For the array of float pointers, output the value
of the pointee.
? Change several random values in the static and dynamic float arrays.
? Print the values of each index for each array again. What values changed?
? Make sure you free the two dynamic arrays.