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;
}
HazzaOb
  • 71
  • 5

5 Answers5

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.

NVS Abhilash
  • 574
  • 7
  • 24
  • I wrote, that the questioner "intended" to have that behaviour. And even if he did that, that would be wrong. – NVS Abhilash Jul 01 '17 at 20:19
  • 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: `sizeof`is an *operator* and ***not*** a *function*, no need for parenthesis, it does *not* get "called". – alk Jul 02 '17 at 07:47
  • @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? – NVS Abhilash Jul 02 '17 at 08:21
  • 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 Jul 02 '17 at 08:26
  • @alk Thanks for clarification. :) – NVS Abhilash Jul 02 '17 at 08:27
1
i <= 0

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

FredK
  • 4,094
  • 1
  • 9
  • 11
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;
}
O.Rares
  • 1,031
  • 1
  • 16
  • 19
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;
};
Pieter De Clercq
  • 1,951
  • 1
  • 17
  • 29
  • 4
    It does give you the size of the array in bytes, though. `sizeof arr / sizeof arr[0]` would work fine. – melpomene Jul 01 '17 at 19:07
-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.

Federico
  • 221
  • 3
  • 11
  • 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. – tofro Jul 01 '17 at 19:02
  • Is it necessary to do the third point as all the array elements are used only as lvalue. – GAURANG VYAS Jul 01 '17 at 19:12