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 12 3682
"prashna" <va******@rediffmail.com> wrote in message
news:d4**************************@posting.google.c om...
| 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
-- http://ivan.vecerina.com
prashna wrote: 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 */
--
pete
In article <d4**************************@posting.google.com >, prashna wrote: 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).
--
Andreas Kähäri
On Fri, 10 Oct 2003 11:48:00 +0200, Ivan Vecerina wrote: | 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
"Zygmunt Krynicki" <zyga@_CUT_2zyga.MEdyndns._OUT_org> wrote in message
news:pan.2003.10.10.19.03.41.271708@_CUT_2zyga.MEd yndns._OUT_org... On Fri, 10 Oct 2003 11:48:00 +0200, Ivan Vecerina wrote:
| 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[i]=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
-- http://ivan.vecerina.com
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
On Fri, 10 Oct 2003 21:56:55 +0200, Ivan Vecerina wrote: 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[i]=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
On Fri, 10 Oct 2003 19:51:08 +0000, Default User wrote: 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.
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
Zygmunt Krynicki wrote: 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
"Zygmunt Krynicki" <zyga@_CUT_2zyga.MEdyndns._OUT_org> wrote in message
news:pan.2003.10.10.20.24.28.829682@_CUT_2zyga.MEd yndns._OUT_org... On Fri, 10 Oct 2003 21:56:55 +0200, Ivan Vecerina wrote: 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[i]=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.
template <typename T, int N>
T* arrayEnd(T (&a)[N]) { return a+N; }
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
-- http://ivan.vecerina.com
On Fri, 10 Oct 2003 23:23:45 +0200, "Ivan Vecerina"
<ivecATmyrealboxDOTcom> wrote: > // 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.
--
Al Balmer
Balmer Consulting re************************@att.net
On Fri, 10 Oct 2003 21:56:55 +0200, "Ivan Vecerina"
<ivecATmyrealboxDOTcom> wrote in comp.lang.c: "Zygmunt Krynicki" <zyga@_CUT_2zyga.MEdyndns._OUT_org> wrote in message news:pan.2003.10.10.19.03.41.271708@_CUT_2zyga.MEd yndns._OUT_org... On Fri, 10 Oct 2003 11:48:00 +0200, Ivan Vecerina wrote:
| 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michael Sgier |
last post by:
Hello
I want to replace a windows bitmap load function through an equivalent
SDL function.
I should extract the img height etc. and assign it to the terrainTex
array. I've also
three beginner...
|
by: junky_fellow |
last post by:
Consider an array.
char arr;
When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.
When we say arr+1, ---> arr is treated as a pointer to char.
Why is...
|
by: tarscher |
last post by:
Hi all,
2 array questions;
I have a multidimensional matrix.
C1 C2 C3 C4
R1
R2
R3
R4
R5
|
by: foker |
last post by:
I have this problem where I have 2 text files, one with student name,
id#, # of courses and course #, the second file has course name and
course number. I want to make a multidimensional array that...
|
by: robusto33 |
last post by:
Hi everyone, I'm really new to C#.net development, especially for win32 applications. I'm basically making a board game and was wondering if anyone could help me out with this predicament:
I have...
|
by: README |
last post by:
so what i need to do is implement a code that will take the maximum number in a array and put it into another array. events is an array that contains numbers that was extracted from a file. Count...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |