《大連東軟信息學(xué)院大一上 電子工程 c語言-實(shí)驗(yàn)第六次,第十二周》由會員分享,可在線閱讀,更多相關(guān)《大連東軟信息學(xué)院大一上 電子工程 c語言-實(shí)驗(yàn)第六次,第十二周(7頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、Lab7 Pointers
OBJECTIVES
After completing this experiment, you will be able to:
? Understand pointers fundamentals
? Understand functions and pointers
? Pass arrays to functions
? Use pointers to One-Dimensional Arrays
PROCEDURES
1. What format speci?ers (in order) should be used in the
2、printf() statement in the
following program? Note that in the program the correct format speci?ers have been
replaced by Z.
#include
int main(void)
{
int x = 5;
int *x_ptr = &x;
printf("%Z, %Z, %Z, %Z\n", x, *x_ptr, &x, x_ptr);
}
Your answers:(2 Marks)
%d,%d,%p,%p
—————————————
3、—————
Print Screen of Result: (3 Marks)
2. On a machine in which addresses are 2 bytes, what is printed by the following program:
#include
int main(void)
char fun[] = "Programming is fun.";
char favorite[] = "My favorite class is programming.";
char *x = fun;
printf("%d\n
4、", sizeof(fun));
printf("%d\n", sizeof(favorite));
printf("%d\n", sizeof(x));
}
Your answers:(2 Marks)
20
34
2
——————————————————
Print Screen of Result: (3 Marks)
3. Rewrite the following program, replacing the subscript (index) notation with the corresponding pointer notation. Only c
5、hange the parts of the program necessary for the replacement.
#include
int main(void)
{
int data[] = {1, 2, 3, 4, 5, 6};
int i;
for(i = 0; i < 6; i++)
printf("%2d", data[i]);
}
Print Screen of Code: (6 Marks)
——————————————————
Print Screen of Result: (3 Marks)
————————————
6、——————
4. Rewrite the following program such that the function has a return type of void and the variable y gets its value using pointers.
#include
int dbl(int num);
int main(void)
{
int x = 13;
x = dbl(x);
printf("x doubled is %d\n", x);
}
int dbl(int num)
{
return 2*num;
7、
}
Print Screen of Code: (6 Marks)
——————————————————
Print Screen of Result: (3 Marks)
——————————————————
5. Write a program that uses pointers to pass a character array A to a function. The function will change all characters to uppercase,Please write different two programs in the followi
8、ng cases:
(1)The return type of the function should be pointer of char type.
(2)The return type of the function should be void.
The elements of the array should be printed from main() after the function call.
Compare the differences between two program.
(1)Print Screen of Code: (6 Marks)
9、
——————————————————
Print Screen of Result: (3 Marks)
(2)Print Screen of Code:
Print Screen of Result:
Describe the differences between the two programs: (3 Marks)
First program ,the type of return value is points,it pass by reference and use return points
10、value. Second program the type of return value is void.it pass by value.
——————————————————
6. Write a program that declares a 1D array of integers. The program calls a function, passing it the array, the number of elements in the array, and the addresses of two variables, evensum and o
11、ddsum, for storing integers. The function produces two sums: the sum of the even integers in the array data and the sum of the odd integers in the array data. Pointers are used to update the values of evensum and oddsum declared in main().
Print Screen of Code: (6 Marks)
——————————————————
Print Screen of Result: (3 Marks)
——————————————————