Why is a[10] not equal to 1.1000?

  • Thread starter nenyan
  • Start date
In summary, the conversation discusses the use of an array declared as "double a[10]" and accessing a[10] which is beyond the end of the array and may result in unexpected behavior. It is recommended to declare the array as "double a[11]" to safely access a[10].
  • #1
nenyan
67
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
  • #2
a[10] doesn't exist. No idea why it is printed as zero, it must be implementation thing.
 
  • #3
#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
 
  • #4
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.
 
  • #5
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].
 
  • #6
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.
 
  • #7
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].
 

Related to Why is a[10] not equal to 1.1000?

1. Why is a[10] not equal to 1.1000?

One possible reason for this is that a[10] may not be assigned a value of 1.1000. It is important to check the code or data to ensure that the correct value is being used.

2. Can you explain the difference between a[10] and 1.1000?

Yes, a[10] is a variable that can hold different values, while 1.1000 is a specific numerical value. They are not necessarily equal unless a[10] has been specifically assigned a value of 1.1000.

3. Why does a[10] have a different value than 1.1000?

The value of a[10] can be determined by the code or data that it is assigned to, while 1.1000 is a specific numerical value. If the code or data is not properly written or organized, it can result in a different value for a[10] than 1.1000.

4. Is there a way to change the value of a[10] to 1.1000?

Yes, if the code or data is written in a way that allows the value of a[10] to be changed, it can be assigned a value of 1.1000. However, it is important to understand the implications of changing the value and ensure it will not cause any errors in the code or data.

5. Why does a[10] have a different value each time I run my code?

This could be due to a variety of factors, such as the use of random numbers or the code being affected by other variables or inputs. It is important to carefully analyze the code and any potential external factors to determine the reason for the varying values of a[10].

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
822
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
949
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top