Chinaunix首页 | 论坛 | 博客
  • 博客访问: 503424
  • 博文数量: 81
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1500
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-15 10:51
文章分类

全部博文(81)

文章存档

2011年(1)

2009年(22)

2008年(58)

我的朋友

分类: C/C++

2009-02-04 00:18:15

  笔试部分:

  (1).请指出下面程序的错误。

  main()

  {

char string[10];
char *str1 = "0123456789";
strcpy( string, str1 );

}


答案:字符串str1需要11个字节才能存放下(包括末尾的’\0),而string只有10个字节的空间,strcpy会导致数组越界;

 

(2).请在不改变a,b定义的情况下修改下面代码,使它可以正确输出a,b的值。

main()

{

             int *a;

             int *b;

 

             *b=1;

             *a=*b;

             printf(“%d”,*a);

             printf(“%d”,*b);

}

 

答案:a,b指针没有分配空间就赋值。应该加上a=(int *)malloc(sizeof(int));

b=(int *)malloc(sizeof(int));

 

(3).请问下面两条输出哪句是错误的。

main()

{

             char *a=”123456”;

  

             printf(“%s”,a);

             printf(“%s”,*a);

}

 

答案:printf(“%s”,*a);是错误的。

 

(4).下面的程序哪句是错误的,为什么。

main()

{

       char a[4]=”123”;  

        char *b=”456”;     

a=b;              

        b=a;

        printf(“%s”,a);

        printf(“%s”,b);

}

 

答案:a=b;错误解释:数组不可以直接赋值。

 

(5).请写出下面程序的运行结果。

main()

{

    char str[20];

    char *ptr;

    strcpy(str,"123456");

    ptr=str;

    *ptr++=0;

    printf("%s",ptr);

}

 

答案:23456  解释:*ptr++ ptr指向了2,输出就从2开始遇到结束标志‘/0 ’结束。

 

(6). 请写出下面程序的运行结果。

void temp(int *a,int *b)

{

        int *temp=0;

        temp=a;

        a=b;

        b=temp;

}

 

main()

{

        int a=1;

        int b=2;

 

        temp(&a,&b);

        printf(“%d,%d”,a,b);

}

 

答案:12 解释:主函数传递的,temp函数交换的都是指针。形参改变不影响实参。

 

(7). 请写出下面程序的运行结果。

int test(int a)

{

        a=a+1;

        return a;

}

 

main()

{

        int a=1,i;

        for(i=0;i<5;i++)

             test(a);

printf(“%d”,a);

}

 

答案:1   解释:a是局部变量,在test调用被重新赋值。

阅读(951) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~