zoukankan      html  css  js  c++  java
  • c

    1:mallo内存分配
    #include"stdio.h"
    #include"string.h"
    #include "malloc.h"
    void main()
    {
    char *src ="hello world"; //数据大小 11=10+1空格
    char *dst=NULL;
    dst=(char *)malloc(sizeof(char)*strlen(src)+1);//分配内存时候的注意事项:对应类型的转换,数据大小和
    memcpy(dst,src,strlen(src));
    printf("dst=%c ",*dst);
    printf("src=%c ",*src);
    printf("sizeofchar=%d ",sizeof(char));
    printf("sizeofchar=%d ",strlen(src));
    printf("sizeofchar=%d ",sizeof(char*));
    }

    dst=h
    src=h
    sizeofchar=1
    sizeofchar=11
    sizeofchar=4

    2:常量指针变量:
    #include"stdio.h"
    #include"string.h"
    void main()
    {
    int num1=10;
    int num2=10;
    int * const ptr1=&num1;//必须在这地方有一个指向,否则报错 指向的地址不可改变,ptr1=&num2 代表一个常量 ,
    //但是num1的数值是可以改变的 比如num1 =100,*ptr1=1000; 只是指向地址不能改变
    printf("value=%d ",*ptr1);
    }
    其实是指向的地址不可改变 为一个常量 其他可以改变 可用 通过num1=1000;或者*ptr1=1000来改变数值
    常量指针:

    #include"stdio.h"
    #include"string.h"
    void main()
    {
    int num1=10;
    int num2=10;
    const int * ptr1=&num1;//修饰的是(int * ptr1) 所以//*ptr1=1000; 这个是不能操作成功的 已经固定为常量而已 不能通过*ptr1改变 其他可以操作
    //可以改变初始化的地址,
    printf("value=%d ",*ptr1);
    num1=100;
    printf("value=%d ",*ptr1);
    }


    int const * const a6 = &a1; ///const data,const pointer
    const int * const a7 = &a1; ///const data,const pointer
    只能修改a1的数值

    3:函数指针

    #include"stdio.h"
    #include"string.h"
    #include "malloc.h"
    void mesg(int num)
    {
    printf("mesg no.%d ",num);

    }
    int* add(int x,int y)
    {
    int *z=(int *)malloc(sizeof(int));
    *z=10;
    printf("z=%d,x+y=%d ",*z,x+y);
    return z;
    }
    int one(int data)
    {

    printf("data=%d ",data+1);
    printf("one ");
    }

    int two(int data)
    {

    printf("data=%d ",data+2);
    printf("two ");
    }

    int three(int data)
    {

    printf("data=%d ",data+3);
    printf("three ");
    }


    int serach(int(*all)(int),int data)
    {

    return (*all)(data);
    }

    int (*p[])(int)={one,two,three};
    typedef int (*calc)(int y);
    calc remathfunc(int index)
    {
    return p[index];

    }


    void main()
    {
    int *t;
    void (*fpmsg)(int);
    int* (*addfptr)(int ,int );
    int (*calcc)(int x);
    calcc=remathfunc(0);

    addfptr=&add;
    fpmsg=&mesg;
    addfptr(10,100);
    fpmsg(100000);
    serach(one ,10);
    serach(two ,10);
    serach(three ,10);
    calcc(10000);
    }

    7,1 Top

    #include"stdio.h"
    #include"string.h"
    #include "malloc.h"
    void mesg(int num)
    {
    printf("mesg no.%d ",num);

    }
    int* add(int x,int y)
    {
    int *z=(int *)malloc(sizeof(int));
    *z=10;
    printf("z=%d,x+y=%d ",*z,x+y);
    return z;
    }

    void main()
    {
    int *t;
    void (*fpmsg)(int);
    int* (*addfptr)(int ,int );
    addfptr=&add;
    fpmsg=mesg;
    addfptr(10,100);
    fpmsg(100000);


    }
    }
    int* add(int x,int y)
    {
    int *z=(int *)malloc(sizeof(int));
    *z=10;
    printf("z=%d,x+y=%d ",*z,x+y);
    return z;
    }

    void main()
    {
    int *t;
    void (*fpmsg)(int);
    int* (*addfptr)(int ,int );
    addfptr=&add;
    fpmsg=mesg;
    addfptr(10,100);
    fpmsg(100000);

    #include"stdio.h"
    #include"string.h"
    void main()
    {
    int arr[]={1,2,3,4};
    int (*ptr2arr)[4];
    int i=0;
    int *ptrr=arr;
    ptr2arr=&arr;
    for(i=0;i<4;i++)
    {
    printf("address of arrdar=%p ",arr+i);
    }
    for(i=0;i<4;i++)
    {
    printf("address of %p=%d ",(ptr2arr[0]+i),*(ptr2arr[0]+i));
    }
    }


    address of arrdar=0xbfc53b90
    address of arrdar=0xbfc53b94
    address of arrdar=0xbfc53b98
    address of arrdar=0xbfc53b9c
    address of 0xbfc53b90=1
    address of 0xbfc53b94=2
    address of 0xbfc53b98=3
    address of 0xbfc53b9c=4

    //////****************************************************************/////
    char *p1[10];
    char (*p2)[10];
    int i=0;

    char a1[10]={'1','2','3','4','5','6','7','8','9',''};
    char a2[2][10]={{'q','w','e','r','t','y','u','i','o',''},{'a','s','d','f','g','h','j','k','l',''}};
    p1[0]=&a1[0];
    p1[1]=&a1[1];
    p1[8]=&a1[8];
    printf("%c ",*p1[0]);
    printf("%c ",*p1[1]);
    printf("%c ",*p1[8]);
    p2=a2;

    for(i=0;i<10;i++)
    {
    printf("%c ",p2[0][i]);
    printf("%c ",p2[1][i]);
    }

    1
    2
    9
    q
    a
    w
    s
    e
    d
    r
    f
    t
    g
    y
    h
    u
    j
    i
    k
    o
    打印结果 实际为操作一维数组 的理解方式即可

  • 相关阅读:
    【机器学习笔记】EM算法及其应用
    【机器学习笔记】循环神经网络RNN
    【caffe范例详解】
    Caffe on Windows (Visual Studio 2015+CUDA8.0+cuDNNv5)
    【Keras案例学习】 CNN做手写字符分类(mnist_cnn )
    Celery分布式文件队列
    通过nginx+lua或openresty实现web站点的waf功能
    使用docker hub获取kubernetes各个组件的镜像
    使用Ansible快速构建kubernetes1.10.4HA高可用集群
    创建私服maven服务
  • 原文地址:https://www.cnblogs.com/wangzhihong-102511/p/10412109.html
Copyright © 2011-2022 走看看