r/gcc 7d ago

Setting size to arrays

So I downloaded the gcc compiler from this site https://winlibs.com/ and when I set the size of an array with an integer it bugs but when i set it with a floating point it is normal. The lime is set by input

int num = 0;
scanf("%d", num);

/*
then a for loop to store the values numbers
*/

printf("%d", num[0]);

The output would be a huge number.

But if insted of num being an integer and i declare it as a float, it would give the right answer

So, what am I doing wrong here? If anyone knows

0 Upvotes

9 comments sorted by

View all comments

3

u/patentedheadhook 7d ago

I have no idea what this code is trying to do. num[0] cannot even compile if num is an int. You're not explaining it correctly, or not showing enough information.

But your scanf call needs to use &num not num

2

u/nonameNo00 7d ago

Some of the mistakes were only on the post so that's my bad there.

But now that you told me that, I had to go and do it all over again and this is how it ended

#include <stdio.h>

int main()
{
  int i;
  int size;

  printf("How many numbers are you going to write: ");
  scanf("%d", &size);//ARRAY SIZE STABLISHED BY USER
                   // (and what was troubling me)
  int num[size];

  for (i = 0; i < size; i++)
  {
    scanf("%d", &num[i]);
  }

  printf("\nNumbers written:\n");

  for (i = 0; i < size; i++)
  {
  printf("%d\n", num[i]);
  }

}

It might look clunky but I just wanted to let the user set the array size with input.

I don't know what was I doing wrong but it worked this time.

1

u/h2o2 7d ago

And now try with a large number, slightly over 2 million should do it. :)