473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

naive size of array question

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
7 1497
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
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
Ouch!

It was even more naive than I realised!

Cheers!
Hal

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

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: terry | last post by:
Hi, Could anyone tell me how to determine the size of array of characters dynamically? For example, : : char *a={"hello","hi","kitty"}; char *b={"orange","apple"};
4
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
9
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
3
by: johan2sson | last post by:
The documentation for PyThreadState_SetAsyncExc says "To prevent naive misuse, you must write your own C extension to call this". Anyone care to list a few examples of such naive misuse? Johan
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
17
by: Christopher Benson-Manica | last post by:
Some recent posts got me thinking about how one might have dealt with simplistic malloc() implementations which might return NULL for a 64K request but might accept two 32K requests or four 16K...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.