使用字符指针和字符数组的区别 [英] Difference between using character pointers and character arrays

查看:120
本文介绍了使用字符指针和字符数组的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本的问题。

char new_str[]="";

char * newstr;

如果我有一些数据连接成或使用字符串函数类似的strcat / SUBSTR / strcpy的,什么是两者之间的区别?

If I have to concatenate some data into it or use string functions like strcat/substr/strcpy, what's the difference between the two?

我明白我必须分配内存为char *的方法(2号线)。我真的不知道怎么虽然。

I understand I have to allocate memory to the char * approach (Line #2). I'm not really sure how though.

和const char *与字符串文字是一样的吗?

And const char * and string literals are the same?

我需要知道更多关于这个。有人能指出一些不错的内容无遗/材料?

I need to know more on this. Can someone point to some nice exhaustive content/material?

推荐答案

请通过的下面这篇文章

在字符数组的情况下,也看到像你的情况,焦炭new_str []那么new_str将总是指向数组的基地。本身的指针不能被递增。是的,你可以使用下标来访问下一个字符数组例如: new_str [3] ;

Also see in case of array of char like in your case, char new_str[] then the new_str will always point to the base of the array. The pointer in itself can't be incremented. Yes you can use subscripts to access the next char in array eg: new_str[3];

但在指针的情况下为char,指针可以增加 new_str ++ 来获取你的数组中的一个字符。

But in case of pointer to char, the pointer can be incremented new_str++ to fetch you the next character in the array.

此外,我会建议 >更加清晰。

Also I would suggest this article for more clarity.

这篇关于使用字符指针和字符数组的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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