Can arrays in C be stored in non-sequential pointers? - c

This is a conceptual question for me to understand C better. From what I understand about C arrays, they store pointers to values which are generated in sequence as per the space allocated. So for example, if I have this code
char someCharArray[] = {'a', 'b', 'c'};
Let's say my heap doesn't have sequential 12 bits for this array, so I store pointers to the following non-sequential addresses 0x001, 0x011, 0x040.
I know two ways of accessing values from this array as shown in this answer.
Let's say I access the second element of the array like this
char datPointer = someCharArray[1];
In this case, datPointer will point to 0x011 because the address is stored in the array.
However, wouldn't the following code return the wrong address?
char datWrongPointer = (someCharArray + 1);
Because I'm adding sequential addresses to the starting address of someCharArray, so assuming chars are 4 bits I'll get datWrongPointer to be 0x004 instead of what it should be? Maybe I'm misunderstanding the addition function but I'd appreciate if someone could explain it to me.

From what I understand about C arrays, they store pointers to values
No, arrays store values directly. In fact, arrays essentially are their elements. C adds no additional structure.
char someCharArray[] = {'a', 'b', 'c'};
OK, here someCharArray consists of 3 bytes of memory. If it lives in static storage (e.g. because it's a global variable), this memory is typically part of the data section; if it lives in automatic memory (i.e. it's a local variable), it's typically placed on the stack.
Let's say my heap doesn't have sequential 12 bits for this array
How is the heap involved here? Why 12 bits? C doesn't really support objects smaller than 1 byte (and bytes cannot be smaller than 8 bits).
Let's say for the sake of an example that someCharArray ends up being placed at address 0xAB0000. Then the first element lives at 0xAB0000, the second element at 0xAB0001, and the third element at 0xAB0002.
char datPointer = someCharArray[1];
datPointer is not a pointer, it's a single char. This code will assign 'b' to datPointer (because someCharArray[1] is 'b').
char datWrongPointer = (someCharArray + 1);
This is a type error.
someCharArray evaluates to a pointer to its first element (as arrays do unless they're the operand of sizeof or &). This gives us a value of type char * (and per above, the value is 0xAB0000).
We add 1, which adds enough bytes to get to the next element in memory. Here the pointer type is char *, so we're adding 1 * sizeof (char) (which is just 1 * 1, which is just 1) to the raw pointer value.
We end up with a char * whose value is 0xAB0001.
We then try to assign a pointer to a plain char, which is not valid. But we could dereference the pointer with * and fetch the value stored there:
char x = *(someCharArray + 1); // 'b'
... or we could use a pointer variable:
char *ptr = someCharArray + 1; // 0xAB0001 in this example

Related

How do array pointers work in C and how to change pointers

I'm very new to C and I have trouble understanding array pointers. I'm trying to make a array bigger,I copy all of its element to new bigger array but I can't make original variable to point the new array. I'm use to C# where you can do
double[] array1 = new double[5];
double[] array2 = new double[10];
array1 = array2;
I did something similar using int array
int array1 [5];
int array2 [10];
*array1 = &array2;
and it compile but crash the program. Same lines but double or char[] (I was told to use char[] instead of sting in C) do not even compile
[Error] incompatible types when assigning to type 'double' from type 'double (*)[(sizetype)(newsize)]'
The results I found on the topic told me to use double* array1 for variable type but this change the interactions with that variable.
If someone can explain the concept to me or at least tell me what to search for that will be huge help.
I do know the basics of pointers!
There are a few things you need to know about arrays (and pointers):
The first is that arrays and pointers are two different things;
The second is that an array can decay to a pointer to its first element. So if you use array1 (from your example) when a pointer is expected, that's the same as doing &array1[0]. The type of such a pointer is a pointer to a single element type (so for array1 the type will be int *);
The third thing is that for any array of pointer a and index i, the expression a[i] is exactly equal to *(a + i). That means *array1 (again from your example) is the same as array1[0] (*array1 is equal to *(array1 + 0) which is equal to array1[0]);
An array will have a fixed size. Once defined the size of an array can't change;
Lastly when you get a pointer to an array (as in &array2) then you get a pointer to the actual array, not to one of its elements. The type of e.g. &array2 is int (*)[10].
Now we can puzzle together the statement
*array1 = &array2;
If we do the array-indexing replacement for *array1 then we get
array[0] = &array2;
And here we can see a big problem: The type of a single element of array1 is a plain int. So what the assignment is trying to do is to assign a pointer to an array (of type int (*)[10]) to a single int.
If you want to copy all the elements from one array to another, then use the memcpy function. You're not allowed to assign between arrays.
But beware of the different sizes for array1 and array2. If you go out of bounds of an array (or other allocated memory) you will have undefined behavior.
In C there is no way to make an array variable "reference" a different variable. If you need to use "references" they can be emulated using pointers:
int *pointer1 = array1; // array1 here will decay to &array[0]
int *pointer2 = array2; // Same here for array2
With the above definition pointer1 is (in a way) "referencing" array1. You can now use pointer1 and array1 almost interchangeably.
One major difference between using pointers and arrays is how their sizes are calculated: When you do sizeof on an array you get the size (in bytes) of the whole array. Assuming 32-bit int (the most common) then sizeof array1 will return 5 * 4 (or 20) as the size. If you get the size of a pointer, you get the size of the pointer itself, not what it might point to. So sizeof pointer1 will return either 4 or 8 (depending on if you're in a 32-bit or 64-bit system).
Going back to references, we can now change where pointer1 is pointing:
pointer1 = pointer2; // Assuming pointer2 is unchanged, equivalent to pointer1 = array2
Now pointer1 and pointer2 are pointing to the same array, array2.
In C# you can overload the = to copy the arrays. In C it is just simple assignment.
In C arrays decays to pointers for the sake of simplicity. In C *(array + N) == array[N] and *array == array[0]
int array1 [5]; it is not the array of pointers only integers so *array1 = &array2; assigns array[0] with address of the first element of the the array2 converted to signed integer which generally doesn't make too much sense and it does not copy array2 to array
To copy array you need to use memcpy or the loop to copy the element. You need to make sure that the destination array is large enough to accommodate the second array. C will not change the destination array size.
The assignments that your are doing is wrong. Basically a pointer points to a block of memory. from your code I can understand that array1 = array2; and *array1 = &array2; is wrong.
Syntax in C is something like this data-type* pointer-variable = (data-type*)malloc(no. of bytes you want);
See consider you want 10 block of memory of type int
int *p = (int *)malloc(10 * sizeof(int))
sizeof(int) return 4 bytes.
Now p points to 10 * 4 = 40 bytes of memory, I multiplied by 4 because int is usually of 4 bytes and double is of 8 bytes and so on.
Follow this link to understand C - Data Types
Now regarding changing pointers refer below example and read the comments
int *q = NULL // declare a pointer of same type as the block of memory it is going to point
q = p; //now q and p point same memory of 40 bytes, means value at q[0] is equal to p[0]
When you have an integer pointer and you increment it by p++ it will point to next memory location p[1], pointer will be exactly incremented by 4 bytes as int size is 4 bytes and for double it will be 8 bytes, for char it will be 1 byte and so on.
Now if you want to increase the size of dynamically allocated memory you can use realloc please follow this link to understand more.
Dynamic Memory Allocation in C
int *p = NULL;
// Dynamically allocate memory using malloc()
p = (int*)malloc(no. of bytes, sizeof(int));
// Dynamically re-allocate memory using realloc()
p = realloc(p, (no. of bytes) * sizeof(int));
// Avoid memory leaks
free(p);
Syntax in C++ is something like this data-type* pointer-variable = new data-type[size];
See consider you want 10 block of memory of type int
int *p = new int[10]
Just use new operator to allocate block of memory and use delete to free allocated memory to avoid memory leaks.follow this link
new and delete operators in C++ for dynamic memory
Or If you are looking for containers where you don't know how much memory should be allocated the use standard template library vector, it helps creating dynamic arrays.follow this link
Vector in C++ STL

Type casting the character pointer

I am from Java back ground.I am learning C in which i gone through a code snippet for type conversion from int to char.
int a=5;
int *p;
p=&a;
char *a0;
a0=(char* )p;
My question is that , why we use (char *)p instead of (char)p.
We are only casting the 4 byte memory(Integer) to 1 byte(Character) and not the value related to it
You need to consider pointers as variable that contains addresses. Their sole purpose is to show you where to look in the memory.
so consider this:
int a = 65;
void* addr = &a;
now the 'addr' contains the address of the the memory where 'a' is located
what you do with it is up to you.
here I decided to "see" that part of the memory as an ASCII character that you could print to display the character 'A'
char* car_A = (char*)addr;
putchar(*car_A); // print: A (ASCII code for 'A' is 65)
if instead you decide to do what you suggested:
char* a0 = (char)addr;
The left part of the assignment (char)addr will cast a pointer 'addr' (likely to be 4 or 8 bytes) to a char (1 byte)
The right part of the assignment, the truncated address, will be assigned as the address of the pointer 'a0'
If you don't see why it doesn't make sense let me clarify with a concrete example
Say the address of 'a' is 0x002F4A0E (assuming pointers are stored on 4 bytes) then
'*addr' is equal to 65
'addr' is equal to 0x002F4A0E
When casting it like so (char)addr this become equal to 0x0E.
So the line
char* a0 = (char)addr;
become
char* a0 = 0x0E
So 'a0' will end up pointing to the address 0x0000000E and we don't know what is in this location.
I hope this clarify your problem
First of all, p is not necessarily 4 bytes since it's architecture-dependent. Second, p is a pointer to an integer, a0 is a pointer to a character, not a character. You're taking a pointer pointing to an integer and casting it to a pointer to a character. There are few good reasons to do this. You could also cast the value to a character, but I can't imagine any reason for doing this either.
Pointers do not provide information whether they point to a single object of first object of an array.
Consider
int *p;
int a[5] = { 1, 2, 3, 4, 5 };
int x = 1;
p = a;
p = &x;
So having a value in the pointer p you can not say whether the value is the address of the first element of the array a or it is the address of the single object x.
It is your responsibility to interpret the address correctly.
In this expression-statement
a0=(char* )p;
the address of the extent of memory pointed to by the pointer p and occupied by an object of the type int (it is unknown whether it is a single object or the first object of an array) is interpreted as an address of an extent of memory occupied by an object of the type char. Whether it is a single object of the type char or the first object of a character array with the size equal to sizeof( int ) depends on your intention that is how you are going to deal with the pointer.

Difference between pointer and array in C [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 8 years ago.
I just confused with these codes.
int state[arraySize] = {};
And
int *state;
state = (int*)calloc(arraySize,sizeof(int));
//............
free(state);
What's the difference between them?
As I need to collect some figures for my program. And each time run with different variables which may affect the array size. So I cannot use an array immediately. But when I changed my array to the (2), the result of my code appears differently and sometimes seems to entered a dead cycle.
A pointer points to another area of memory. In your second example state is not an array, it points to an array. I.e. by following (or dereferencing) the pointer you find an array.
An array, on the other hand, is an area of memory large enough to accommodate some number of some type of element. In your first example state is a region of memory which may hold arraySize ints.
What's confusing is that in many instances you can name an array, and a pointer to the first element of the array will be the result. This is the array-to-pointer decay.
C-FAQ 6.2:
The array declaration char a[6] requests that space for six characters be set aside, to be known by the name a. That is, there is a location named a at which six characters can sit. The pointer declaration char *p, on the other hand, requests a place which holds a pointer, to be known by the name p. This pointer can point almost anywhere: to any char, or to any contiguous array of chars, or nowhere.
As usual, a picture is worth a thousand words. The declarations
char a[] = "hello";
char *p = "world";
would initialize data structures which could be represented like this:
In case of
int *state = calloc(arraySize,sizeof(int));
a chunk of memory is allocated on heap. state is now pointing to the first memory cell of that chunk. The rest of the memory cell can be visited by using pointer arithmetic. If state is pointing at memory address 0x100 then state + 1 will point 0x104 (taking int of 4 bytes).
state -----> Points to the first memory cell of the allocated memory block.
state + 1 -----> Points to the second memory cell of the allocated memory block.
state + 2 -----> Points to the third memory cell of the allocated memory block.
...
...
You should note that: Pointer arithmetic and array indexing are equivalent in C, pointers and arrays are different.
As already stated a pointer is simply a region of memory that can be referenced that holds the address of another location in memory (or the 'heap' to be more precise) that contains actual data. An array on the other hand is an aggregate data type meaning that it contains a collection of elements (individual cubbyholes of the same data type) normally in contiguous regions of memory. The first element of the array is the pointer to the beginning of the structure. Let's put all of this into some code.
Pointer
There are multiple ways that pointers can be used depending on how flexible you want them to be. The simplest way is to have a pointer point to a variable, say
int a = 1;
int* a_ptr = &a;
int b = *a_ptr; /* b is initialized to 1 via the pointer to a. */
Array
An array, on the other hand is also a very simple concept. Bare in mind that the address of the first element in the array is the stating address of the overall structure.
When we access an array we use the conventional notation:
int someArr[3] = {0, 2, 4};
someArr[0]; /* 0 */
someArr[1]; /* 2 */
someArr[2]; /* 4 */
but we can also use the regular pointer dereferencing syntax to access an array:
*(someArr); /* 0 */
*(someArr + 1); /* 2 */
*(someArr + 2); /* 4 */
which clearly exposes how closely related arrays are to pointers. As an explanation of the last section of code:
*(someArr + n) where 'n' is the number of bytes of array type that is advanced in memory to retrieve the value pointed to, so:
*(someArr + n) is the same as *(someArr + (n * [size of array element type])) to find the value of array element 'n'.
This looks more like C than C++. The differences are:
The first one is of a "fixed" size during its lifetime. C99 allows arraySize to be a variable but it is still "automatic" in the call-stack.
The second one is a pointer and the call of calloc points it to some allocated memory which will remain in storage until the call to free. That free could take place after the function has returned.
the first state is an r-value. You cannot assign state to point somewhere else. You cannot safely return it from your function either.
If you take sizeof(state) on both you will also see that the first will be sizeof(int)*arraySize the second will be the standard size of a pointer. If arraySize is 50 and sizeof(int) is 4 and sizeof(int *) is 8 say, the first will be 200 the second 8.
The first one the array is fully controlled by the scope. It will allocate the needed memory correctly and when the variable goes out of escope the memory will be freely automatically.
The second one you have to control the memory by yourself. It does not have a defined scope, although if you do not destroy it when it gets out of reach, it will leave a memory leak.
To help you more about the dead cycle, I need more info about what you are doing with the array or array-pointer.

C - semantics to do with pointers

A very short question; how does one refer to 'names' below?
char *names[] = {
"Alan", "Frank",
"Mary", "John", "Lisa"
};
Is 'names' (a)a pointer to an array of strings?, or (b)an array of pointers to strings?
I've noticed that if the address of "Alan"(and 'names') is x, then the address of "Frank" is x+(0x08), the address of "Mary" is x+(0x10), and so on. So for this reason I'm leaning more towards (a).
As intro first of all: There are not "string"s in C.
What in C commonly is called a "string" really is an array of characters (chars) with at least one of it's elements carrying a '\0', the "string"-terminator, also known as the 0-termination marking the end of the "string".
So a "string" is a char array like this:
char s[42] = "alk"; /* with char[0] == 'a', char[1] == 'l', char[2] == 'k' and char[4] == '\0' */
This declares s to be an array of char, which could carry 42 chars, that may be a "string" with a maximum length of 41 chars, as one char needs to carry the terminator (see above).
You mentioned two types in your question:
(a) a pointer to an array of strings
(b) an array of pointers to strings
Refering (a):
A pointer to an array of string needs to have an array of strings to point to:
Let's define it:
char stringarray[3][42]; /* An array of 3 "string"s, each with a max length of 41+'\0'-terminator. */
A pointer to this would be:
char (*pstringarray)[3][42] = &stringarray; /* A pointer to an array of 3 elements of "string"s with a max length of 41 (see above). */
Referring (b):
An array of pointers to "string"s needs some "string"s to point to:
char s1 = "alk";
char s2 = "football";
char s3 = "champion";
Now lets define the array of pointers to "string"s pointing to the "string"s defined above:
char * pointerarray[4] = {
s1,
s2,
s3
}
The latter is equal to:
char * pointerarray[] = {
"alk",
"football",
"champion",
NULL
}
with the 1st three elements referring to some char-arrays ("string"s) and the last element carrying NULL(the null-pointer-value) to indicate the end of the array. Please note: In the first example the latter, the 4th element, the 4th pointer is implcitily set to all 0s, as if an initialiser misses to provide values for what it is initialising those (missed) bytes are all set to to 0.
Conclusion
The OP's example matches proposal (b), so it's: an array of pointers to "string"s.
char* means Pointer to character
[] means array
char* name[] means array of pointers to characters
String in C is an array of characters. For the string-literal like "Alan" the compiler adds a 0x0 at the end.
For performance reasons, the compiler aligns the reserved memory on the register width (32-bit = 4-byte). That's why the actual string "Alan"+ 0x0 requires 8 bytes instead of 5.
EDIT
Sorry for confusion!
I think the confusion was: address of "Alan"(and 'names') is x
Address of "Alan" is not the address of names! Address of pointer to "Alan" after initialization is the address of names.
If you define such a variable, the compiler does three things:
allocates 2 different blocks of memory:
One for holding the pointers in the array = 5 * sizeof(char*)
Another for storing the literals ("Alan", ...)
initializes the pointers with proper addresses of the data in initial values.
names[0] = start of "Alan"-string etc.
The pointers are of cause aligned in the array - apparently for your machine 8-byte wise (64-Bit) If you mean by address the address of the pointer, then of cause the difference will be always 8 (on your machine)

C strings confusion

I'm learning C right now and got a bit confused with character arrays - strings.
char name[15]="Fortran";
No problem with this - its an array that can hold (up to?) 15 chars
char name[]="Fortran";
C counts the number of characters for me so I don't have to - neat!
char* name;
Okay. What now? All I know is that this can hold an big number of characters that are assigned later (e.g.: via user input), but
Why do they call this a char pointer? I know of pointers as references to variables
Is this an "excuse"? Does this find any other use than in char*?
What is this actually? Is it a pointer? How do you use it correctly?
thanks in advance,
lamas
I think this can be explained this way, since a picture is worth a thousand words...
We'll start off with char name[] = "Fortran", which is an array of chars, the length is known at compile time, 7 to be exact, right? Wrong! it is 8, since a '\0' is a nul terminating character, all strings have to have that.
char name[] = "Fortran";
+======+ +-+-+-+-+-+-+-+--+
|0x1234| |F|o|r|t|r|a|n|\0|
+======+ +-+-+-+-+-+-+-+--+
At link time, the compiler and linker gave the symbol name a memory address of 0x1234.
Using the subscript operator, i.e. name[1] for example, the compiler knows how to calculate where in memory is the character at offset, 0x1234 + 1 = 0x1235, and it is indeed 'o'. That is simple enough, furthermore, with the ANSI C standard, the size of a char data type is 1 byte, which can explain how the runtime can obtain the value of this semantic name[cnt++], assuming cnt is an integer and has a value of 3 for example, the runtime steps up by one automatically, and counting from zero, the value of the offset is 't'. This is simple so far so good.
What happens if name[12] was executed? Well, the code will either crash, or you will get garbage, since the boundary of the array is from index/offset 0 (0x1234) up to 8 (0x123B). Anything after that does not belong to name variable, that would be called a buffer overflow!
The address of name in memory is 0x1234, as in the example, if you were to do this:
printf("The address of name is %p\n", &name);
Output would be:
The address of name is 0x00001234
For the sake of brevity and keeping with the example, the memory addresses are 32bit, hence you see the extra 0's. Fair enough? Right, let's move on.
Now on to pointers...
char *name is a pointer to type of char....
Edit:
And we initialize it to NULL as shown Thanks Dan for pointing out the little error...
char *name = (char*)NULL;
+======+ +======+
|0x5678| -> |0x0000| -> NULL
+======+ +======+
At compile/link time, the name does not point to anything, but has a compile/link time address for the symbol name (0x5678), in fact it is NULL, the pointer address of name is unknown hence 0x0000.
Now, remember, this is crucial, the address of the symbol is known at compile/link time, but the pointer address is unknown, when dealing with pointers of any type
Suppose we do this:
name = (char *)malloc((20 * sizeof(char)) + 1);
strcpy(name, "Fortran");
We called malloc to allocate a memory block for 20 bytes, no, it is not 21, the reason I added 1 on to the size is for the '\0' nul terminating character. Suppose at runtime, the address given was 0x9876,
char *name;
+======+ +======+ +-+-+-+-+-+-+-+--+
|0x5678| -> |0x9876| -> |F|o|r|t|r|a|n|\0|
+======+ +======+ +-+-+-+-+-+-+-+--+
So when you do this:
printf("The address of name is %p\n", name);
printf("The address of name is %p\n", &name);
Output would be:
The address of name is 0x00005678
The address of name is 0x00009876
Now, this is where the illusion that 'arrays and pointers are the same comes into play here'
When we do this:
char ch = name[1];
What happens at runtime is this:
The address of symbol name is looked up
Fetch the memory address of that symbol, i.e. 0x5678.
At that address, contains another address, a pointer address to memory and fetch it, i.e. 0x9876
Get the offset based on the subscript value of 1 and add it onto the pointer address, i.e. 0x9877 to retrieve the value at that memory address, i.e. 'o' and is assigned to ch.
That above is crucial to understanding this distinction, the difference between arrays and pointers is how the runtime fetches the data, with pointers, there is an extra indirection of fetching.
Remember, an array of type T will always decay into a pointer of the first element of type T.
When we do this:
char ch = *(name + 5);
The address of symbol name is looked up
Fetch the memory address of that symbol, i.e. 0x5678.
At that address, contains another address, a pointer address to memory and fetch it, i.e. 0x9876
Get the offset based on the value of 5 and add it onto the pointer address, i.e. 0x987A to retrieve the value at that memory address, i.e. 'r' and is assigned to ch.
Incidentally, you can also do that to the array of chars also...
Further more, by using subscript operators in the context of an array i.e. char name[] = "..."; and name[subscript_value] is really the same as *(name + subscript_value).
i.e.
name[3] is the same as *(name + 3)
And since the expression *(name + subscript_value) is commutative, that is in the reverse,
*(subscript_value + name) is the same as *(name + subscript_value)
Hence, this explains why in one of the answers above you can write it like this (despite it, the practice is not recommended even though it is quite legitimate!)
3[name]
Ok, how do I get the value of the pointer?
That is what the * is used for,
Suppose the pointer name has that pointer memory address of 0x9878, again, referring to the above example, this is how it is achieved:
char ch = *name;
This means, obtain the value that is pointed to by the memory address of 0x9878, now ch will have the value of 'r'. This is called dereferencing. We just dereferenced a name pointer to obtain the value and assign it to ch.
Also, the compiler knows that a sizeof(char) is 1, hence you can do pointer increment/decrement operations like this
*name++;
*name--;
The pointer automatically steps up/down as a result by one.
When we do this, assuming the pointer memory address of 0x9878:
char ch = *name++;
What is the value of *name and what is the address, the answer is, the *name will now contain 't' and assign it to ch, and the pointer memory address is 0x9879.
This where you have to be careful also, in the same principle and spirit as to what was stated earlier in relation to the memory boundaries in the very first part (see 'What happens if name[12] was executed' in the above) the results will be the same, i.e. code crashes and burns!
Now, what happens if we deallocate the block of memory pointed to by name by calling the C function free with name as the parameter, i.e. free(name):
+======+ +======+
|0x5678| -> |0x0000| -> NULL
+======+ +======+
Yes, the block of memory is freed up and handed back to the runtime environment for use by another upcoming code execution of malloc.
Now, this is where the common notation of Segmentation fault comes into play, since name does not point to anything, what happens when we dereference it i.e.
char ch = *name;
Yes, the code will crash and burn with a 'Segmentation fault', this is common under Unix/Linux. Under windows, a dialog box will appear along the lines of 'Unrecoverable error' or 'An error has occurred with the application, do you wish to send the report to Microsoft?'....if the pointer has not been mallocd and any attempt to dereference it, is guaranteed to crash and burn.
Also: remember this, for every malloc there is a corresponding free, if there is no corresponding free, you have a memory leak in which memory is allocated but not freed up.
And there you have it, that is how pointers work and how arrays are different to pointers, if you are reading a textbook that says they are the same, tear out that page and rip it up! :)
I hope this is of help to you in understanding pointers.
That is a pointer. Which means it is a variable that holds an address in memory. It "points" to another variable.
It actually cannot - by itself - hold large amounts of characters. By itself, it can hold only one address in memory. If you assign characters to it at creation it will allocate space for those characters, and then point to that address. You can do it like this:
char* name = "Mr. Anderson";
That is actually pretty much the same as this:
char name[] = "Mr. Anderson";
The place where character pointers come in handy is dynamic memory. You can assign a string of any length to a char pointer at any time in the program by doing something like this:
char *name;
name = malloc(256*sizeof(char));
strcpy(name, "This is less than 256 characters, so this is fine.");
Alternately, you can assign to it using the strdup() function, like this:
char *name;
name = strdup("This can be as long or short as I want. The function will allocate enough space for the string and assign return a pointer to it. Which then gets assigned to name");
If you use a character pointer this way - and assign memory to it, you have to free the memory contained in name before reassigning it. Like this:
if(name)
free(name);
name = 0;
Make sure to check that name is, in fact, a valid point before trying to free its memory. That's what the if statement does.
The reason you see character pointers get used a whole lot in C is because they allow you to reassign the string with a string of a different size. Static character arrays don't do that. They're also easier to pass around.
Also, character pointers are handy because they can be used to point to different statically allocated character arrays. Like this:
char *name;
char joe[] = "joe";
char bob[] = "bob";
name = joe;
printf("%s", name);
name = bob;
printf("%s", name);
This is what often happens when you pass a statically allocated array to a function taking a character pointer. For instance:
void strcpy(char *str1, char *str2);
If you then pass that:
char buffer[256];
strcpy(buffer, "This is a string, less than 256 characters.");
It will manipulate both of those through str1 and str2 which are just pointers that point to where buffer and the string literal are stored in memory.
Something to keep in mind when working in a function. If you have a function that returns a character pointer, don't return a pointer to a static character array allocated in the function. It will go out of scope and you'll have issues. Repeat, don't do this:
char *myFunc() {
char myBuf[64];
strcpy(myBuf, "hi");
return myBuf;
}
That won't work. You have to use a pointer and allocate memory (like shown earlier) in that case. The memory allocated will persist then, even when you pass out of the functions scope. Just don't forget to free it as previously mentioned.
This ended up a bit more encyclopedic than I'd intended, hope its helpful.
Editted to remove C++ code. I mix the two so often, I sometimes forget.
char* name is just a pointer. Somewhere along the line memory has to be allocated and the address of that memory stored in name.
It could point to a single byte of memory and be a "true" pointer to a single char.
It could point to a contiguous area of memory which holds a number of characters.
If those characters happen to end with a null terminator, low and behold you have a pointer to a string.
char *name, on it's own, can't hold any characters. This is important.
char *name just declares that name is a pointer (that is, a variable whose value is an address) that will be used to store the address of one or more characters at some point later in the program. It does not, however, allocate any space in memory to actually hold those characters, nor does it guarantee that name even contains a valid address. In the same way, if you have a declaration like int number there is no way to know what the value of number is until you explicitly set it.
Just like after declaring the value of an integer, you might later set its value (number = 42), after declaring a pointer to char, you might later set its value to be a valid memory address that contains a character -- or sequence of characters -- that you are interested in.
It is confusing indeed. The important thing to understand and distinguish is that char name[] declares array and char* name declares pointer. The two are different animals.
However, array in C can be implicitly converted to pointer to its first element. This gives you ability to perform pointer arithmetic and iterate through array elements (it does not matter elements of what type, char or not). As #which mentioned, you can use both, indexing operator or pointer arithmetic to access array elements. In fact, indexing operator is just a syntactic sugar (another representation of the same expression) for pointer arithmetic.
It is important to distinguish difference between array and pointer to first element of array. It is possible to query size of array declared as char name[15] using sizeof operator:
char name[15] = { 0 };
size_t s = sizeof(name);
assert(s == 15);
but if you apply sizeof to char* name you will get size of pointer on your platform (i.e. 4 bytes):
char* name = 0;
size_t s = sizeof(name);
assert(s == 4); // assuming pointer is 4-bytes long on your compiler/machine
Also, the two forms of definitions of arrays of char elements are equivalent:
char letters1[5] = { 'a', 'b', 'c', 'd', '\0' };
char letters2[5] = "abcd"; /* 5th element implicitly gets value of 0 */
The dual nature of arrays, the implicit conversion of array to pointer to its first element, in C (and also C++) language, pointer can be used as iterator to walk through array elements:
/ *skip to 'd' letter */
char* it = letters1;
for (int i = 0; i < 3; i++)
it++;
In C a string is actually just an array of characters, as you can see by the definition. However, superficially, any array is just a pointer to its first element, see below for the subtle intricacies. There is no range checking in C, the range you supply in the variable declaration has only meaning for the memory allocation for the variable.
a[x] is the same as *(a + x), i.e. dereference of the pointer a incremented by x.
if you used the following:
char foo[] = "foobar";
char bar = *foo;
bar will be set to 'f'
To stave of confusion and avoid misleading people, some extra words on the more intricate difference between pointers and arrays, thanks avakar:
In some cases a pointer is actually semantically different from an array, a (non-exhaustive) list of examples:
//sizeof
sizeof(char*) != sizeof(char[10])
//lvalues
char foo[] = "foobar";
char bar[] = "baz";
char* p;
foo = bar; // compile error, array is not an lvalue
p = bar; //just fine p now points to the array contents of bar
// multidimensional arrays
int baz[2][2];
int* q = baz; //compile error, multidimensional arrays can not decay into pointer
int* r = baz[0]; //just fine, r now points to the first element of the first "row" of baz
int x = baz[1][1];
int y = r[1][1]; //compile error, don't know dimensions of array, so subscripting is not possible
int z = r[1]: //just fine, z now holds the second element of the first "row" of baz
And finally a fun bit of trivia; since a[x] is equivalent to *(a + x) you can actually use e.g. '3[a]' to access the fourth element of array a. I.e. the following is perfectly legal code, and will print 'b' the fourth character of string foo.
#include <stdio.h>
int main(int argc, char** argv) {
char foo[] = "foobar";
printf("%c\n", 3[foo]);
return 0;
}
One is an actual array object and the other is a reference or pointer to such an array object.
The thing that can be confusing is that both have the address of the first character in them, but only because one address is the first character and the other address is a word in memory that contains the address of the character.
The difference can be seen in the value of &name. In the first two cases it is the same value as just name, but in the third case it is a different type called pointer to pointer to char, or **char, and it is the address of the pointer itself. That is, it is a double-indirect pointer.
#include <stdio.h>
char name1[] = "fortran";
char *name2 = "fortran";
int main(void) {
printf("%lx\n%lx %s\n", (long)name1, (long)&name1, name1);
printf("%lx\n%lx %s\n", (long)name2, (long)&name2, name2);
return 0;
}
Ross-Harveys-MacBook-Pro:so ross$ ./a.out
100001068
100001068 fortran
100000f58
100001070 fortran

Resources