 # I am learning C in 2020 – Beginner – Understanding the size of pointer

Operation System : Windows 10

## Problem

### Goal

Be able to predict the number of bytes that will be allocated for an array, a pointer, integer, a single character etc.

No need for one.

## Solution

### Chars and pointer to strings

As shown below, 1 byte is used to store a character.

```char a = 'A';
printf("The size of char <a> is %ld, while the size of a char is %ld \n", sizeof(a), sizeof(char));

The size of char <a> is 1, while the size of a char is 1```

On the other hand, 8 bytes are used to store a pointer to a string or char array. Keep in mind that the string is stored in read-only memory. I looked up the bytes size of a pointer and apparently the value differs from operating system to operating system. In a 64 bit OS, a pointer occupies 8 bytes.

```char *b = "B";
printf("The size of a pointer variable <b> is %ld, while the size of a pointer to a string is %ld \n",
sizeof(b), sizeof(char *));

The size of a pointer variable <b> is 8, while the size of a pointer to a string is 8```

I also was curious to see if the size of a pointer is influenced by the length of the string/char array it points to. As indicated below, 8 bytes are still used to store the pointer. Thus I can assume the storage is independent of the length of the char array.

``` char *message = "My name is Comfort Ajala";
printf("The size of a pointer variable <message> is %ld, while the size of a pointer to a string
is %ld \n", sizeof(message), sizeof(char *));

The size of a pointer variable <message> is 8, while the size of a pointer to a string is 8```

### Array of Pointers to Strings

What if the pointer points to an array of read-only strings ? Does the pointer’s storage size change ? As shown below, the pointer variable size is 32 bytes, which points to an array of 4 read-only char arrays. After reading this articlE realised that this isn’t a single pointer to an entire array rather an array of pointers to strings. I misunderstood this. Each of the pointers point to the base of the read-only strings, thus each pointer is a pointer to a char living in read-only memory. This means the sizeof(friends) is essentially char * ( 8 bytes ) times length of friends ( 4).

``` char *friends = {
"Annie",
"Tobi",
"Karen",
"Fransika"};
printf("The size of a pointer variable <friends> is %ld, while the size of a pointer
to an array of read-only strings is %ld \n", sizeof(friends), sizeof(char *));

The size of a pointer variable <friends> is 32, while the size of a pointer to an array of read-only strings is 8```
```char *fruits[] = {"apple", "orange", "mango"};
printf("The size of a pointer variable <fruits> is %ld, while the size of a pointer to an
array of read-only strings is %ld \n", sizeof(fruits), sizeof(char *));

The size of a pointer variable <fruits> is 24, while the size of a pointer to an array of read-only strings is 8```

Based on the above results, the size of the enemies pointer array should be 16 ( 8 bytes times 2 ).

```char *enemies = {
"Lisa", "Patrick"};
printf("The size of a pointer variable <enemies> is %ld, while the size of a pointer
to an array of read-only strings is %ld \n", sizeof(enemies), sizeof(char *));

The size of a pointer variable <enemies> is 16, while the size of a pointer to an array of read-only strings is 8```

And the bestfriend pointer array should be 8 bytes.

```char *bestfriend = {"me"};
printf("The size of a pointer variable <bestfriend> is %ld, while the size of a pointer
to an array of read-only strings is %ld \n", sizeof(bestfriend), sizeof(char *));

The size of a pointer variable <bestfriend> is 8, while the size of a pointer to an array of read-only strings is 8```

### Array of chars

The snippet below should be clear. Since there are 14 characters in the string favdrink and 1 byte is used to store 1 char, then the number of bytes for the string storage = 13 + 1 byte for the null character.

```char favedrink[] = "mineral water";
printf("The size of a variable <favedrink> is %ld, while the size of an array characters is %ld \n",
sizeof(favedrink), sizeof(char));

The size of a variable <favedrink> is 14, while the size of an array characters is 1```

### Pointer to array of char arrays vs Char arrays

I know this is unnecessary, but I stumbled upon this and thought it would be interestings to delve into it.

The favecakes char arrays have similar contents, however when you check their sizes, they are completely different. I was shocked. The variable is simply an array of 3 char arrays not string literals. So let’s break this down.

Analysis time !

Size of char = 1

Length of array = 3

Length of individual char array = 100

How many chars spaces in “favcakes” = 3 * 100 * 1 byte= 300 bytes

This is the only explanation I could come up with.

```char favcakes = {"red velvet cake", "kasekuchen", "devil's chocolate cake"};
printf("The size of a pointer variable <favcakes> is %ld, while the size of a pointer to chars is %ld \n",
sizeof(favcakes), sizeof(char));

The size of the array variable <favcakes> is 300, while the size of a char is 1```

However, the pointer array is shown to have a size of 2400 bytes. Significantly larger than the above.

Analysis time !

Size of char * pointer = 8

Length of array = 3

Length of pointers in each array = 100

How many spaces for pointers in the favecakes variable = 8 bytes * 3 * 100 = 2400

Makes sense, oder ?

But who would need an array of pointers,with each pointer pointing to a char though ?

```char *favcakes = {"red velvet cake", "kasekuchen", "devil's chocolate cake"};
printf("The size of a pointer variable <favcakes> is %ld, while the size of a pointer to chars is %ld \n",
sizeof(favcakes), sizeof(char *));

The size of a pointer variable <favcakes> is 2400, while the size of a pointer to chars is 8```

### Integers

As you can see below, the number of bytes for storing an integer is 4.

```int favenumber = 2;
printf("The size of a  variable <favenumber> is %ld, while the size of an int  is %ld \n", sizeof(favenumber),
sizeof(int));
The size of a  variable <favenumber> is 4, while the size of an int  is 4```

Similar to a char * pointer, the int * pointer is stored in 8 bytes on my platform. This may differ on your system.

```int *notsofavenumber = 7;
printf("The size of a  variable <notsofavenumber> is %ld, while the size of an int  is %ld \n",
sizeof(notsofavenumber), sizeof(int *));
The size of a  variable <notsofavenumber> is 8, while the size of an int  is 8```

### Array of integers

Seeing that 4 bytes are used for storing a single integer on my platform, storing a list of 5 integers would require 4 * 5 bytes, 20 bytes.

```int evennumbers[] = {2, 4, 6, 8, 10};
printf("The size of a  variable <evennumbers> is %ld, while the size of an int  is %ld \n",
sizeof(evennumbers), sizeof(int));
The size of a  variable <evennumbers> is 20, while the size of an int  is 4```

A three integer array, 3 * 4 = 12 bytes.

```int oddnumbers[] = {3, 6, 9};
printf("The size of a  variable <oddnumbers> is %ld, while the size of an int  is %ld \n",
sizeof(oddnumbers), sizeof(int));
The size of a  variable <oddnumbers> is 12, while the size of an int  is 4```

While a two integer array, 2 * 4 = 8 bytes

```int numbersbyfive[] = {5, 10};
printf("The size of a  variable <numbersbyfive> is %ld, while the size of an int  is %ld \n",
sizeof(numbersbyfive), sizeof(int));
The size of a  variable <numbersbyfive> is 8, while the size of an int  is 4```

### Pointer to an Integer in an array

The pointer below does not point to the entire array, rather to the first element of the numbersbyfive integer array. Therefore we should expect the size of the pointer to be 8 bytes.

```int numbersbyfive[] = {5, 10};
int *numbersbyfive_ptr = numbersbyfive;
printf("The size of a pointer variable <numbersbyfive_ptr> is %ld, while the size of a pointer to an
int is %ld \n", sizeof(numbersbyfive_ptr), sizeof(int *));
The size of a pointer variable <numbersbyfive_ptr> is 8, while the size of a pointer to an int is 8```

### Array of pointers to integers

This should be already predictable. 5 * 8 bytes = 40 bytes.

```int * numbersbytens;
for(int i = 0; i < 5; i++){
int num = i;
numbersbytens[i] = #
}
printf("The size of a array pointer variable <numbersbytens> is %ld  while the size of a pointer to an
int is %ld \n", sizeof(numbersbytens), sizeof(int *));```

## Double pointers

Maybe I am dumb, but I genuinely expected the size of a double pointer to be 16 bytes. Jedoch, as shown below, a double pointer is still a pointer.

```char **b_db_ptr = &b; // char *b = "B";
printf("The size of double pointer variable <b_db_ptr> is %ld, while the size of a double pointer to
an char is %ld\n", sizeof(b_db_ptr), sizeof(char **));
The size of double pointer variable <b_db_ptr> is 8, while the size of a double pointer to an char is 8```
```char **message_db_ptr = &message; // char *message = "My name is Comfort Ajala";
printf("The size of double pointer variable <message_db_ptr> is %ld, while the size of a double pointer
to a read-only string is %ld\n", sizeof(message_db_ptr), sizeof(char **));
The size of double pointer variable <message_db_ptr> is 8, while the size of a double pointer to a
```char **friends_db_ptr = friends;
// char *friends = {"Annie","Tobi","Karen","Fransika"};
printf("The size of double pointer variable <friends_db_ptr> is %ld, while the size of a double
pointer to a char is %ld\n", sizeof(friends_db_ptr), sizeof(char **));
The size of double pointer variable <friends_db_ptr> is 8, while the size of a double pointer to a char is 8```
```int **favenumber_db_ptr = ¬sofavenumber;
// int *notsofavenumber = 7;
printf("The size of double pointer variable <favenumber_db_ptr> is %ld, while the size of a
double pointer to a int is %ld\n", sizeof(favenumber_db_ptr), sizeof(int **));
The size of double pointer variable <favenumber_db_ptr> is 8, while the size of a double pointer to a int is 8```

See you next time! Happy learning!

## Author Notes

This is most likely not the cleanest implementation, just a heads up.