C指针强化测试

接下来的几个用代码的形式给出,可以直接粘贴到vim编译器中进行运行,测试

1:

#include<stdio.h>
#include <string.h>
/*---------------------------19-----------------------------*/
#if 0
int main()
{
        int a=3,b=4,c=0;
        c = a+++b;
        printf("%d,%d,%d\n",a,b,c);
        return 0;
}
#endif
/*---------------------------18-----------------------------*/
#if 0
int main(){
        char str[] = "abc456double8.09";
        int i;
        double db; 
        sscanf(str, "abc%ddouble%lf", &i, &db);
        scanf(      "abc%ddouble%lf", &i, &db);
        //  strstr
        //  strchr
        printf("%d\n%lf\n", i, db);
        return 0;
}

#endif
/*---------------------------17-----------------------------*/
#if 0
int main()
{
        int a=3,b=4,c=0;
        c = a+++b;
        printf("%d,%d,%d\n",a,b,c);
        return 0;
}
#endif
/*---------------------------16-----------------------------*/
#if 0
typedef int* point_type;
#define POINT_TYPE int*
int main()
{
        point_type p1,p2;
        POINT_TYPE p3,p4;
        printf("%d,%d,%d,%d\n",sizeof(p1),sizeof(p2),sizeof(p3),sizeof(p4));
        return 0;
}
#endif
/*---------------------------15-----------------------------*/
#if 0
struct data_struct{
        char ds1;
        char ds2;
        char ds3;
        char ds4;
};
union data_union{
        int d1;
        struct data_struct d2;
};
int main()
{
        union data_union x;
        x.d1 = 0x11223344;
        x.d2.ds2 = 0xaa;
        printf("%x\n",x.d1);
        return 0;
}
#endif
/*---------------------------14-----------------------------*/
#if 0
struct data{
        char d1;
        char d2;
        char d3;
        char d4;
};
int main()
{
        struct data d = {.d4 = 0x44,.d3 = 0x33,.d2 = 0x22,.d1 = 0x11};
        struct data *p=&d;
        char *pc = (char *)p;
        short *ps = (short *)p;
        int *pi = (int *)p;
        *pc = 0xaa;
        printf("d1=%x,d2=%x,d3=%x,d4=%x\n",d.d1,d.d2,d.d3,d.d4);
        *(ps+1)=0xbbcc;
        printf("d1=%x,d2=%x,d3=%x,d4=%x\n",d.d1,d.d2,d.d3,d.d4);
        *pi = 0xaabbccdd;
        printf("d1=%x,d2=%x,d3=%x,d4=%x\n",d.d1,d.d2,d.d3,d.d4);
        return 0;
}
#endif
/*---------------------------13-----------------------------*/
#if 0
int main()
{
        char *p = NULL;
        char str[] = "hello";
        char str1[] = "world";
        char str2[50];
        char str3[] = "12345";
        p = strcpy(str2,str);
        p = strcat(str2+1,str1);
        strcpy(p,str3);
        printf("%s\n",p);
        return 0;
}
#endif
/*---------------------------12-----------------------------*/
#if 0
int main()
{
        char str[50] = "hello world";
        printf("%d,%d\n",sizeof(str),strlen(str));
        return 0;
}
#endif
/*---------------------------11-----------------------------*/
#if 0
struct data{
        short a;
        int b;
        char c;
        char d[5];
};
int main(){
        printf("%d\n",sizeof(struct data));
        return 0;
}
#endif
/*---------------------------10-----------------------------*/#if 0
int main()
{
        int i,j;
        int arr[3][3]={1,2,3,4,5,6,7,8,9};
        int (*pt)[4]=NULL;
        int *p = NULL;
        pt = arr;
        *(pt+1) = 100;//error   *(pt+1)是一行数组
        *(p+1) = 200;
        for(i=0;i<3;i++){
                for(j=0;j<3;j++){
                        printf("%d,",arr[i][j]);//如果结果最高为为1 那么自动补齐
                }
                printf("\n");           
        }
        return 0;
}
#endif
/*---------------------------9------------------------------*/
#if 0
int main()
{
        int array[]={1,2,3,4,5};
        int *p = NULL;

        p = &array;
        p = p+1;
        printf("%d\n",*p);
        return 0;
}
#endif
/*---------------------------8------------------------------*/
#if 0
int main()
{
        int i,j;
        char array[3][3]={0x11,0x22,0x33
                                         ,0x44,0x55,0x66
                                         ,0x77,0x88,0x99};

        short *p = (short *)array;
        *p = 0xaabb;
        *(p+1) = 0xccdd;
        for(i=0;i<3;i++){
                for(j=0;j<3;j++){
                        printf("%p,",array[i][j]);//如果结果最高为为1 那么自动补齐
                }
                printf("\n");           
        }
        return 0;
}
#endif
/*---------------------------7------------------------------*/
#if 0
int main()
{
        char array[]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
        char *p1 = array;
        short *p2 = (short *)array;
        int *p3 = (int *)array;

        printf("*p1=%p,*p2=%p,*p3=%p\n",*p1,*p2,*p3);
        printf("*(p1+1)=%p,*(p2+1)=%p,*(p3+1)=%p\n",*(p1+1),*(p2+1),*(p3+1));
        return 0;
}
#endif
/*---------------------------6------------------------------*/
#if 0
int *fun(int *p)
{
        int x = *p;
//      static int x = *p;
        return &x;              //函数结束后内容消失,出参出地址非法
}
int main()
{
        int x=10;
        int *p=NULL;
        p = fun(&x);
        printf("*p = %d\n",*p);
        return 0;
}
#endif
#if 0
int main()
{
        int x=1;
//      printf("%d\n",x++);
//      printf("%d,%d\n",x=16,x++);
        printf("%d,%d,%d\n",x=x+10,x=16,x--);
        return 0;
}
#endif
/*---------------------------5------------------------------*/
#if 0
int main()
{
        unsigned int i = 5; //i永远是大于0的数,所以为死循环
//      int i = 5;                      //打印结果x = 7
        int x = 1;
        for( ;i>=0;i--)
        {
                x = x+1;
                printf("%d\n",i);
                getchar();
        }
        printf("x=%d\n",x);
        return 0;
}
#endif
/*---------------------------4------------------------------*/
#if 0
int main()
{
        int x=2,y=2,z=0;        //0000 0010     ^异或  相同为0,不同为1
        printf("%d\n",x^1);     //0000 0001     0000 0011   = 3
        z=x^1||++y;                     // = 0^1 = 1;   
        printf("x=%d,y=%d,z=%d\n",x,y,z);
        return 0;
}
#endif
/*---------------------------3------------------------------*/
#if 0
int main()
{
        int x=2,y=1,z=0;
        z=x>>2&&y++;    //&& 左边遇0就不看右边  ||左边有1就不看右边
        printf("x=%d,y=%d,z=%d\n",x,y,z);
        return 0;
}
#endif
/*---------------------------2------------------------------*/
#if 0
int main(){
        int a=3,b=4;
        if(a++ || b++){   //  || 一个运算成立,则不看后面的原算
                a++;
                b++;
        }
        printf("%d %d\n",a,b); // result ?
        return 0;
}
#endif
/*---------------------------1------------------------------*/
#if 0
int main(){
        int a,b,c,d;
        a = 10;
        b = a++;        //++在后  先运算a 在+1
        c = ++a;        //++在前  先+1 在运算a
        d = 10*a++;
        printf("%4d %4d %4d\n",b,c,d); //result ?
        return 0;
}
#endif


2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*------------------------7----------------------------*/
#if 0
char *pp[2][3] = {"abc","defgh","ijkl","mnopqr","stuvw","xyz"};
int main()
{
        printf("%p\n",pp);
        printf("%c\n",***(pp+1));
//      printf("%c\n",**pp[0]);
//      printf("%c\n",(*(*(pp+1)+1))[4]);
//      printf("%c\n",*(pp[1][2]+2));
//      printf("%s\n",**(pp+1));
}
#endif
/*------------------------15---------------------------*/
#if 0
#endif
/*------------------------14---------------------------*/
#if 0
#endif
/*------------------------13---------------------------*/
/* 如何引用一个已经定义过的局部变量?局部变量与全局变量是否能重名
 * 1.可以用extern关键字声明外部变量,或者引用头文件
 * 2.局部变量与全区变量可以重名,局部会屏蔽全局
 * */
/*------------------------12---------------------------*/
/* 堆栈溢出一般都是由什么原因导致的?
 * 1.可能由于循环的递归引起的
 * 2.分配了过大的局部变量
 * */
/*------------------------11---------------------------*/
/* a: An integer
 *              int a;
 * b: A pointer to an integer
 *              int *a;
 * c: A pointer to a pointer to an integer
 *              int **a;
 * d: An array of 10 integer
 *              int a[10];
 * e: an array of 10 pointers to integers
 *              int (*a)[10];
 * f: A pointer to an array of 10 integers
 *              int a[10];
 * g: A pointer to a function that takes an integers as an argument and returns an integer
 *              int (*a)(int );
 * h: An array of ten pointers to functions that take an integer argument and return an integer
 *              int (*a[10])(int );
 * */
/*------------------------10---------------------------*/
#if 0
#define SQR(X) X*X
int main()
{
        int a=10,k=2,m=1;
        a/=SQR(k+m)/SQR(k+m);
        printf("%d\n",a);
}
#endif
/*------------------------9----------------------------*/
#if 0
int main()
{
        int a[5]={1,2,3,4,5};
        int *ptr = (int *)(&a+1);
        printf("%d %d\n",*(a+1),*(ptr-1));
}
#endif
/*------------------------8----------------------------*/
#if 0
int main()
{
        struct node
        {
                int a;
                int b;
                int c;
        };
        struct node s = {3,5,6};
        struct node *pt = &s;
        printf("%d\n",*(int *)pt);
}
#endif
/*------------------------6----------------------------*/
#if 0
void main()
{
        int k=5,n=0;
        while(k>0)
        {
                printf("k %d\n",k);
                switch(k)
                {
                        case 1:n+=k;printf("case1 n %d\n",n);
                        case 2:printf("case2 n %d\n",n);
                        case 3:n+=k;printf("case3 n %d\n",n);
                        default:break;
                }
                k--;
                getchar();
        }
        printf("%d\n",n);
}
#endif
/*------------------------5----------------------------*/
#if 0
void main()
{
        char *str = (char *)malloc(100);
        strcpy(str,"hello");
        free(str);
        if(str != NULL)
        {
                strcpy(str,"world");
                printf("%s\n",str);
        }
}
#endif
/*------------------------4----------------------------*/
#if 1
int main()
{
        int arr[2][3] = {{1,2,3},{4,5,6}};
        int m,*p;
        p = arr[0];
        m = (*p)*(*(p+2))*(*(p+4));
        printf("%d\n",m);
}
#endif
/*------------------------3----------------------------*/
#if 0
int main()
{
        char *p;
        char buf[] = {1,2,3,4,5,6,7,9,8};
        p = (buf+2)[5];
        printf("%d\n",p);
}
#endif
/*------------------------2----------------------------*/
#if 0
int main()
{
        int a[][3]={1,2,3,4,5,6};
        int (*ptr)[3]=a;
        printf("%d %d\n",(*ptr)[1],(*ptr)[2]);
        ++ptr;
        printf("%d %d\n",(*ptr)[1],(*ptr)[2]);
}
#endif
/*------------------------1----------------------------*/
#if 0
void main()
{
        char a[30];
        char *b = (char *)malloc(20*sizeof(char ));
        char **c[3][4];   //???

        printf("%d\n",sizeof(a));
        printf("%d\n",sizeof(b));
        printf("%d\n",sizeof(b[3]));
        printf("%d\n",sizeof(b+3));
        printf("%d\n",sizeof(c));
}
#endif


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值