Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 723
halluc1nati0n Avatar asked Nov 27 '09 08:11

halluc1nati0n


People also ask

What is the difference between char * and char array?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable). Why is char* str commonly used when str denotes a string.

What is the difference between pointers and arrays?

An array is a collection of elements of similar data type whereas the pointer is a variable that stores the address of another variable. An array size decides the number of variables it can store whereas; a pointer variable can store the address of only one variable in it.

What is the difference between C strings and arrays or pointers to arrays of characters?

Key Differences Between Character Array and StringA character array is a collection of variables which are of character datatype. String is a class that is instantiated to declare strings. Using index value you can access a character from a character array.

Is an array a char pointer?

Since each element of games array is a pointer to char or (char*) , it can point to any string literal assigned to it.

What is the difference between an array and a pointer?

anyway, array in C is just a pointer to the first object of an adjust objects in the memory. the only different s are in semantics. while you can change the value of a pointer to point to a different location in the memory an array, after created, will always point to the same location.

What is the difference between a character array and string?

Both a character array and string contain the sequence of characters. But the fundamental difference between character array and string is that the “character array” can not be operated with standard operators, whereas, the “string “objects can be operated with standard operators.

What is the difference between sizeof and char pointer in C++?

When we use the sizeof operator on the char array arr it gives the total number of characters whereas char pointer ptr only gives the size of the pointer. Example,

What is the difference between arr and char* in C++?

The type of both the variables is a pointer to char or (char*), so you can pass either of them to a function whose formal argument accepts an array of characters or a character pointer. Here are the differences: arr is an array of 12 characters.


3 Answers

The excellent source to clear up the confusion is Peter Van der Linden, Expert C Programming, Deep C secrets - that arrays and pointers are not the same is how they are addressed in memory.

With an array,

char new_str[];
the compiler has given the new_str a memory address that is known at both compilation and runtime, e.g. 0x1234, hence the indexing of the new_str is simple by using []. For example new_str[4], at runtime, the code picks the address of where new_str resides in, e.g. 0x1234 (that is the address in physical memory). by adding the index specifier [4] to it, 0x1234 + 0x4, the value can then be retrieved.

Whereas, with a pointer, the compiler gives the symbol

char *newstr
an address e.g. 0x9876, but at runtime, that address used, is an indirect addressing scheme. Supposing that newstr was malloc'd
newstr = malloc(10);
, what is happening is that, everytime a reference in the code is made to use newstr, since the address of newstr is known by the compiler i.e. 0x9876, but what is newstr pointing to is variable. At runtime, the code fetches data from physical memory 0x9876 (i.e. newstr), but at that address is, another memory address (since we malloc'd it), e.g 0x8765 it is here, the code fetches the data from that memory address that malloc assigned to newstr, i.e. 0x8765.

The char new_str[] and char *newstr are used interchangeably, since an zeroth element index of the array decays into a pointer and that explains why you could newstr[5] or *(newstr + 5) Notice how the pointer expression is used even though we have declared char *newstr, hence

*(new_str + 1) = *newstr;
OR
*(new_str + 1) = newstr[1];

In summary, the real difference between the two is how they are accessed in memory.

Get the book and read it and live it and breathe it. Its a brilliant book! :)

like image 118
t0mm13b Avatar answered Oct 25 '22 03:10

t0mm13b


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.

like image 42
Justin Samuel Avatar answered Oct 25 '22 03:10

Justin Samuel


This is a character array:

char  buf [1000];

So, for example, this makes no sense:

buf = &some_other_buf;

This is because buf, though it has characteristics of type pointer, it is already pointing to the only place that makes sense for it.

char *ptr;

On the other hand, ptr is only a pointer, and may point somewhere. Most often, it's something like this:

ptr = buf;              // #1:  point to the beginning of buf, same as &buf[0]

or maybe this:

ptr = malloc (1000);    // #2:  allocate heap and point to it

or:

ptr = "abcdefghijklmn"; // #3:  string constant

For all of these, *ptr can be written to—except the third case where some compiling environment define string constants to be unwritable.

*ptr++ = 'h';          // writes into #1: buf[0], #2: first byte of heap, or
                       //             #3 overwrites "a"
strcpy (ptr, "ello");  // finishes writing hello and adds a NUL
like image 35
wallyk Avatar answered Oct 25 '22 03:10

wallyk