Doubts on Array Size

H

Harry

Good Day To all,

When i am declaring a array for e.g char [45]....it means i am
declaring array of 45 characters each of which has a maximum,minimum
value limit or range...for example in VC++(compiler that i am using,it
has nothing to do with C++) char's range is -128 to +127....this is the
range for each char element.....

My doubt is what is the upper limit for the array size(45 is the size
here) and what decides it.....

In VC++,where char range is -128 to +127,am i allowed to declare array
size out of this range...for e.g char[3168].....some say that array
size is int and so it should be within the limits of int,but some say
that some compilers take the range of array size to be same as the
range of the array type...in my case the range for array size is same
as range of char(-128 to +127)........

Expecting your views....

Thanks in advance
 
J

J. J. Farrell

Harry said:
When i am declaring a array for e.g char [45]....it means i am
declaring array of 45 characters each of which has a maximum,minimum
value limit or range...for example in VC++(compiler that i am using,it
has nothing to do with C++) char's range is -128 to +127....this is the
range for each char element.....

My doubt is what is the upper limit for the array size(45 is the size
here) and what decides it.....

The compiler decides it, constrained by the relevant C Standard. For
example, the C99 Standard requires that the compiler must allow an
object to be at least 65535 bytes in size. Your object is an array of
char, so the compiler must let at least [65535] be valid. Many
compilers will allow it to be a lot bigger than this. This minimum size
applies only to "hosted" environments (that is, "normal" situations
where you are compiling a program to run under an operating system, as
opposed to where you are writing the OS or some code for an embedded
controller).
In VC++,where char range is -128 to +127,am i allowed to declare array
size out of this range...

Yes. There is no connection between the values which can be held in
the individual items in an array and the maximum size of the array.
for e.g char[3168].....some say that array
size is int and so it should be within the limits of int,but some say
that some compilers take the range of array size to be same as the
range of the array type...in my case the range for array size is same
as range of char(-128 to +127)........

Ignore everything these "someone"s say about C - they are all wrong. A
variable of type size_t must be able to hold the size of your array,
but the maximum size allowed for an array need not be the same as the
maximum value which can be held in a size_t.
 
M

mark_bluemel

Harry said:
Good Day To all,

When i am declaring a array for e.g char [45]....it means i am
declaring array of 45 characters each of which has a maximum,minimum
value limit or range...for example in VC++(compiler that i am using,it
has nothing to do with C++) char's range is -128 to +127....this is the
range for each char element.....

My doubt is what is the upper limit for the array size(45 is the size
here) and what decides it.....

I think you are confusing two concepts.

1) The range of valid index values for the array. As C indexes from 0,
a 45-element array has index values from 0 to 44.

2) The range of valid values which can be stored in an element of the
array. In this case, elements of the array are "plain" char items (by
"plain" I mean they are not qualified as either signed or unsigned) and
your compiler takes them as signed char items. So the range of valid
values which can be stored in any element is -128 to +127.

Does that clarify your question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,318
Members
48,002
Latest member
EttaPfeffe

Latest Threads

Top