Difference between using character pointers and character arrays

前端 未结 9 792
不思量自难忘°
不思量自难忘° 2020-11-29 03:45

Basic question.

char new_str[]=\"\";

char * newstr;

If I have to concatenate some data into it or use string functions like strcat/substr/

相关标签:
9条回答
  • 2020-11-29 04:14
    char new_str[]="abcd";  
    

    This specifies an array of characters (a string) of size 5 bytes (one byte for each character plus one for the null terminator). So it stores the string 'abcd' in memory and we can access this string using the variable new_str.

    char *new_str="abcd";  
    

    This specifies a string 'abcd' is stored somewhere in the memory and the pointer new_str points to the first character of that string.

    0 讨论(0)
  • 2020-11-29 04:15

    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.

    0 讨论(0)
  • 2020-11-29 04:25

    The difference is that one is a pointer, the other is an array. You can, for instance, sizeof() array. You may be interested in peeking here

    0 讨论(0)
提交回复
热议问题