C程序设计基础及实验 习题11 复习

文章详细解释了C语言中关于函数返回指针、内存管理、单向链表、动态分配、结构嵌套、递归、宏定义、全局变量使用等方面的基础知识,以及涉及字符串操作、函数调用和数组链表的实例。
摘要由CSDN通过智能技术生成

判断题

1-1 如果函数的返回类型是指针,则可以返回函数内部任意变量的地址。F,不能返回局部变量的地址噢

1-2 如果函数的返回类型是指针,则可以返回0。T

1-3 在单向链表中,头指针中存放的是头结点的内容。F,是头指针的地址呀

1-4 单向链表中的每个结点都需要动态分配内存空间。T

1-5 通常使用结构的嵌套来定义单向链表结点的数据类型。T

1-6 用链表代替数组进行数据操作时,查询更加方便。F,链表查询很麻烦呢

单选题

2-1 要调用数学处理函数时,在#include命令行中应包含()。

A."stdio.h" B."string.h" C."math.h" D."ctype.h"

选择C,ctype.h提供了一些可用于测试和映射字符的函数。

2-2 对于以下递归函数f,调用f(4),其返回值为() 。

int f(int n)
{    if (n)  return f(n - 1) + n;
     else return n;
}

A.10 B.4 C.0 D.以上均不是

选择A,函数为求1+2+……+n的值

2-3 执行下列程序后变量i的值应为()。

       #define MA(x, y)  ( x*y )
       i = 5;
       i = MA(i, i + 1) – 7;

A.30 B.19 C.23 D.1

选择B,解析见习题10

2-4 宏定义“#define DIV(a, b) a/b”,经DIV(x + 5, y - 5) 引用,替换展开后是()。

A.x + 5 / y - 5 B.(x + 5 / y – 5) C.(x + 5) / (y - 5) D.(x + 5) / (y - 5);

选择A

2-5 执行下面程序,正确的输出是()。

int x = 5, y = 7;
void swap ( )
{
      int z ;

      z = x ;  x = y ;  y = z ;
}
int main(void)
{
      int x = 3, y = 8;
      swap ( ) ;
      printf ("%d,%d \n", x , y ) ;

      return 0 ;
}

A.3,8 B.8,3 C.5,7 D.7,5

选择A,解析见习题10

2-6 下面说法中正确的是()。

A.若全局变量仅在单个C文件中访问,则可以将这个变量修改为静态全局变量,以降低模块间的耦合度

B.若全局变量仅由单个函数访问,则可以将这个变量改为该函数的静态局部变量,以降低模块间的耦合度

C.设计和使用访问动态全局变量、静态全局变量、静态局部变量的函数时,需要考虑变量生命周期问题

D.静态全局变量使用过多,可那会导致动态存储区(堆栈)溢出

选择A,如果全局变量仅由单个函数访问,不存在耦合度的问题,B错误;当退出函数时,静态局部变量虽然存在,但不能使用,C错误;静态全局变量当然存储在静态存储区啊,D错误。

2-7 下面程序的运行结果是()。

#include<stdio.h>
int main(void)
{
      int x[5] = { 2, 4, 6, 8, 10 }, *p, **pp;

      p = x;
      pp = &p;
      printf("%d ", *(p++));  /* 数字后有一个空格 */
      printf("%d\n", **pp);

      return 0;
}

A.4 4 B.2 4 C.2 2 D.4 6

选择B,*(p++)先取*p再自加

2-8 对于以下变量定义,正确的赋值是()。

int *p[3], a[3];

A.p = a B.*p = a[0] C.p = &a[0] D.p[0] = &a[0]

选择D,p是指针数组,而a是整型数组,p不能直接赋值,A、C错误;p[0]应存储a[0]的地址,而*p = a[0]等价于p[0]=a[0],B错误

2-9 下列程序的输出是()。

#include<stdio.h>
int main(void)
{
      int i, a[12] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, *p[4];

      for (i = 0; i < 4; i++){
           p[i] = &a[i*3];
      }
      printf("%d\n", p[3][2]);

      return 0;
}

A.上述程序有错误 B.6 C.8 D.12

选择D,实质上p所指向一个由a变换而来的4*3二维数组

2-10 以下正确的说明语句是( )。

A.int *b[ ] = {1, 3, 5, 7, 9};

B.int a[5], *num[5] = {&a[0], &a[1], &a[2], &a[3], &a[4]};

C.

int a[ ] = {1, 3, 5, 7, 9}; 
int *num[5] = {a[0], a[1], a[2], a[3], a[4]};

D.

int a[3][4], (*num)[4]; 
num[1] = &a[1][3];

选择B, 指针必须是指向一个可控的已知内存地址,所以A错误;num是指针数组,需要对a取地址,C错误;num的长度为4,只能取a每一行首位元素的地址,D错误。

2-11 有说明:char *language[] = {"FORTRAN", "BASIC", "PASCAL", "JAVA", "C"};

则表达式 *language[1] > *language[3]  比较的是 ( )。

A.字符 'F' 和字符 'P'

B.字符串 "BASIC" 和字符串 "JAVA"

C.字符 'B' 和字符 'J'

D.字符串 "FORTRAN" 和字符串 "PASCAL"

选择C,先比较首元素大小

2-12 设有以下程序段,若 k 为 int 型变量且 0≤k<4,则对字符串的不正确引用是( )。

char str[4][10] = {"first", "secone", "third", "fourh"}, *strp[4];
int i;

for(i = 0; i < 4; i++){
    strp[i] = str[i];
}

A.strp B.str[k] C.strp[k] D.*strp

选择A,strp只是指针,不是元素

2-13 以下程序段的功能是:输入一行字符,按输入的逆序建立一个链表。

char c;
struct node{
    char info;
    struct node *link;
} *top, *p;

top = NULL;
while ( (c = getchar() ) != '\n'){
    p = (struct node*) malloc(sizeof(struct node));
    p->info = c;
    _________ ;
    top = p;
}

A.top->link = p B.p->link = top C.top = p->link D.p = top->link

选择B

2-14 若已建立下面的链表结构,指针 p、q 分别指向图中所示结点,则不能将 q 所指结点插入到链表末尾的语句是( )。

1.jpg

A.

q->next = NULL;
p = p->next; 
p->next = q;

B.

p = p->next; 
q->next = p->next;
p->next = q;

C.

p = p->next; 
q->next = p;
p->next = q;

D.

p = (*p).next; 
(*q).next = (*p).next;
(*p).next = q;

选择C 

2-15 假设下列程序保存在test.c中,编译后运行test hello world,则输出是

#include<stdio.h>
int main(int argc, char *argv[ ])
{
      printf("%d,%s", argc, argv[1]+1);
            
      return 0;
}

A.2,est B.2,ello C.3,ello D.3,orld

选择C

2-16 设有如下定义的链表,则值为7的表达式是( )。

struct st{
     int n;
     struct st *next;
} a[3] = {5, &a[1], 7, &a[2], 9, NULL}, *p = &a;

A.p->n B.(p->n)++ C.++p->n D.p->next->n

选择D

2-17 如果有函数char *func(char *p, char ch),则下面说法错误的是( )。

A.函数返回一个字符指针

B.可以通过语句return NULL;返回函数结果

C.可以通过语句return -1;返回函数结果

D.可以通过语句return p;返回函数结果

选择C,return -1;怎么想都不对吧

2-18 以下程序的输出结果是( )。

#include <stdio.h>

struct HAR{
    int x, y; 
    struct  HAR  *p;
} h[2];

int main(void)
{ 
    h[0].x = 1; h[0].y = 2;
    h[1].x = 3; h[1].y = 4;
    h[0].p = h[1].p = h;
    printf("%d%d\n", (h[0].p)->x, (h[1].p)->y);

    return 0;        
}

A.12 B.23 C.14 D.32

选择A

2-19 以下程序的输出结果是( )。

#include <stdio.h>
#include <stdlib.h>

struct NODE{ 
    int num;  
    struct NODE  *next; 
};

int main(void)
{ 
    struct NODE *p, *q, *r;
        
    p = (struct NODE*)malloc(sizeof(struct NODE));
    q = (struct NODE*)malloc(sizeof(struct NODE));
    r = (struct NODE*)malloc(sizeof(struct NODE));
    p->num = 10; 
    q->num = 20;
    r->num = 30;
    p->next = q;
    q->next = r;
    printf("%d\n", p->num + q->next->num);
    
    return 0;
}

A.10 B.20 C.30 D.40

选择D

填空题

4-1 

下面程序可以逐行输出由language数组元素所指向的5个字符串。请填写程序中相应语句。

#include<stdio.h>

int main(void)

{

        char *language[] = {"BASIC", "FORTRAN", "PROLOG", "JAVA", "C++"} ;

        char **q;

        int k;

        for(k = 0 ; k < 5 ; k++) {

                q = language+k;

                printf("%s\n", *q);

        }

        return 0;

}

4-2 

下面程序的运行结果是 8

#include<stdio.h>
int main(void)
{
    int  i, j, sum, *p[4];
    static int a[4][4];
        
    for(i = 0; i < 4; i++){
        p[i] = &a[i][0];
    }
    for(i = 0 ; i < 4 ; i++) {
        *(p[i] + i) = 1 ;
        *(p[i] + 4 - (i + 1)) = 1;
    }
    sum = 0;
    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            sum  += p[i][j]; 
        }
    }
    printf("%d", sum);
        
    return 0;
}

4-3 下列函数用于将链表中各结点的数据依次输出。

struct student {

        long data;

        struct student *next;

};

void print(struct student *head)

{

        struct student *p ;

        p=head;

        if(head != NULL)

                do {

                        printf("%ld\n”, p->data);

                        p=p->next;

                } while (p!=NULL);

}

4-4 若有定义int a = 10, *p1 = &a, **p2 = &p1;,则表达式 **p2 的值是 10

4-5 若有定义double *p;请写出利用malloc函数使p指向一个双精度型的动态存储单元的完整语句为 p=(double *)malloc(sizeof(double));

4-6 

下面程序运行结果是 4,5

#include <stdio.h>
int main()
{
    int x[5] = {2, 4, 6, 8, 10}, *p, **pp;
    
    p = x;
    pp = &p;
    printf("%d," , *(++p));
    printf("%d\n" , (**pp)+1);
    
    return 0;
}

4-7 

写出以下程序的运行结果: 6375

#include<stdio.h>
int main()
{
    char ch[2][5] = {"6937", "7254"}, *p[2];
    int i, j, s = 0;
    
    for(i = 0; i < 2; i++){
        p[i] = ch[i];
    }
    for(i = 0; i < 2; i++){
      for(j = 0; p[i][j] > '\0'; j += 2){
        s = 10 * s + p[i][j] - '0';
      }
    }
    printf("%d\n", s);
    
    return 0;
}

4-8 已建立学生“英语”课程的成绩链表(成绩存于score域中,学号存于num域中), 下列函数用于输出不及格学生的学号和成绩,及补考学生人数。

void require(struct student *head)

{

        struct student *p;

        long x;

        if( head != NULL){

                x=0;

                p=head;

                while(p != NULL) {

                        if(p->score<60) {

                                printf(”%7d %6.1f\n”, p->num, p->score);

                                x++;

                        }

                        p = p->next;

                }

        printf(”%ld\n”, x);

        }

}

4-9 

下列程序的输出结果是 efgh

#include <stdio.h>
#include <string.h>
char *fun ( char *t );
 
int main(void) 
{ 
    char  *str = "abcdefgh"; 
    
    str = fun ( str ); 
    puts ( str );     
    
    return 0; 
}

char *fun ( char *t ) 
{ 
    char *p = t; 
    
    return ( p + strlen(t)/2 ); 
} 

程序填空题

5-1 查找最高分

输入10个成绩,查找最高分并输出。

#include <stdio.h>

int *GetMax(int score[ ], int n);

int main(void) {

        int i, score[10], *p;

        for(i = 0; i < 10; i++){

                scanf("%d", &score[i]);

        }

        p =GetMax(score,10);

        printf("%d\n", *p);

        return 0;

}

int *GetMax(int score[ ], int n)

{

        int i, max, pos = 0;

        max = score[0] ;

        for(i = 0 ; i < 10 ; i++){

                if(score[i] > max){

                        max = score[i];

                        pos = i ;

                }

        }

        return score+pos;

}

5-2 输出多个字符串中最小的字符串。

#include <stdio.h>

#include <string.h>

int main()

{

        const char *st[] = {"bag", "good", "This", "are", "Zoo", "park"};

        const char *smin;

        int i;

        smin=st[0];

        for(i = 1; i < 6; i++){

                if(strcmp(st[i],smin) < 0){

                        smin = st[i];

                }

        }

        printf("%s\n",smin);

        return 0;

}

  • 36
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北欧海盐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值