使用字符指针和字符数组的区别 [英] Difference between using character pointers and character arrays
问题描述
基本问题。
char new_str [] =;
char * newstr;
如果我必须连接一些数据或使用strcat / substr / strcpy等字符串函数,两者之间的区别?
我知道我必须分配内存给char *方法(行#2)。我不太确定如何。
和const char *和字符串文字是一样的吗?
我需要知道更多。有人能指出一些非常详尽的内容/材料吗?
请通过本文:
此外看到如果数组的字符在你的情况下,char new_str []然后new_str将总是指向数组的基数。指针本身不能递增。是的,您可以使用下标访问数组中的下一个字符,例如: new_str [3]
;
的指针指向char,指针可以递增 new_str ++
以获取数组中的下一个字符。
为了更清楚,我建议本文。 p>
Basic question.
char new_str[]="";
char * newstr;
If I have to concatenate some data into it or use string functions like strcat/substr/strcpy, what's the difference between the two?
I understand I have to allocate memory to the char * approach (Line #2). I'm not really sure how though.
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?
Please go through this article below:
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]
;
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屋!