-10

when I am compiling this small program I am getting different values as output, instead of getting numbers from 0 to 5. And the size of array is always 8. The different values I am getting are:

-981774704
32767
0
0
4195728
0

Any tips would be really valuable. Thank you

#include <stdio.h>

int main() {
    int array[10];
    int i;
    for (i = 0; i < 6; i++) {
        printf("%d\n", array[i]);
    }
    int z = sizeof(&array);
    printf("\n Size of array is %d", z);

    return 0;
}
5
  • 6
    you are not storing anything in the array before printing. what values do you expect other then garbage?? Commented Sep 26, 2014 at 13:16
  • 2
    You have not initialized the array. It is printing the garbage value. Commented Sep 26, 2014 at 13:16
  • 1
    Your program is TOO simple, the initialization of array is missing :) BTW, welcome to SO! Commented Sep 26, 2014 at 13:17
  • 1
    int z = sizeof(&array); returns the size of a pointer, 8 in your case. To get the size of the array, use int z = sizeof array;, which is likely 40 or 80 bytes. To get the number of elements in the array, use int z = sizeof array/sizeof array[0]; which is 10. in this case. Commented Sep 26, 2014 at 15:25
  • sizeof operator to determine the size of the type and size of the object( of the result of the expression). it is not vector#size. Commented Sep 26, 2014 at 22:12

4 Answers 4

3

You aren't assigning any values to the array, so you're getting the uninitialized values. You need to do something like array[0] = 5; //or some value etc.

If you want an array of size 8, with the numbers indexing the array stored in it, so {0, 1, 2, 3, 4, 5, 6, 7}, you could do something like:

int array[8];
for(int i = 0; i < 8; ++i)
{
    array[i] = i;
}
Sign up to request clarification or add additional context in comments.

Comments

2

the elements of the array are uninitialized therefore it's printing garbage value...first initialize the elements with values...you can do this...

#include<stdio.h>

int main()
{
  int array[10] ;
  int i;
  for (i = 0; i < 6; i++)
  {
    array[i]=i;
    printf("%d\n", array[i]);
  }
  int z = sizeof(&array);
  printf("\n Size of array is %d", z);

  return 0;
}

if you want the array to be initialized automatically then declare the array globally...
if you want to get the size of the whole array remove &

sizeof(array);

4 Comments

int z = sizeof(&array); : remove & or int z = sizeof(array)/sizeof(*array); (always 10! LOL)
thanks for explaining thoroughly. I am very new to C. but thanks
here sizeof(&array); is equivalent to sizeof(array[0]); if you want to get the size of the whole array remove & sizeof(array);
"sizeof(&array); is equivalent to sizeof(array[0]);" is at best mis-leading, at worst, it is wrong. Given int array[10]: sizeof(&array) returns the size of a pointer, typical values area 2, 4 or 8. sizeof(array[0]) returns the size of an int whose typical sizes are 2, 4 or8. Pointer size and int size are not specified as needing to be the same in C. They may or may not have the same value. More info
1

C will not initialize your array to any default value. When you create the array it will be full of garbage values (nothing relevant or defined). Always initialize your data using memset or something equivalnet.

So if you are expecting a value of 0 on a new array then initialize it like this:

memset(array, 0, sizeof(array));

1 Comment

int array[10] = {0};
1

you have to initialize your array first, memory may not be filled with 0 by default

2 Comments

@SamuelCaillerie It does answer the question. The solution is so simple, a complete answer can be written as "initialize your variables"...
@Lundin humm, you're right (in fact, I have flagged it without reading in details the question), but there were already other answers more complete before this one, so that I think this (and some others) are unnecessary there...

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.