473,399 Members | 2,478 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,399 software developers and data experts.

size of an array (number of elements)

If I have a struct declared as :

struct A
{
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};
And I have an array of these structs, how can I determine the number of
items in the array. I need to be able to determine this since I have a
function with signature:

void foo(struct A array_[])
{
//Process each of the elements in the passed array
}
Jul 4 '07 #1
9 11900
Grey Alien wrote:
If I have a struct declared as :

struct A
{
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};
And I have an array of these structs, how can I determine the number of
items in the array.
In general, you can get the size of an array x by calculating
sizeof x / sizeof *x. However, see below.
I need to be able to determine this since I have a
function with signature:

void foo(struct A array_[])
{
//Process each of the elements in the passed array
}
Inside foo, you do not have any array of struct A. You have a pointer to
struct A. From this pointer, it is not possible to reconstruct the size of
any array it might point to. Either tell it directly, using an extra
parameter, or put some marker in struct A.
Jul 4 '07 #2
In article <2f*********************@bt.com>,
Grey Alien <gr**@andromeda.comwrote:
>If I have a struct declared as :
>struct A
{
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};
>And I have an array of these structs, how can I determine the number of
items in the array. I need to be able to determine this since I have a
function with signature:
>void foo(struct A array_[])
{
//Process each of the elements in the passed array
}

You can't do it inside the function; you have to pass the number
of elements in.

At the scope that declares the variable, you can use
sizeof(TheArray) / sizeof(TheArray[0])
(unless, that is, that TheArray is dynamically allocated storage.)
--
All is vanity. -- Ecclesiastes
Jul 4 '07 #3
Grey Alien wrote, On 04/07/07 19:24:
If I have a struct declared as :

struct A
{
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};
And I have an array of these structs, how can I determine the number of
items in the array.
That would be question 6.23 of the comp.lang.c FAQ were you dealing with
an array, but...
I need to be able to determine this since I have a
function with signature:

void foo(struct A array_[])
You should read question 6.4 of the comp.lang.c FAQ as well to see why
you do *not* have an array in here.
{
//Process each of the elements in the passed array
}
Change the signature to pass in the size of have a sentinal value at the
end of the array.
--
Flash Gordon
Jul 4 '07 #4
Grey Alien wrote:
>
If I have a struct declared as :

struct A {
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the
number of items in the array. I need to be able to determine this
since I have a function with signature:

void foo(struct A array_[]) {
//Process each of the elements in the passed array
}
You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 5 '07 #5
CBFalconer wrote, On 05/07/07 00:53:
Grey Alien wrote:
>If I have a struct declared as :

struct A {
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the
number of items in the array. I need to be able to determine this
since I have a function with signature:

void foo(struct A array_[]) {
//Process each of the elements in the passed array
}

You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.
Chuck, I don't know where you get 4 from (well, I can guess) in the
above since the OP wants to iterate over the array, not over the fields
in the struct. Looking at the struct definition does not give you any
clue about how many elements the array has.
--
Flash Gordon
Jul 5 '07 #6
CBFalconer <cb********@yahoo.comwrites:
Grey Alien wrote:
>If I have a struct declared as :

struct A {
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the
number of items in the array. I need to be able to determine this
since I have a function with signature:

void foo(struct A array_[]) {
//Process each of the elements in the passed array
}

You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.
Huh? That's the number of members in the structure. He was clearly
asking about the number of elements in an array of structures.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 5 '07 #7
Flash Gordon wrote:
CBFalconer wrote, On 05/07/07 00:53:
>Grey Alien wrote:
>>If I have a struct declared as :

struct A {
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the
number of items in the array. I need to be able to determine this
since I have a function with signature:

void foo(struct A array_[]) {
//Process each of the elements in the passed array
}

You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.

Chuck, I don't know where you get 4 from (well, I can guess) in
the above since the OP wants to iterate over the array, not over
the fields in the struct. Looking at the struct definition does
not give you any clue about how many elements the array has.
Yes it does. There is one double, one char array, one struct
(undefined), and one void* pointer. That makes 4 objects in the
struct to me.

If he wants to know how big the array of struct A is he will have
to pass that value in. It is not calculable in the called
function. That is a separate question.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 5 '07 #8
CBFalconer <cb********@yahoo.comwrites:
Flash Gordon wrote:
>CBFalconer wrote, On 05/07/07 00:53:
>>Grey Alien wrote:
If I have a struct declared as :

struct A {
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the
number of items in the array. I need to be able to determine this
since I have a function with signature:

void foo(struct A array_[]) {
//Process each of the elements in the passed array
}

You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.

Chuck, I don't know where you get 4 from (well, I can guess) in
the above since the OP wants to iterate over the array, not over
the fields in the struct. Looking at the struct definition does
not give you any clue about how many elements the array has.

Yes it does. There is one double, one char array, one struct
(undefined), and one void* pointer. That makes 4 objects in the
struct to me.

If he wants to know how big the array of struct A is he will have
to pass that value in. It is not calculable in the called
function. That is a separate question.
Yes, and it's the question he asked. "And I have an array of these
structs, how can I determine the number of items in the array."

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 5 '07 #9
CBFalconer wrote, On 05/07/07 07:32:
Flash Gordon wrote:
>CBFalconer wrote, On 05/07/07 00:53:
>>Grey Alien wrote:

If I have a struct declared as :

struct A {
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the
number of items in the array. I need to be able to determine this
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>since I have a function with signature:

void foo(struct A array_[]) {
//Process each of the elements in the passed array
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>}
You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.
Chuck, I don't know where you get 4 from (well, I can guess) in
the above since the OP wants to iterate over the array, not over
^^^^^^^^^^^^^^^^^^^^^^
>the fields in the struct. Looking at the struct definition does
not give you any clue about how many elements the array has.

Yes it does. There is one double, one char array, one struct
(undefined), and one void* pointer. That makes 4 objects in the
struct to me.
That is how many fields the *struct* has, NOT how many elements the
array has, which is what the OP asked. What makes you think iterating
over the array, as the OP stated was the requirement, and as I stated
above was the requirement, has anything to do with the number of fields
in the struct?
If he wants to know how big the array of struct A is he will have
to pass that value in. It is not calculable in the called
function. That is a separate question.
It is the question asked and answered by various others. You seem to be
the only person reading it as a request for the number of fields, so I
suggest it is you expecting the OP to be asking the wrong question and
reading what you expect, not what is there.

Reread the underlined bits.
--
Flash Gordon
Jul 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
5
by: No Spam | last post by:
This is probably an easy question, but I have not used C for quite some time: How do I determine the size of an array? More specifically, how do I determine the number of incoming parameters? ...
8
by: Jeff | last post by:
Hello everybody, I'm kind of new to C programming, but here's a little question. Usually, when you have an array of chars, you put a \0 at the end of it to terminate the string. That way, it is...
4
by: Angus Comber | last post by:
Hello If I do this: struct mystruct { long nKey; char szItem; };
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
6
by: reb | last post by:
Hi, How do i increase the size of the array without losing the data of that array in c#? thanks
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>
2
by: B Williams | last post by:
I have written a function to print my arrays and the sizes, but what I really like to do is to have the function to decided which array to store my value in using sizeof. Can someone point me in...
5
by: Dave Cullen | last post by:
I have a program that reads a delimited text file and splits the input string into string array elements: Dim Data(41) as String 'read semicolon delimited fields into string array Data =...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.