Thread: trying to reverse numbers with sizeof operator

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    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;
    }

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28
    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");

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let's get out the red pen:

    Quote Originally Posted by JoelearningC View Post
    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;
    }

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    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;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Down to one error, but it would lead (when compiling) to way more errors afterwards:
    Quote Originally Posted by JoelearningC View Post
    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;
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for (i=0; i<SIZE); i++)

    red bracket should not be there
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    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

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And where is the array declaration?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    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.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    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;
    }

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    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.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof operator
    By dhanulakshmi in forum C Programming
    Replies: 1
    Last Post: 12-01-2006, 07:09 AM
  2. Cutting up an std::string
    By Shamino in forum C++ Programming
    Replies: 26
    Last Post: 04-04-2006, 11:02 AM
  3. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM
  4. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 03:54 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM