Why is a[10] not equal to 1.1000?

  • Thread starter Thread starter nenyan
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the behavior of an array in C programming, specifically addressing why accessing an out-of-bounds index (a[10]) does not yield the expected value and instead returns zero. The scope includes technical explanations and clarifications regarding array indexing and memory allocation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants note that accessing a[10] is invalid since the declared array size is 10, allowing only indices 0 to 9.
  • One participant suggests that the output of a[10] being zero may be due to implementation specifics of the compiler.
  • Another participant reports that their code shows a[10] as 1.1000 when using a specific compiler, raising questions about compiler behavior.
  • Several participants emphasize that accessing a[10] leads to undefined behavior, as it refers to memory outside the allocated space for the array.
  • There is a suggestion that to legally access a[10], the array should be defined with a size of 11.

Areas of Agreement / Disagreement

Participants generally agree that accessing a[10] is invalid and leads to undefined behavior, but there are differing views on why it may return zero or another value depending on compiler specifics.

Contextual Notes

The discussion highlights the limitations of array indexing in C and the potential for undefined behavior when accessing out-of-bounds indices. There is no consensus on the specific behavior of different compilers regarding this issue.

Who May Find This Useful

This discussion may be useful for programmers learning about array management in C, particularly those interested in understanding memory allocation and the implications of accessing out-of-bounds indices.

nenyan
Messages
67
Reaction score
0
#include <stdio.h>
#include <stdlib.h>
int main()
{ double a[10];
int i=0;
for(i=0;i<10;i++)
a=1.1;
a[10]=1.1;
for(i=0;i<10;i++)
printf("a[%d]=%f, ", i, a);

printf("a[10]=%f, ", a[10]);
}

it shows
a[10]=0.000 why not 1.1000?
 
Technology news on Phys.org
a[10] doesn't exist. No idea why it is printed as zero, it must be implementation thing.
 
#include <stdio.h>
#include <stdlib.h>
int main()
{ double a[10];
int i=0;
for(i=0;i<10;i++)
a=1.1;
for(i=0;i<10;i++)
printf("a[%d]=%f, ", i, a);

a[10]=1.1;

printf("a[10]=%f, ", a[10]);
}

then
it shows a[10]=1.1000

I use gcc complier
 
You declared this variable a as "double a[10]". The only valid indices into "a" are 0 to 9. An index of 10 is invalid.
 
double a[10] allocates space for 10 doubles. You can access the 10 locations using
a[0], a[1], ... a[9].

If you use a[10], you are accessing a piece of memory doesn't "belong" to you. If the compiler uses that location for something else, then the data you wrote there will be lost. If the compiler doesn't use that location, then you got lucky.

If you want to use a[10] legally, you need to define the array as double a[11].
 
a[10] is beyond the end of the array which is on the stack. It's possible that the call to printf is storing a zero at that same location on the stack.
 
Thank you. I totally understand.

AlephZero said:
double a[10] allocates space for 10 doubles. You can access the 10 locations using
a[0], a[1], ... a[9].

If you use a[10], you are accessing a piece of memory doesn't "belong" to you. If the compiler uses that location for something else, then the data you wrote there will be lost. If the compiler doesn't use that location, then you got lucky.

If you want to use a[10] legally, you need to define the array as double a[11].
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
14
Views
4K
  • · Replies 17 ·
Replies
17
Views
12K