### Thread: Using sizeof to decide length of array

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2

#### Using sizeof to decide length of array

Here's the code:
Code:
```#include <stdio.h>
#define SIZE ((int) (sizeof(a) / sizeof(a[0])))

int main(void)
{
int i, a[] = {0};

printf("Enter a few numbers: ");
for (i = 0; i < SIZE; i++)
scanf("%d", &a[i]);

printf("\nIn reverse order: ");
for (i = SIZE - 1; i >= 0; i--)
printf("%d\n", a[i]);

return 0;
}```
What I am trying to do with this program, is to print out the digits entered in reverse order. I don't have a specific initial array length. It should be determined automatically with the number of digits entered as input. So I am trying to use this code for this job:
Code:
`((int) (sizeof(a) / sizeof(a[0])))`
But when I provide an input, it just prints out random garbage value. What am I doing wrong and what should be done to serve my purpose?
2. It should be determined automatically with the number of digits entered as input.
I fear you did something other instead.

"sizeof" allows one to get the size of the variable. So your expression (sizeof(a)/sizeof(a[0])) is the division of the size of existing array (in bytes) by size of its element (in bytes).

For example, if you declare it as a[5], the expression will be 20/4=5.

So you see, this only helps to find the declared size of array.

You declare array as "int a[] = {0}" - empty brackets mean that size should be determined from the number of initializers - and you have only one initializer - so the size of array is 1.

For 1 element it looks like working correctly.

If you want arrays of dynamic size you should do something like this:
- determine how many elements you will need;
- declare array as a pointer int*a;
- allocate memory for it a = (int*) malloc(n * sizeof(int));
- and don't forget remove it later with "free".

If you have no idea about how many elements would be entered, you are either to reallocate array as soon as it is needed, or use linked list.

In C++ you can do allocation with "new" and also you can use std::list for similar cases.

(however all this is bit clumsy - that is why after 10 years of programming in C I at last turned to java and scripting languages)
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2
Originally Posted by rodiongork
I fear you did something other instead.

"sizeof" allows one to get the size of the variable. So your expression (sizeof(a)/sizeof(a[0])) is the division of the size of existing array (in bytes) by size of its element (in bytes).

For example, if you declare it as a[5], the expression will be 20/4=5.

So you see, this only helps to find the declared size of array.

You declare array as "int a[] = {0}" - empty brackets mean that size should be determined from the number of initializers - and you have only one initializer - so the size of array is 1.

For 1 element it looks like working correctly.

If you want arrays of dynamic size you should do something like this:
- determine how many elements you will need;
- declare array as a pointer int*a;
- allocate memory for it a = (int*) malloc(n * sizeof(int));
- and don't forget remove it later with "free".

If you have no idea about how many elements would be entered, you are either to reallocate array as soon as it is needed, or use linked list.

In C++ you can do allocation with "new" and also you can use std::list for similar cases.

(however all this is bit clumsy - that is why after 10 years of programming in C I at last turned to java and scripting languages)
How do I use a linked list? And I haven't reached pointers or memory allocation yet in my textbook :(
4. About pointers. I think currently it is important only to keep in mind that in C pointers are like arrays for which memory is not reserved beforehand:

Code:
```int*a;
int b[100];

//later they both behave similarly:
a[5] = 8;
b[5] = 8;```
the main difference is that for using pointer as array one needs to find for it some memory chunk and assign its address to pointer (we could either allocate this memory or make the pointer point to existing array).

arrays which you define with square brackets are always preallocated, constant size. they could be regarded as pointers (to memory used by this arrays) with the only difference that such pointer could not be assigned to other memory address.

About linked list - I think that you can safely skip this just for now. It is interesting, but not crucial for your study right now :)

shortly speaking - C allows to group several variables into one structure, addressed by single variable or pointer (like the Person which contains first name, last name, age etc.)

Linked list is a collection of such structures each of which contains not only data variables, but also a pointer to the next-in-chain structure.