Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87690
  • 博文数量: 53
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-06 20:01
文章分类

全部博文(53)

文章存档

2008年(53)

我的朋友

分类: C/C++

2008-05-14 16:34:10

1. 说出下面程序段的功能, 并说出两段程序的优缺点.

程序段1:

int ftest1(const char *str) {
       
char num[256] = {0};
       
unsigned char *pos = (unsigned char *)str;
       
while (*pos != 0) {
                   if(num[*pos] == 0) { 

    num[*pos++] = 1;

}else{

    break;

}
   
}
       
return *pos == 0 ? 0 : 1;
}

程序段2:

int ftest2(const char *str) {
       
const char *p1, *p2;
       
if (*str == 0)
               
return 0;
       
for (p1 = str; *p1 != 0; p1++) {
               
for (p2 = p1 + 1; *p2 != 0; p2++) {
                       
if (*p1 == *p2)
                               
return 1;
               
}
       
}
       
return 0;
}

注:提倡采用程序段2,本着易读原则;

 

2. 写出运行结果

#include

#include

#include

 

int main(int argc, char *argv[]) {

 

           char a[] = "abc";

           char b[] = {'d', 'e', 'f'};

 

           printf("a slen=%d,b slen=%d\n", strlen(a),strlen(b));

          printf("a = %s, b = %s\n", a, b);

           printf("asize len = %d, bsize len = %d\n", sizeof(a),sizeof(b));

           return 0;

 

\0

c

b

a

f

e

d

 

}

 

a slen = 3,b slen = 6

a

a = abc, b = defabc

asize len = 4, bsize len = 3

注:栈分配原则:从高地址->低地址分配;

b

 

 

 


4. 说出错误

void test() {

    char str[10];

    char* str1 = "0123456789";//alloc in the only read data area

    strcpy(str, str1); //array index overflow

       Strcpy(str1,str)  //because str1 alloced in the only read data area

}

注:数组越界

 

5. 说出错误

void test() {

    char str[10], str1[10];

    for( int = 0; i < 10; i++){ //memset(str,0,sizeof(str))

//modif  i< 10-1

            str[i] = 'a';

    }

    strcpy(str1, str);//find not string file end descripe

}

 

6. 写出程序的运行结果

#include

 

int sum(int a) {

        static int b = 3;

        b += 2;

        return(a + b);

}

 

int main(void) {

        int a = 2;

        int i;

 

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

            printf("%d, ", sum(a));

        }

        return 0;

}

 

7, 9, 11, 13, 15,

 

7.写出运行结果

#include

 

int main(void)  {

         int a[3];

           a[0] = 0;

           a[1] = 1;

           a[2] = 2;

 

           int *p, *q;

           p = a;

           q = &a[2];

 

           int c = a[q - p];

//同类指针相减结果:

//两指针之间间隔所指类型元素(变量)的个数,所以两指针差值等于所得地址差值除以所指类型所占的字节数;

           printf("value c = %d\n", c++);

           printf("value c = %d\n", c);

 

           return 0;

}

 

value c = 2

value c = 3

 

8 写出运行结果

#include

#include

 

#define STRCPY(a, b)    strcpy(a##_p, #b)

#define STRCPY1(a, b)   strcpy(a##_p, b##_p)

 

int main(void)  {

        char var1_p[20];

        char var2_p[30];

 

        strcpy(var1_p, "aaaa");

        strcpy(var2_p, "bbbb");

 

        STRCPY1(var1, var2);

        STRCPY(var2, var1);

 

        printf("var1 = %s\n", var1_p);

        printf("var2 = %s\n", var2_p);

 

        return 0;

}

 

var1 = bbbb

var2 = var1

 

9不用除法(即不能使用"/")和库函数,计算285 / 16的商和余数, 要求效率尽量高.

#define NUM_SHIFT       4

int inta = 285;

int left;

       

left = inta & ((1L << NUM_SHIFT) - 1);//(10000-1) ===>1111

inta = inta >> NUM_SHIFT;

 

11.指出下面程序段的错误

void teststr(char* p) {

 char str[16];

 int n;

 assert(NULL != p);

 sscanf(p, "%s%d", str, n);//  n->&n

 if (0 == strcmp(str, "something")) {

  ...

 }

}

 

12.指出下面程序段的错误

void test2() {

  char string[10], str1[10];

  int i;

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

        str1[i] ='a';

  }

  strcpy(string, str1);

}

错误:数组越界;

解决方法:

    方法一:

#define STR_LEN     10

void test2() {

  char string[10], str1[10];

  int i;

  memset(str1, 0, sizeof(str1));

for(i=0; i

        str1[i] ='a';

  }

  strcpy(string, str1);

}

    方法二:

#define STR_LEN     10

void test2() {

  char string[10], str1[10];

  int i;

  for(i=0; i

        str1[i] ='a';

  }

  str1[STR_LEN] = \0;

  strcpy(string, str1);

}

 

13.交换两个值

int i = 5;

int j = 8;

i^=j; //I = I ^ j

j^=i; //j = j ^ i

i^=j; // I = I ^ j

 

14进程间通信的方式有?

进程间通信的方式有 共享内存, 管道 ,Socket ,消息队列, file map,signal

 

15.写出程序的结果

struct abc {

char t:4; //low bit

char k:4;

unsigned short i:8;

unsigned long m;

} // add __attribute__((packed)) sizeof(abc) = 0.5+0.5+1+4=6

    No add : sizeof = 0.5+0.5+1+2+4=8 //2: auto align ,add 2

sizeof(abc)=?(不考虑边界对齐)6

 

16.请指出下列程序中的错误并且修改

void get_memory(char *p, int size) {

  p = (char *)malloc(size); //p is copyer str's

}

void test(void) {

  char *str = NULL;

  get_memory(str,100);

  strcpy(str, "hello world");

  printf(str);

}

 

A:错误--参数的值改变后,不会传回

get_memory并不能传递动态内存,Test函数中的 str一直都是 NULL

strcpy(str, "hello world");将使程序崩溃。

修改如下:

方法一:

char *get_memory(int size){

  char *p=(char *)malloc(size);

  return p;

}

void test(void){

  char *str = NULL;

  str = get_memory(100);

  strcpy(str,"hello world");

  printf(str);

}

方法二:

void get_memory(char **p, int size){

    *p = (char *)malloc(size);

}

17.写一个“标准”宏MIN,这个宏输入两个参数并返回较小的一个

#define MIN(A, B) ((A) <= (B) ? (A) : (B))

 

19.假一个硬件寄存器是的地址为0x50000000, 宽度为32位,给出其寄存器在C语言中读/写宏定义

#define LREG (*(volatile unsigned int *)0x50000000)

#define READ_LREG LREG

#define WRITE_LREG(ch) (LREG = (unsigned int)(ch))

 

20.写下面sizeof 的求值

A. 32位为例, 计算sizeof的值.

     char  str[] = "Hello";

     char   *p = str;

     int     n = 10;

请计算

sizeof (str) =   6 //include '/0'           

sizeof (p) =   4          

sizeof (n) = 4

 

B. void func( char str[100]){

   请计算

   sizeof(str) =   4

}

 

C. void *p = malloc( 100 );

请计算

sizeof(p) = 4

 

D. struct date {

    int year;

    int months;

    int days;

};

struct date *d;

请计算

d = malloc(sizeof(struct date));

sizeof(d) = 4

sizeof(*d) = 12

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