编辑代码

#include <stdio.h>
#include<string.h>
#define SIZE 9999
char buf[SIZE];
char *p = buf;
char *ap(int n){
    char *begin;
    if(p + n <= buf + SIZE){ //判断是否大于缓冲区空间
        begin = p;
        p = p + n;
        return begin;
    }else{
        return NULL;
    }
}
int main () {
    char *p1,*p2;
    int i;
    p1 = ap(4);
    strcpy(p1,"234");
    p2 = ap(5);
    strcpy(p2,"efgh");
    printf("buf=%p\n",buf);//缓冲区地址
    printf("p1=%p\n",p1);//在缓冲区起始地址,此时buf = p1
    printf("p2=%p\n",p2);//p2 = p1 + 5(p1 = ap(4))
    puts(p1);  //输出*p1  ==> 2,3,4,\0   ==> end
    puts(p2);  //输出*p2  ==> e,f,g,h,\0  ==> end
    for(i = 0;i < 9;i++){
        printf("%c\n",buf[i]);
    }
    return 0;
}