naive size of array question

S

sillyhat

Hello,

Can someone please help.

I have come across code similar to this which I prepared as an
example:-
/*-------------8<------------------*/
#define ASIZE 10
int main()
{
char *A[] = { "one", "two","three","four","five",
"six","seven","eight","nine","ten" };
int i;

for (i=0; i<ASIZE; i++)
puts(A);

return 0;
}
/*-------------8<------------------*/
and I would like to know how to code this so that ASIZE is calculated
at compile time - by macros perhaps.

Alternatively I could put an empty string in as the last entry and
ignore ASIZE but I wondered if there were other techniques to determine
the number of items in an array with variable length entries like this.

Thanks for any help given.
Hal
 
A

Antonio Contreras

Hello,

Can someone please help.

I have come across code similar to this which I prepared as an
example:-
/*-------------8<------------------*/
#define ASIZE 10
int main()
{
char *A[] = { "one", "two","three","four","five",
"six","seven","eight","nine","ten" };
int i;

for (i=0; i<ASIZE; i++)
puts(A);

return 0;
}
/*-------------8<------------------*/
and I would like to know how to code this so that ASIZE is calculated
at compile time - by macros perhaps.

Alternatively I could put an empty string in as the last entry and
ignore ASIZE but I wondered if there were other techniques to determine
the number of items in an array with variable length entries like this.

Thanks for any help given.
Hal


You can calculate the size with:

sizeof (A) / sizeof (A[0])

HTH
 
C

Christopher Benson-Manica

#define ASIZE 10
int main()
{
char *A[] = { "one", "two","three","four","five",
"six","seven","eight","nine","ten" };
}
and I would like to know how to code this so that ASIZE is calculated
at compile time - by macros perhaps.

You want sizeof A/sizeof *A. No macros are needed.
 
A

Alf P. Steinbach

* (e-mail address removed):
I have come across code similar to this which I prepared as an
example:-
/*-------------8<------------------*/
#define ASIZE 10
int main()
{
char *A[] = { "one", "two","three","four","five",
"six","seven","eight","nine","ten" };
int i;

for (i=0; i<ASIZE; i++)
puts(A);

return 0;
}
/*-------------8<------------------*/
and I would like to know how to code this so that ASIZE is calculated
at compile time - by macros perhaps.

Alternatively I could put an empty string in as the last entry and
ignore ASIZE but I wondered if there were other techniques to determine
the number of items in an array with variable length entries like this.


There are five commonly used ways, which differ in what they're good
for.

A very short comparision is provided at
<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/array_size.doc.pdf>.

Hth.,

- Alf
 
F

Flash Gordon

Alf said:
* (e-mail address removed):
I have come across code similar to this which I prepared as an
example:-
/*-------------8<------------------*/
#define ASIZE 10
int main()
{
char *A[] = { "one", "two","three","four","five",
"six","seven","eight","nine","ten" };

There are five commonly used ways, which differ in what they're good
for.

A very short comparision is provided at
<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/array_size.doc.pdf>.

Most of those ways are C++ not C.
 
M

Mike Wahler

Hello,

Can someone please help.

I have come across code similar to this which I prepared as an
example:-
/*-------------8<------------------*/
#define ASIZE 10
int main()
{
char *A[] = { "one", "two","three","four","five",
"six","seven","eight","nine","ten" };
int i;

for (i=0; i<ASIZE; i++)
puts(A);

return 0;
}
/*-------------8<------------------*/
and I would like to know how to code this so that ASIZE is calculated
at compile time - by macros perhaps.


That's exactly what you're doing already. The *macro* 'ASIZE'
generates the integer constant value 10 at compile time.
Alternatively I could put an empty string in as the last entry and
ignore ASIZE but I wondered if there were other techniques to determine
the number of items in an array with variable length entries like this.

I think you're asking about determining an array's size at run time,
not compile time.

The size in bytes is:
sizeof A;

The size in elements is:
sizeof A / sizeof *A;

But be warned that this only works in the scope where the array
is defined. Passed to a function, an array's name is converted
to a pointer to the first element, so sizeof will give the pointer's
size, not the array's size.

-Mike
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top