halluc1nati0n halluc1nati0n - 1 year ago 112
C Question

Difference between using character pointers and character arrays

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?

Answer Source

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.