C语言的指针函数(小测)

char * test(){
return "hello";
}
char * test2(){
    ///Users/lanqs/Desktop/c语言学习/谭启宏-C-9/谭启宏-C-9/main.c:23:12: Address of stack memory associated with local variable 'string' returned
//    char string[20] = "HELLO";
//    char * string = "HELLO";
    char * string = malloc(20);
    *string ='b';
    *(string +1) ='a';
//    free(string);//(有木有这一句后面都没得输出,eee 这一句是对内存的管理不能及时清理,所以应该写在下面)
    return string;//直接赋值第二个元素不会输出,因为第一个元素是'\0'
}
    //test()
    char *temp = test();
    printf("temp = %s\n",temp);
    
    
//    temp[0]='h';//崩溃
//    printf("temp = %s",temp);
    
    temp ="wrold";//指向world地址
    printf("temo = %s\n",temp);
    
    //test2()
    
    char *temp1 = test2();
    printf("temp1 = %s\n",temp1);

输出结果:

temp = hello

temo = wrold

temp1 = ba


Hello, World!

Program ended with exit code: 0



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值