array in prototype


C

cerr

Hi,

How legal is it to have a function like:

int switch_wan_set_ipv6addr(uint32 routeID,char ipv6addr[L3ADDR_LEN]) {
int i;

if (routeID > MAX_ROUTE_ENTRIES) {
printf("RouteId %d is out of range! [0-%d]", routeID, MAX_ROUTE_ENTRIES);
return ERROR;
}
for (i=0; i<L3ADDR_LEN; i++)
route_entry[routeID].ipv6addr = ipv6addr;
return OK;
}

L3ADDR_LEN being 16 and route_entry.ipv6addr being of type unsigned char bcm_ip6_t[16]

I'd expect the function the be called like

switch_wan_set_ipv6addr(0,"0123456789012345");

is this legal and perfectrly fine? Assume that the caller can't pass pointers as arguments.

Thanks,
Ron
 
Ad

Advertisements

J

James Kuyper

Hi,

How legal is it to have a function like:

int switch_wan_set_ipv6addr(uint32 routeID,char ipv6addr[L3ADDR_LEN]) {
int i;

if (routeID > MAX_ROUTE_ENTRIES) {
printf("RouteId %d is out of range! [0-%d]", routeID, MAX_ROUTE_ENTRIES);
return ERROR;
}
for (i=0; i<L3ADDR_LEN; i++)
route_entry[routeID].ipv6addr = ipv6addr;
return OK;
}

L3ADDR_LEN being 16 and route_entry.ipv6addr being of type unsigned char bcm_ip6_t[16]


That being the case, I'd recommend declaring the second parameter of
switch_wan_set_ipv6addr as "unsigned char", not "char".
I'd expect the function the be called like

switch_wan_set_ipv6addr(0,"0123456789012345");

is this legal and perfectrly fine? Assume that the caller can't pass pointers as arguments.

It's not possible to declare a C function as taking a parameter of array
type. That's because, in the parameter list of a function declaration, a
declaration of a parameter as an "array of T" gets automatically and
unavoidably converted into a "pointer to T". If the caller (presumably
NOT written in C?) cannot pass pointers, it can't call this function.

The closest you can get to doing what you want to do is to declare the
function as taking a parameter which has a struct type that contains an
array. Whether that is any help at all depends upon what language the
calling routine is written in.
 
C

cerr

Hi,
How legal is it to have a function like:
int switch_wan_set_ipv6addr(uint32 routeID,char ipv6addr[L3ADDR_LEN]) {
int i;

if (routeID > MAX_ROUTE_ENTRIES) {
printf("RouteId %d is out of range! [0-%d]", routeID, MAX_ROUTE_ENTRIES);
return ERROR;

for (i=0; i<L3ADDR_LEN; i++)
route_entry[routeID].ipv6addr = ipv6addr;

return OK;

L3ADDR_LEN being 16 and route_entry.ipv6addr being of type unsigned char bcm_ip6_t[16]




That being the case, I'd recommend declaring the second parameter of

switch_wan_set_ipv6addr as "unsigned char", not "char".


I'd expect the function the be called like



is this legal and perfectrly fine? Assume that the caller can't pass pointers as arguments.



It's not possible to declare a C function as taking a parameter of array

type. That's because, in the parameter list of a function declaration, a

declaration of a parameter as an "array of T" gets automatically and

unavoidably converted into a "pointer to T". If the caller (presumably

NOT written in C?) cannot pass pointers, it can't call this function.



The closest you can get to doing what you want to do is to declare the

function as taking a parameter which has a struct type that contains an

array. Whether that is any help at all depends upon what language the

calling routine is written in.



Hm, yeah, that is what I was worried about....
The caller is actually going to be the VzWorks shell... so do I need to expect 16 separate variables in the prototype?
 
K

Keith Thompson

cerr said:
How legal is it to have a function like:

int switch_wan_set_ipv6addr(uint32 routeID,char ipv6addr[L3ADDR_LEN]) {
int i;

if (routeID > MAX_ROUTE_ENTRIES) {
printf("RouteId %d is out of range! [0-%d]", routeID, MAX_ROUTE_ENTRIES);
return ERROR;
}
for (i=0; i<L3ADDR_LEN; i++)
route_entry[routeID].ipv6addr = ipv6addr;
return OK;
}

L3ADDR_LEN being 16 and route_entry.ipv6addr being of type unsigned
char bcm_ip6_t[16]


Perfectly legal, but it doesn't mean what you probably thnk it means.

The parameter ipv6addr is of type char*. The value of L3ADDR_LEN is
quietly ignored in the parameter declaration.
I'd expect the function the be called like

switch_wan_set_ipv6addr(0,"0123456789012345");

is this legal and perfectrly fine? Assume that the caller can't pass
pointers as arguments.

Your final assumption is incorrect. In the call you've shown, the
second argument *is* a pointer value.

Recommended reading: Section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

Incidentally, the "%d" format requires an argument of type int. Since
routeID is of type uint32 (why not uint32_t, which is defined in the
standard header <stdint.h>?), you need to change the format string
and/or cast the argument to the expected type. This may also apply to
MAX_ROUTE_ENTRIES, depending on how it's defined.
 
K

Keith Thompson

cerr said:
Hm, yeah, that is what I was worried about....
The caller is actually going to be the VzWorks shell... so do I need
to expect 16 separate variables in the prototype?

That depends on the capabilities of the VzWorks shell, which most of us
here are unlikely to know about.
 
K

Keith Thompson

James Kuyper said:
It's not possible to declare a C function as taking a parameter of array
type. That's because, in the parameter list of a function declaration, a
declaration of a parameter as an "array of T" gets automatically and
unavoidably converted into a "pointer to T". If the caller (presumably
NOT written in C?) cannot pass pointers, it can't call this function.

Quibble: The standard uses the word "adjusted" for the transformation of
an array parameter to a pointer parameter, emphasizing that it takes
place at compile time, not during execution (as most conversions do).
 
Ad

Advertisements

Ad

Advertisements

J

Jorgen Grahn

That depends on the capabilities of the VzWorks shell, which most of us
here are unlikely to know about.

BTW, I suppose he means the VxWorks shell ...

/Jorgen
 

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

Top