内存分配char *和char [] [英] Memory Allocation char* and char[]

查看:143
本文介绍了内存分配char *和char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个在内存分配方面的区别是什么。

What is the difference between these two in terms of memory allocation.

char *p1 = "hello"; 
char p2[] = "hello";


推荐答案

第一个创建一个 em>变量(取决于平台的四个或八个字节的存储)并且存储字符串文本的位置。第二个创建六个字符的数组

The first one creates a pointer variable (four or eight bytes of storage depending on the platform) and stores a location of a string literal there, The second one creates an array of six characters (including zero string terminator byte) and copies the literal there.

你应该在第一行得到一个编译器警告,因为字面量是 const

You should get a compiler warning on the first line since the literal is const.

这篇关于内存分配char *和char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆