473,698 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 stupid array questions!!

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
Nov 13 '05 #1
12 3707
"prashna" <va******@redif fmail.com> wrote in message
news:d4******** *************** ***@posting.goo gle.com...
| 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


Nov 13 '05 #2
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
Nov 13 '05 #3
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
Nov 13 '05 #4
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
Nov 13 '05 #5
"Zygmunt Krynicki" <zyga@_CUT_2zyg a.MEdyndns._OUT _org> wrote in message
news:pan.2003.1 0.10.19.03.41.2 71708@_CUT_2zyg a.MEdyndns._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<typena me 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


Nov 13 '05 #6
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
Nov 13 '05 #7
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<typena me 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

Nov 13 '05 #8
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

Nov 13 '05 #9
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
Nov 13 '05 #10

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

Similar topics

3
4586
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 questions: -Concerning that array why is texID used? terrainTex doesn't state the first element? What does terrainTex represent? Especially the "0". -How do i transfer terrainTex back to the calling terrain.cpp. the
18
2398
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 this anomalous behaviour ?
1
1509
by: tarscher | last post by:
Hi all, 2 array questions; I have a multidimensional matrix. C1 C2 C3 C4 R1 R2 R3 R4 R5
29
3298
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 takes the student id into one portion and the course id into the other. This is my code: include <iostream> #include <string> #include <fstream> using namespace std;
6
25805
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 a dynamically created array based on the size of the Board (13x13 or 19x19). I can make the array fine and position the pictureBoxes over the background, but I want to be able to change the properties of all the pictureBoxes based on a Click event. ...
9
1537
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 is the number of numbers in the array events because i did not know how many numbers i was reading. numDays is also an array.eventNumDays is the array for which i need to put the maximun number that numDays contains. but this is just not the any...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.