0

When attempting this (code below), the console doesn't even request an input value, then spits out a random number (likely from a number previously stored at the location).

Why does this not work and how can i fix it?

int main( ) {
    int arr[3];

    for(int i = sizeof(arr); i <= 0; i--) {
        scanf("%d", &arr[i]);
   }
   printf("%d", arr[2]);

   return 0;
}
3
  • for(int i = sizeof(arr); i <= 0; i--) --> for(int i = sizeof(arr) - 1; i >= 0; i--) Commented Jul 1, 2017 at 18:55
  • See stackoverflow.com/questions/7826605/… Commented Jul 1, 2017 at 18:56
  • 2
    sizeof(arr)? What do you think sizeof(arr) is in this case? Commented Jul 1, 2017 at 19:15

5 Answers 5

2

There are two things that are wrong.

Assuming you want the number of elements in the array, it is done, using:

size_t len = sizeof(arr) / sizeof(*arr)

sizeof gives the actual size (number of bytes allocated for arr.

  1. You should start with len - 1 and not len.

NOTE: Array indexing is 0 based not 1 based, so the array elements are indexed from 0 to 2 But you would have tried to access arr[3], which can result in undefined behaviour.

  1. You wrote i <= 0. So, i starts from let's say 2, is 2 <= 0 ? NO!

Hence it will never go inside the loop. The correct condition is i >= 0

int len = sizeof(arr) / sizeof(*arr);
for(int i = len - 1; i >= 0; i--)

Well, I don't know why you are taking reverse order input, but a general convention is to take input using:

size_t len = sizeof(arr)/sizeof(*arr);
for (i = 0; i < len; i++)
{
    // take input
}

EDIT: From other comments it seems that you don't understand the for loop.

Have a look in this answer

Please comment for any further clarification.

Sign up to request clarification or add additional context in comments.

5 Comments

I wrote, that the questioner "intended" to have that behaviour. And even if he did that, that would be wrong.
Two nitpicks here: 1.) sizeof returns size_t not int 2.) The preferred pattern to calculate the size of an array by the array itself is: char a[42]; size_t s = sizeof arr / sizeof *arr; For completeness: sizeofis an operator and not a function, no need for parenthesis, it does not get "called".
@alk Thanks! for your input. I have updated the answer. I have a doubt though. Assume somehow size_t len turns out to be 0 In that case if I do int i = len - 1;. What will i contain?
For any array T a[N]; this expression sizeof a / sizeof *a; will never by <1 by definition, as the definttion T a[0]; is not allowed by the C Standard. Recent versions of GCC allow it as an extension, but make it to have the size of T.
@alk Thanks for clarification. :)
1
i <= 0

the code can never enter the loop since the initial value of i is greater than zero.

3 Comments

The other problem is that sizeof returns the size in bytes, so i will start at (probably) 12 or 24.
Isn't that the terminator of the loop though?
@HazzaOb No, the middle part of a for loop is like the condition of a while loop: As long as it is true, the loop repeats.
0

It is important to note that in C, other than languages like Java/Python, you must explicitly know the length of the array, sizeof will NOT give you the amount of items in the array.

int main() {
    int arr[3];
    int itemsInArray = 3;

    for(int i = itemsInArray-1; i >= 0; i--) {
        scanf("%d", &arr[i]);
    }
    printf("%d", arr[2]);

   return 0;
};

1 Comment

It does give you the size of the array in bytes, though. sizeof arr / sizeof arr[0] would work fine.
0

Since i-- will decrease the value of i , and the condition for loop is i <=0 to start the loop the i must be 0 or negative.Since arr[3] will return 12(3 elements and each has 4 bytes(int has 4 bytes)), the value will be posivite,greater than 0 so we need to change the loop condition to check if i is positive

#include <stdio.h>

int main( ) {
    int arr[3]={0};
    int i = sizeof(arr)/sizeof(arr[0]) -1;

    for(; i >= 0; i--) {
        scanf("%d", &arr[i]);
   }
   printf("%d", arr[2]);

   return 0;
}

7 Comments

arr[3] won't return 3. arr[3] is not a valid element of the array (index 3 is out of bounds).
What is the "it" that's returning 0 in your question?
sizeof(arr) does not return 0. What are you talking about?
@melpomene I compiled again the code and returns 3 ,but few minutes ago it returned 0,strange.Anyway ,thanks a lot for contribution
No, sizeof(arr) will be 12 or 24 on common C implementations.
|
-2

There are a couple of issues with your code: first of all, sizeof(arr) won't return "3" as you probably thought; "arr" is a pointer to arr[0], so you are requesting the size of an int pointer.
Secondly, i <= 0 prevent the loop to even be executed.
Finally, please set array to zero while declarating, as best practice, ie:
int arr[3] = {0};

EDIT: i wrong-spelled my thoughts: you are requesting the size of the whole memory area allocated for the array.
Comments below are corrected though.

2 Comments

Nah. sizeof(anArray) (if its known as an array like here and not decayed to a pointer), returns the size of the whole array in bytes.
Is it necessary to do the third point as all the array elements are used only as lvalue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.