haihongyuan.com

# Exam12_C作业与实验12

1．选择题

2. (1) 9,20,30\$

(2) Max=4, 4, 3

(3) Max=4, 3, 4

(4) Max=4, 3, 4

(5) 5f00 7 5 15 24328

1．选择题

(1) 下列程序段的输出结果是。

int m = 1, n = 2, *ptr1 = &m, *ptr2 = &n;

ptr1 = ptr2;

ptr2 = &m;

printf(“%d %d %d ”, *ptr1, *ptr2, *ptr1 - *ptr2);

A. 1 2 3 B. 1 2 4 C. 2 1 3 D. 2 1 1

(2) 设有定义语句int x, *p = &x, *p1; 下列表达式中错误的是。

A. p = 0xffff B. *p = 0xffff C. x = 0xffff D. p1 = &x

(3) 设有定义语句int x, y, *p1 = &x, *p2 = &y; 下列表达式中哪一个是不合适或错误的 。

A. p1 =(int *)123 B. *p1 = 123 C. p1 - p2 D. p2 = *&x

(4) 如果n和m都是int型，而p和q是指向int型的指针变量，下列表达式哪一个是不合适或错误的 。

A. p = &n; *q = &m; B. p = &*&n; n = (*&)m;

C. n = (int)p; n = *&*&m; D. p = &q; n = (*p)++ + *q;

2. 写出下列程序段的输出结果

(1)

#include <stdio.h>

int c;

void change(int *a, int b)

{

--*a;

b++;

c=c + 1;

}

main()

{

int a = 10, b = 20, c = 30;

change(&a, b);

printf(“%d,%d,%d\$”, a, b, c);

}

(2)

#include <stdio.h>

int Max(int a, int b)

{

int temp;

if(a < b){

temp = a;

a = b;

b = temp;

}

return a;

}

main()

{

int x = 3, y = 4, max = 0;

max = Max(x, y);

printf(“Max=%d, %d, %d”, max, y, x); }

#include <stdio.h>

int Max(int *a, int *b)

{

int temp;

if(*a < *b){

temp = *a;

*a = *b;

*b = temp;

}

return *a;

}

main()

{

int x = 3, y = 4, max = 0;

max = Max(&x, &y);

printf(“Max=%d, %d, %d”, max, y, x); }

(3)

(4)

#include <stdio.h>

int Max(int *a, int *b)

{

int *temp;

if(*a < *b){

temp = a;

a = b;

b = temp;

}

return *a;

}

main()

{

int x = 3, y = 4, *p1 = &x, *p2 = &y, max = 0;

max = Max(p1, p2);

printf(“Max=%d, %d, %d”, max, *p1, *p2);

}

(5)假定x的地址是0x5f00。

unsigned short x=5, *p = &x;

printf("%p %u %u %u %u\n", p, *p+2, **&p, 3**p, p+4);