c_test

1.int a[][4]={0,0};与int a[3][4] = {0};

元素不够的就以位模式初始化为0

a[第一维][第二维] 的大小,也就是最多存几个

int a[][3]={1,2,3,4,5,6,7,8};实际上等于
int a[][3]={ {1,2,3}, {4,5,6}, {7,8} };
有3个一维元素,所以一维大小为3

测试代码:
 1 int main(void) {
 2     int a[3][4] = {0};
 3     for (int i = 0; i < 3; ++i) {
 4         for (int j = 0; j < 4; ++j) {
 5             printf("%d ", a[i][j]);
 6             if(j == 3) printf("\n");
 7         }
 8     }
 9     return 0;
10 }
View Code

 

2.int k=10;
while(k=0) k=k-1;
循环体语句一次也不执行
while中k=0,则while(0)判断为0后不再执行下面的语句
3.t=0; while(printf("*")) { t++; if(t>3)break; } 这段程序可执行几次 (4次)

int printf(const char *format, ...);

Upon successful return, these functions return the number of characters
printed (excluding the null byte used to end output to strings).

 4.内存操作:将指针unsigned char* ptr的内容向后移动4个字节

*(ptr+4)=*ptr;

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <vector>
 6 #include <time.h>
 7 using namespace std;
 8 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
 9 
10 int main(void) {
11     int i=65535;
12     unsigned char* ptr=(unsigned char *)&i;
13     for (int j = 0; j < 4; ++j) {
14         printf("%#x %#x\n", &i+j, ptr[j]);
15     }
16     *(ptr+4) = *ptr;
17     for (int j = 0; j < 5; ++j) {
18         printf("%#x %#x\n", &i+j, ptr[j]);
19     }
20     return 0;
21 }

 

5.将无符号变量unsigned int val进行字节序颠倒

    //unsigned int val = 0xff000000;
    unsigned int val = 0xff;
    //val = ((val&0x000000ff)<<24)|((val&0x0000ff00)<<8) |( (val&0x00ff0000)>>8)| ((val&0xff000000)>>24);
    val = ((val&0x000000ff)<<24)|((val&0x0000ff00)<<8) |( (val&0x00ff0000)>>8)| (val>>24);
    printf("%#x\n", val);

 

 6.字符串逆转:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <time.h>
using namespace std;
#define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl

int main(void)
{
    char *src = "hello,world";
    char *dest = NULL;
    int len = strlen(src);
    dest = (char *)malloc(len + 1);    //要为\0分配一个空间
    char *d = dest;
    char *s = &src[len - 1];    //指向最后一个字符
    while (len-- != 0)
    {
        *d++ = *s--;
        *d = 0;        //尾部要加上\0
    }
    cout << dest << endl;
    free(dest);        //使用完,应当释放空间,以免造成内存泄露
    return 0;

}
 
 

 7.编写strcpy函数(10分)  已知strcpy函数的原型是  char *strcpy(char *strDest, const char *strSrc);   其中strDest是目的字符串,strSrc是源字符串。

(1)不调用C++/C的字符串库函数,请编写函数 strcpy

char *strcpyx(char *strDest, const char *strSrc){
    assert((strDest!=NULL) && (strSrc !=NULL));//2分
    char *address = strDest;// 2分
    while( (*strDest++ = *strSrc++) != '\0')NULL;//2分
    return address;// 2分
}

 

 

(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
答:为了实现链式表达式。 // 2分
例如 int length = strlen( strcpy( strDest, "hello world") );

 

 

转载于:https://www.cnblogs.com/guxuanqing/p/5976381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值