array size

R

RedLars

Given this generic object;
typedef struct
{
const char * name;
int address;

} Parameter;

I defined the following array of Parameters;
static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

Would this be the most robust \ safest way of getting the size of the
array ?
void foo(Parameter * parameter)
{
int arraySize = 0;
while (parameter->name != NULL) arraySize++;

}

Thanks
 
I

Ivan Gotovchits

RedLars said:
{
int arraySize = 0;
while (parameter->name != NULL) arraySize++;

}
This will return infinity or zero (if parameter was NULL)
look at this code:
{
int arraySize = 0; /* array size in bytes */
for(;parameter->name != NULL; parameter++) {
arraySize += sizeof(Parameter);
}
 
I

Ivan Gotovchits

Ivan said:
{
int arraySize = 0; /* array size in bytes */
for(;parameter->name != NULL; parameter++) {
arraySize += sizeof(Parameter);
}
}
forgotten ))
 
G

Gilles Chehade

Given this generic object;
typedef struct
{
const char * name;
int address;

} Parameter;

I defined the following array of Parameters;
static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

Would this be the most robust \ safest way of getting the size of the
array ?
void foo(Parameter * parameter)
{
int arraySize = 0;
while (parameter->name != NULL) arraySize++;

}

Thanks

Why not sizeof(para)/sizeof(Parameter) since your array is fixed ?

Gilles
 
G

Gilles Chehade

Why not sizeof(para)/sizeof(Parameter) since your array is fixed ?

Sorry I misread your declaration, this would be:
sizeof(para)/sizeof(Parameter) - 1;
 
M

Mark Bluemel

RedLars said:
Given this generic object;
typedef struct
{
const char * name;
int address;

} Parameter;

I defined the following array of Parameters;
static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

I don't think you did. That won't compile...

You should really use copy and paste to show us your source.

The code probably reads :-

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
Would this be the most robust \ safest way of getting the size of the
array ?


What's wrong with this?

sizeof para/sizeof(Parameter)
 
R

RedLars

Mark: Thank you for pointing out a flaw in the code;
static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

(Thats what happens when I try to type something based on memory)

Sorry for not explaining myself properly, using
sizeof para/sizeof(Parameter)

would give me the size of the variable para which is fine. However, I
would like to get the size of Parameter * passed to the function foo
(the definition of para is unknown to this function).

void foo(Parameter * parameter)
{

}

Appreciate any help.

RedLars said:
Given this generic object;
typedef struct
{
const char * name;
int address;
} Parameter;
I defined the following array of Parameters;
static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

I don't think you did. That won't compile...

You should really use copy and paste to show us your source.

The code probably reads :-

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};


Would this be the most robust \ safest way of getting the size of the
array ?

What's wrong with this?

sizeof para/sizeof(Parameter)
 
A

Army1987

Sorry for not explaining myself properly, using
sizeof para/sizeof(Parameter)

would give me the size of the variable para which is fine. However, I
would like to get the size of Parameter * passed to the function foo
(the definition of para is unknown to this function).

void foo(Parameter * parameter)
{

}
You can't. Simply pass the number of Parameter's as an argument:
void foo(Parameter *parameter, size_t n_of_pars);
 
T

Tor Rustad

RedLars wrote:

[...]
Would this be the most robust \ safest way of getting the size of the
array ?

I like this macro:

#define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
void foo(Parameter * parameter)

void foo(Parameter *param, size_t max_param)
 
P

pete

RedLars said:
Mark: Thank you for pointing out a flaw in the code;
static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

(Thats what happens when I try to type something based on memory)

Sorry for not explaining myself properly, using
sizeof para/sizeof(Parameter)

would give me the size of the variable para which is fine. However, I
would like to get the size of Parameter * passed to the function foo
(the definition of para is unknown to this function).

void foo(Parameter * parameter)
{

}

Appreciate any help.

RedLars said:
Given this generic object;
typedef struct
{
const char * name;
int address;
} Parameter;
I defined the following array of Parameters;
static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

I don't think you did. That won't compile...

You should really use copy and paste to show us your source.

The code probably reads :-

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};


Would this be the most robust \
safest way of getting the size of the array ?

What's wrong with this?

sizeof para/sizeof(Parameter)

/* BEGIN new.c */

#include <stdio.h>

typedef struct {
const char * name;
int address;
} Parameter;

void foo(Parameter * parameter, size_t nmemb);

int main(void)
{
static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

foo(para, sizeof para / sizeof *para);
return 0;
}

void foo(Parameter * parameter, size_t nmemb)
{
size_t index;

printf("The Parameter array has %u elements.\n", (unsigned)nmemb);
for (index = 0; index != nmemb; ++index) {
if (parameter[index].name != NULL) {
printf("%s ", parameter[index].name);
} else {
printf("NULL ");
}
printf("%d\n", parameter[index].address);
}
}
/* END new.c */
 
B

Barry Schwarz

Mark: Thank you for pointing out a flaw in the code;
static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

(Thats what happens when I try to type something based on memory)

Sorry for not explaining myself properly, using
sizeof para/sizeof(Parameter)

would give me the size of the variable para which is fine. However, I
would like to get the size of Parameter * passed to the function foo
(the definition of para is unknown to this function).

void foo(Parameter * parameter)
{

}

snip

Please don't top post. Place your response immediately after the
relevant original material you are responding to.

At the time foo is executing, parameter contains no information about
the size of the array para. In fact, it points to para[0] and there
need not be a para[1]. The recommended approach is to add a second
parameter to foo along the lines of
size_t number_of_elements_in_array

In the calling statement you could then pass the argument using
sizeof para / sizeof *para
since in that function para is a defined array.

If para were an allocated array, the above argument would not work.
You would have to keep track of how much space you allocated and pass
that value directly.


Remove del for email
 
R

RedLars

Please don't top post. Place your response immediately after the
relevant original material you are responding to.

Sorry about that.
At the time foo is executing, parameter contains no information about
the size of the array para. In fact, it points to para[0] and there
need not be a para[1]. The recommended approach is to add a second
parameter to foo along the lines of
size_t number_of_elements_in_array
So it's not possible to add sort of a dummy object at the end of the
array. Then inside foo(..) one could loop through the array, looking
for the dummy instance.

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

Just a thought..
 
J

Joachim Schmitz

RedLars said:
Please don't top post. Place your response immediately after the
relevant original material you are responding to.

Sorry about that.
At the time foo is executing, parameter contains no information about
the size of the array para. In fact, it points to para[0] and there
need not be a para[1]. The recommended approach is to add a second
parameter to foo along the lines of
size_t number_of_elements_in_array
So it's not possible to add sort of a dummy object at the end of the
array. Then inside foo(..) one could loop through the array, looking
for the dummy instance.

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

Just a thought..
This is indeed possible, and there are library functions that do exactly
this. It requires one more element in the array though (the end marker) and
also needs some code in the receiving function to count.

Bye, Jojo
 
K

Keith Thompson

RedLars said:
Please don't top post. Place your response immediately after the
relevant original material you are responding to.

Sorry about that.
At the time foo is executing, parameter contains no information about
the size of the array para. In fact, it points to para[0] and there
need not be a para[1]. The recommended approach is to add a second
parameter to foo along the lines of
size_t number_of_elements_in_array
So it's not possible to add sort of a dummy object at the end of the
array. Then inside foo(..) one could loop through the array, looking
for the dummy instance.

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

Just a thought..

Sure, you can do that. C strings use the same technique, using '\0'
to mark the end.

You can't directly pass arrays as parameters, so instead you pass a
pointer to the first element, which of course loses information about
the length. There are a number of ways to get that information into
the function. The most straightforward is to pass it as a separate
argument, but using a special terminator works too -- assuming the
terminator can't be a valid value. But it means you need to scan the
array to find the terminator, which may or may not cause performance
problems.
 
B

Barry Schwarz

Please don't top post. Place your response immediately after the
relevant original material you are responding to.

Sorry about that.
At the time foo is executing, parameter contains no information about
the size of the array para. In fact, it points to para[0] and there
need not be a para[1]. The recommended approach is to add a second
parameter to foo along the lines of
size_t number_of_elements_in_array
So it's not possible to add sort of a dummy object at the end of the
array. Then inside foo(..) one could loop through the array, looking
for the dummy instance.

static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

Just a thought..

This will work if there is never any situation where the (first)
member of your structure could have the value NULL legitimately. In
the case where the member is a pointer, this is often reasonable. But
what about the case where the members are all arithmetic values?
Unless you know that some member value is never legitimate (such as 0
or INT_MIN), it is hard to determine if the structure is really a
sentinel or just an array element with "strange" values.


Remove del for email
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top