# trying to reverse numbers with sizeof operator

• 03-08-2008
JoelearningC
trying to reverse numbers with sizeof operator
Hi all,
I'm trying to write code that will result in reversing 10 numbers while defining the array size using the sizeof operator. I'm getting some errors and it doesn't reverse the numbers. The output asks me to enter -858993460 numbers instead of 10 and then I enter 10 numbers anyway and it yields 10 of whatever the first number is that I enter. Can someone help me with this code? Thank you! Here it is:

Code:

```#include <stdio.h> #define SIZE (sizeof(a) / sizeof(a[10])) int main () {         int a[10], i;         printf("Enter %d numbers:", i);         for (i=0; i<sizeof(a) / sizeof(a[10]); i++)         a[10] = 0;         scanf_s("%d", &a[i]);         printf("In reverse order:");         for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[10]);         printf("\n");         return 0; }```
• 03-08-2008
ting
Code:

```        for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[10] );         printf("\n");```
don't you mean

Code:

```        for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[i] );         printf("\n");```
• 03-08-2008
tabstop
Let's get out the red pen:

Quote:

Originally Posted by JoelearningC
Hi all,
I'm trying to write code that will result in reversing 10 numbers while defining the array size using the sizeof operator. I'm getting some errors and it doesn't reverse the numbers. The output asks me to enter -858993460 numbers instead of 10 and then I enter 10 numbers anyway and it yields 10 of whatever the first number is that I enter. Can someone help me with this code? Thank you! Here it is:

Code:

```#include <stdio.h> #define SIZE (sizeof(a) / sizeof(a[10]))  No such thing as a[10]! int main () {         int a[10], i;         printf("Enter %d numbers:", i);  Use of uninitialized variable!         for (i=0; i<sizeof(a) / sizeof(a[10]); i++)  What happened to SIZE?         a[10] = 0;  No such thing as a[10]!         scanf_s("%d", &a[i]);  This is not in your loop!         printf("In reverse order:");         for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[10]);  No such thing as a[10]!         printf("\n");         return 0; }```

• 03-08-2008
JoelearningC
can you check again?
Thanks for helping out. I'm still stuck here. I made some changes but it seems like it's even worse. Can you have another look? Thanks again.

Code:

```/* Reverses a series of numbers */ #include <stdio.h> #define SIZE (sizeof(a) / sizeof(a[i])) int main () {         int a[i], i;         i = 10;         printf("Enter %d numbers:", i);         for (i=0; i<SIZE); i++)         scanf_s("%d", &a[i]);         printf("In reverse order:");         for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[i]);         printf("\n");         return 0; }```
• 03-08-2008
tabstop
Down to one error, but it would lead (when compiling) to way more errors afterwards:
Quote:

Originally Posted by JoelearningC
Thanks for helping out. I'm still stuck here. I made some changes but it seems like it's even worse. Can you have another look? Thanks again.

Code:

```/* Reverses a series of numbers */ #include <stdio.h> #define SIZE (sizeof(a) / sizeof(a[i]))  Probably ok, provided i is always well defined int main () {         int a[i], i;  No such thing as a variable-length array!         i = 10;         printf("Enter %d numbers:", i);         for (i=0; i<SIZE); i++)         scanf_s("%d", &a[i]);         printf("In reverse order:");         for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[i]);         printf("\n");         return 0; }```

• 03-09-2008
vart
for (i=0; i<SIZE); i++)

red bracket should not be there
• 03-09-2008
whiteflags
The item you are looking for is

#define SIZE(a) (sizeof(a) / sizeof(a[0]))

Dividing the sizeof a local array by it's first element (or any real element -- zeroth one is simply easiest I suppose) results in the size of that array a. This comes from the byte layout of the array object in memory (which is known at compile time because it's automatic memory), and the size of the elements it is made out of (which is also known at compile time).

You can call it almost like a function.
• 03-09-2008
JoelearningC
Here's another try at it
Thanks everyone. I made some adjustments but it's still giving me errors. Here's the code again along with the errors. I don't understand why it's telling me SIZE and 'a' are undeclared. Thanks for the help.

Code:

```/* Reverses a series of numbers */ #include <stdio.h> #define SIZE(a) (sizeof(a) / sizeof(a[0])) main () {         int i;         i = 10;         printf("Enter %d numbers:", i);         for (i=0; i<SIZE; i++)         scanf_s("%d", &a[i]);         printf("In reverse order:");         for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[i]);         printf("\n");         return 0; }```
ERRORS:
(12) : error C2065: 'SIZE' : undeclared identifier
(13) : error C2065: 'a' : undeclared identifier
(13) : error C2109: subscript requires array or pointer type
(16) : error C2065: 'SIZE' : undeclared identifier
(17) : error C2065: 'a' : undeclared identifier
(17) : error C2109: subscript requires array or pointer type
• 03-09-2008
vart
And where is the array declaration?
• 03-09-2008
JoelearningC
Array declaration
That's just the thing. My book doesn't show how to do the array declaration for a sizeof operator. Can you suggest what it should look like and where it should go? Thanks.
• 03-09-2008
matsp
Like many beginners, I think you are attempting to change several things at once [or changing one thing, doesn't work, so you change another thing, without actually reverting the other change).

In this case, your first post has a perfectly fine array declaration.

The proposed SIZE macro now takes a parameter a - it would perhaps be more obvious if it's not called a, so that it's not confusing with your array called a, something like this:
Code:

`#define SIZE(x) (sizeof(x) / sizeof(x[0])`
--
Mats
• 03-09-2008
vart
Quote:

how to do the array declaration for a sizeof operator
You should declare array not "for sizeof operator" but for you - to store the data in it.
• 03-09-2008
JoelearningC
Thank you to all!!!
Thanks to everyone who helped me on this code. I got it to work and here's the final product. Thanks again. Joe

Code:

```#include <stdio.h> #define SIZE (sizeof(a) / sizeof(a[0])) main () {         int a[10], i;         i = 10;         printf("Enter %d numbers:", i);         for (i=0; i<SIZE; i++)         scanf_s("%d", &a[i]);         printf("In reverse order:");         for (i=SIZE-1; i>=0; i--)                 printf(" %d", a[i]);         printf("\n");         return 0; }```
• 03-09-2008
xuftugulus
A much cleaner solution would be to #define SIZE to be just a number.
Code:

```#define SIZE 10 int main() {     int a[SIZE];  /* 10 elements a[0]..a[9] */     int i;     for(i=0; i<SIZE; i++)     {         a[i] = 0;     }     .... }```
As a small example of declaring and using an array correctly.