2 stupid array questions!!

P

prashna

Hi Guru's,
Here are my questions...

1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!

2)How to determine the size of the array which is passes as a
parameter to the function?
ex :
void foo(int arr[])
{
/*I want determine the size of the array "arr" in this function.I
tried *using "sizeof" operator and realised it is not going to work
as "arr" will be *treated as pointer to an int!!!!!!*/
}

Thanks in advance
 
I

Ivan Vecerina

| 1)Why does c allows an extra "," in array intialiser?Is there any
| advantage of this?
|
| ex: int arr[5]={1,2,3,4,5,};
| ^^Compiler does not give error for this!
It makes things easier for code generators.
No real benefit besides that...

| 2)How to determine the size of the array which is passes as a
| parameter to the function?
| ex :
| void foo(int arr[])
| {
| /*I want determine the size of the array "arr" in this function.I
| tried *using "sizeof" operator and realised it is not going to work
| as "arr" will be *treated as pointer to an int!!!!!!*/
| }
The size of the array needs to be passed as a second parameter.
No other way in C (C++ allows what you need, through templates...).

hth,
Ivan
 
P

pete

prashna said:
Hi Guru's,
Here are my questions...

1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!

2)How to determine the size of the array which is passes as a
parameter to the function?
ex :
void foo(int arr[])
{
/*I want determine the size of the array "arr" in this function.I
tried *using "sizeof" operator and realised it is not going to work
as "arr" will be *treated as pointer to an int!!!!!!*/
}

If you want foo to know the size,
then you have to give foo more information.

void foo(int arr[], size_t n_elem) /* number of elements */

void foo(int arr[], size_t bytes) /* size in bytes */
 
A

Andreas Kahari

Hi Guru's,
Here are my questions...

1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!

It simplifies code that generate C code.

Oh, and the 5 in [5] is not needed in your example.
2)How to determine the size of the array which is passes as a
parameter to the function?
[cut]


Change the interface of the function so that the caller also
sends the length of the array (i.e. add an extra int argument).
 
Z

Zygmunt Krynicki

| 2)How to determine the size of the array which is passes as a
| parameter to the function?
| ex :
| void foo(int arr[])
| {
| /*I want determine the size of the array "arr" in this function.I
| tried *using "sizeof" operator and realised it is not going to work
| as "arr" will be *treated as pointer to an int!!!!!!*/
| }
The size of the array needs to be passed as a second parameter.
No other way in C (C++ allows what you need, through templates...).

Offtopic since C++
I must disagree. Usage of a template in this case would be a total
overkill. I believe vector and other collections address this problem
better.

Regards
Zygmunt Krynicki
 
D

Default User

Zygmunt Krynicki wrote:
Offtopic since C++
I must disagree. Usage of a template in this case would be a total
overkill. I believe vector and other collections address this problem
better.


Of course, that just hides the templates from the user. They're still
there.




Brian Rodenborn
 
I

Ivan Vecerina

Zygmunt Krynicki said:
| 2)How to determine the size of the array which is passes as a
| parameter to the function?
| ex :
| void foo(int arr[])
| {
| /*I want determine the size of the array "arr" in this function.I
| tried *using "sizeof" operator and realised it is not going to work
| as "arr" will be *treated as pointer to an int!!!!!!*/
| }
The size of the array needs to be passed as a second parameter.
No other way in C (C++ allows what you need, through templates...).

Offtopic since C++
I must disagree. Usage of a template in this case would be a total
overkill. I believe vector and other collections address this problem
better.

What exactly is overkill in the following examples ?

// returns the size of the C array passed as a param
// This is safer than the sizeof(a)/sizeof(a[0]) trick...
template <typename T, int N> inline
int arraySize(T (&)[N]) { return N; }

// fills an array with a specified value
template<typename T, int N> inline
void fill( T (&array)[N], T const& value )
{ for(int i=0;i<N;++i) array=value; }

If code duplication is of concern (in the second case), you could
choose to call a back-end function that takes a pointer and
an array size as parameters -- and benefit from the template still.


Regards,
Ivan
 
Z

Zygmunt Krynicki

What exactly is overkill in the following examples ?

// returns the size of the C array passed as a param
// This is safer than the sizeof(a)/sizeof(a[0]) trick...
template <typename T, int N> inline
int arraySize(T (&)[N]) { return N; }

This can only be used in the scope of the declaration of the array you're
going to pass along.
// fills an array with a specified value
template<typename T, int N> inline
void fill( T (&array)[N], T const& value )
{ for(int i=0;i<N;++i) array=value; }


Don't reinvent the wheel: std::fill
If code duplication is of concern (in the second case), you could
choose to call a back-end function that takes a pointer and
an array size as parameters -- and benefit from the template still.

What is the benefit?
That I can type foo(a) instead of foo(a, n)?
I don't see that as a benefit but a inconveniance. It's illogical to
assume all routines need to operate on the whole set of data.
This is why most of STL routines use begin/end iterators as input.

Your code, interesting in its design, is not the kind of a solution
I would advice to anyone interested in C++, usage of plain C arrays
is a bad thing (to quote the appropriate FAQ) as it undermines your
code with all C-memory-handling related issues.

Regards
Zygmunt Krynicki
 
Z

Zygmunt Krynicki

Of course, that just hides the templates from the user. They're still
there.

I was not trying to say that templates are not present or that they are
bad in general. I was just objecting to using them in accompaniance with C
arrays as a method of obtaining the declared array size.

Regards

Zygmunt
 
D

Default User

Zygmunt said:
Don't reinvent the wheel: std::fill

Did you forget which newsgroup you are on?

Your code, interesting in its design, is not the kind of a solution
I would advice to anyone interested in C++, usage of plain C arrays
is a bad thing (to quote the appropriate FAQ) as it undermines your
code with all C-memory-handling related issues.


Seems like it. This is comp.lang.c.



Brian Rodenborn
 
I

Ivan Vecerina

Zygmunt Krynicki said:
What exactly is overkill in the following examples ?

// returns the size of the C array passed as a param
// This is safer than the sizeof(a)/sizeof(a[0]) trick...
template <typename T, int N> inline
int arraySize(T (&)[N]) { return N; }

This can only be used in the scope of the declaration of the array you're
going to pass along.

Yes. And it will produce a compile error when mis-used.
As stated in my comment, this is much safer than the classic sizeof trick.
// fills an array with a specified value
template<typename T, int N> inline
void fill( T (&array)[N], T const& value )
{ for(int i=0;i<N;++i) array=value; }


Don't reinvent the wheel: std::fill
If code duplication is of concern (in the second case), you could
choose to call a back-end function that takes a pointer and
an array size as parameters -- and benefit from the template still.

What is the benefit?
That I can type foo(a) instead of foo(a, n)?


When the size of an array is implicitly specified by an initializer-
list, n is not always known to the reader. So yes, I consider that
one can benefit from the use of such templates.
This is why this trick has often been mentioned in the literature,
and in newsgroups.
I don't see that as a benefit but a inconveniance. It's illogical to
assume all routines need to operate on the whole set of data.
This is why most of STL routines use begin/end iterators as input.

Even when using the STL, statically initialized arrays are useful,
be it just to initialize a C++ container from them.
I could have as well provided the following example:

// returns a pointer to the end of an array.
Your code, interesting in its design, is not the kind of a solution
I would advice to anyone interested in C++, usage of plain C arrays
is a bad thing (to quote the appropriate FAQ) as it undermines your
code with all C-memory-handling related issues.

There is no such thing as a one true way to code in C++.

Let me get back to your original comment: you said that the usage
of templates would be overkill. Why ?
If they provide a safer alternative to C macro tricks,
why should one avoid them ?

When I do some embedded programming, I feel my code can truly
benefit from some C++ techniques, including those that rely on
templates, even when standard library containers are not only
overkill, but totally of the question (i.e. w/ only 4Kb of RAM).


Kind regards,
Ivan
 
A

Alan Balmer

// returns the size of the C array passed as a param
// This is safer than the sizeof(a)/sizeof(a[0]) trick...
template <typename T, int N> inline
int arraySize(T (&)[N]) { return N; }

This can only be used in the scope of the declaration of the array you're
going to pass along.

Yes. And it will produce a compile error when mis-used.
As stated in my comment, this is much safer than the classic sizeof trick.

It *always* produces a compile-time error in C.

*Please* move this thread to a C++ group.
 
J

Jack Klein

Zygmunt Krynicki said:
| 2)How to determine the size of the array which is passes as a
| parameter to the function?
| ex :
| void foo(int arr[])
| {
| /*I want determine the size of the array "arr" in this function.I
| tried *using "sizeof" operator and realised it is not going to work
| as "arr" will be *treated as pointer to an int!!!!!!*/
| }
The size of the array needs to be passed as a second parameter.
No other way in C (C++ allows what you need, through templates...).

Offtopic since C++
I must disagree. Usage of a template in this case would be a total
overkill. I believe vector and other collections address this problem
better.

What exactly is overkill in the following examples ?

What is WRONG with your code is that this is comp.lang.c, and your
code is not C.

Either take it to email or to comp.lang.c++. C++ is OFF-TOPIC here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top