By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,951 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,951 IT Pros & Developers. It's quick & easy.

naive size of array question

P: n/a
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[i]);

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

Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
si******@yahoo.com wrote:
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[i]);

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

Nov 21 '05 #2

P: n/a
si******@yahoo.com wrote:
#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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 21 '05 #3

P: n/a
Ouch!

It was even more naive than I realised!

Cheers!
Hal

Nov 21 '05 #4

P: n/a
* si******@yahoo.com:

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[i]);

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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 21 '05 #5

P: n/a
Alf P. Steinbach wrote:
* si******@yahoo.com:
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" };

<snip>
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 21 '05 #6

P: n/a
* Flash Gordon:
Most of those ways are C++ not C.


I'm sorry, wrong group.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 21 '05 #7

P: n/a

<si******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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[i]);

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
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.