# different between pointers and pointer to arrays?

difference between --

char a[SIZE]; //Char array of size (SIZE)

char *a; //character pointer. Points towards a char (a).

char *a[SIZE]; // Array of pointer characters.

char *(a[SIZE]); // Pointer to an array of characters.

char (*a[SIZE]);

ARE char *a[SIZE]; and char (*a[SIZE]); EQUIVALENT ?? And we also say string a; is actually char *a;. So the later is a pointer to a character , which when initialised stores address of char a. So how it acts as a string? Isn't it just a variable storing address of a single character. Similar to int *i. Which is a pointer to an integer not an array of integers.

Very confused !!!!

``````char a[SIZE]; // declares an char array of size SIZE
char *a; // declares a char pointer

// each of these declare an array of char pointers of size SIZE
char *a[SIZE];
char (*a[SIZE]);
char *(a[SIZE]);
``````

ARE char *a[SIZE]; and char (*a[SIZE]); EQUIVALENT ?

Yes.

And we also say string a; is actually char *a;. So the later is a pointer to a character , which when initialised stores address of char a. So how it acts as a string?

Well, a string is just an array of chars that terminates with a the a '\0' (which is of course a char by itself).

Array elements are stored in contiguous places in memory. The address of an array (the whole memory block), whether that's a string or just a regular char array, is the same as the address of the first byte in the array which is also the same as the address of the first element in the array as well.

You may have now concluded how a variable of type `char *` is capable of storing an address of a string. And the answer, to make that clear, is that the address of a string is the same as the address of the first char in the string and thus, it is an address of a char. Therefore, a variable of type `char *` has no problem storing that.

Now the question is, if a `char *` variable stores an address of a char, how does the computer know whether this char is part of a string/an array or just a single char. And the short answer is that the computer doesn't bother caring about that.

What should care about that is the code that uses this `char *` variable. So if I have some function that expects an argument of type string (aka `char *`) and the client code passed a pointer to a char that is not '\0', then the behavior of my function will be undefined. Here's a quick example:

``````#include <stdio.h>
#include <string.h>

int main(void)
{
char dest;
char src = 'a';
strcpy(dest, &src); // NOTICE a pointer to a single char is passed
printf("%s\n", dest);
}
``````

Output:

``````aP_m�
``````

Isn't it just a variable storing address of a single character. Similar to int *i. Which is a pointer to an integer not an array of integers.

You should have learned now that a variable of a pointer type (let's call that `T *`) in general can point to either a value of type `T` or an array of `T` values.

Update: answering your questions in the comment

While using int array[Size]; we have to allot the size in advance. And while using int *array; we need not allocate the size..Is that true ?

Not really. When you declare an int array `int array[size]`, you get size * 4 bytes (typically on most 32-bit systems) allocated for you automatically.

When using an `int *` on the other hand, you may either set it to point to an address of already allocated piece of memory (whether this was allocated manually or automatically). For example:

``````int i = 10;
int *p = &i; // points to i
``````

or you may allocate memory for it manually using a function like `malloc`

``````int p0 = malloc(sizeof (int)); // allocate memory for an int and point to it
``````

If you chose the latter though, you'll need to manually free the memory that you've allocated manually.

If we are not allocating memory to the *array; pointer variable, how can we say that it will allocate contiguous memory,as the next memory address might be already occupied, as we have not reserved for the array.

As the answer to the previous question shows, a pointer in general or an int pointer in particular does not point to anything initially (does not have memory allocated for it).

Here's another thing: the word contiguous does NOT mean that multiple calls to a function like `malloc` allocate contiguous blocks in memory. It basically means that that each call allocates one block of the size provided as an argument. For example:

``````malloc(4 * sizeof (int));
``````

allocates a block for 4 int values (or you could say for an array of 4 ints). These ints will live contiguously in memory.

It is not guaranteed that multiple calls to a function like `malloc` will allocate contiguous blocks so you shouldn't consider that.

• 1. In the above example, while using strcpy , how can the address of a char be stored in a non pointer variable. 2. While using int array[Size]; we have to allot the size in advance. And while using int *array; we need not allocate the size..Is that true ? If we are not allocating memory to the *array; pointer variable, how can we say that it will allocate contiguous memory,as the next memory address might be already occupied, as we have not reserved for the array. Feb 10, 2015 at 4:42
• @PrateekPande please see the Update section in the answer!
– kzidane
Feb 10, 2015 at 4:56
• @PrateekPande sorry I didn't see the first question. Where do you think there is an address of a char that is stored in a non-pointer variable exactly?
– kzidane
Feb 10, 2015 at 5:41
• strcpy(dest, &src); usually we use strcpy(dest,src); Feb 10, 2015 at 16:53
• @PrateekPande yeah, `src` is a variable of type `char` here. `strcpy` takes two arguments each of which is a `char *`. I passed `&src` to pass to address of `src`.
– kzidane
Feb 10, 2015 at 18:23

In `char *a`, `a` is a variable storing the address of a single character. However, `char *a` is the same as `char a[]`, It is just assumed that other characters are stored in sequence with the value contained in `a`(unless terminated by `\0`).

`char (*a[SIZE])` is the same as `char *a[SIZE]`, which is an array of pointer variables.

• I get the char*a. But, then similarly int *a;, can be used as an array of integers equivalent to int a[SIZE];. Feb 8, 2015 at 14:57
• if chara is equivalent to char a[SIZE]. Then using chara; we can over come the disadvantage of having fixed size arrays. As we are not specifying the size in this, we can use as per requirement. But then what if the next address after that of (a) is already used for storing another element,as we are not allocating any memory at the begining. Feb 8, 2015 at 15:02