473,507 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

length of array

Hi,
I write the following code:

void someproc() {
int controlids [] = {
IDC_BUTTON_RC_CONNECT,
IDC_BUTTON_RC_CONNECTPUBLIC,
IDC_BTN_SENDHELLO };

someproc2(controlids);
}

void someproc2(int * e)
{
int size = sizeof(e)/sizeof(int);
//.. do something with size.. like use in a for loop etc...

}

I expected to get the correct size, but it just gives me 1. What am I
doing wrong?

regards,

X.

Mar 16 '06 #1
9 2007
because void someproc2(int * e) is wrong
in the scope e is changed to point type !
so int size = sizeof(e)/sizeof(int); size will == 1

Mar 16 '06 #2
So what should the signature of someproc2 be to make things right?

Mar 16 '06 #3
because array formal argument decays to a pointer to its first element
and the array bound is ignored.
so, IMHO, the only way to achieve your purpose is
void someproc() {
int controlids [] = {
IDC_BUTTON_RC_CONNECT,
IDC_BUTTON_RC_CONNECTPUBLIC,
IDC_BTN_SENDHELLO };
someproc2(controlids, sizeof(controlids)/sizeof(controlids[0]));

}
void someproc2(int * e, int size)
{
//.. do something with size.. like use in a for loop etc...
}

Mar 16 '06 #4
Alright, thanks.

X.

Mar 16 '06 #5
pr****@googlemail.com wrote:
Hi,
I write the following code:

void someproc() {
int controlids [] = {
IDC_BUTTON_RC_CONNECT,
IDC_BUTTON_RC_CONNECTPUBLIC,
IDC_BTN_SENDHELLO };

someproc2(controlids);
}

void someproc2(int * e)
{
int size = sizeof(e)/sizeof(int);
//.. do something with size.. like use in a for loop etc...

}

I expected to get the correct size, but it just gives me 1. What am I
doing wrong?


Your expectations were met: you got the correct size.

Note the signature: void someproc2(int * e ). You say that e is a variable
of type int*. Well, the size of a pointer to int on your machine just
happens to be the same as the size of an int. Note that the size of an int*
says nothing about the length of the array to whose first element that
pointer is pointing.
Best

Kai-Uwe Bux

ps.: If you want to keep track of the length of an array, consider using
std::vector.
Mar 16 '06 #6
void someproc() {
int controlids [] = {
IDC_BUTTON_RC_CONNECT,
IDC_BUTTON_RC_CONNECTPUBLIC,
IDC_BTN_SENDHELLO };

someproc2(controlids);
}

void someproc2(int * e)
{
int size = sizeof(e)/sizeof(int);
//.. do something with size.. like use in a for loop etc...

}

Could use a template:
void someproc()
{
int controlids [] = {
IDC_BUTTON_RC_CONNECT,
IDC_BUTTON_RC_CONNECTPUBLIC,
IDC_BTN_SENDHELLO };

someproc2(controlids);
}

template<class T, unsigned i>
void someproc2(int (&array)[i])
{
int size = i;
}
-Tomás
Mar 16 '06 #7
chenchen wrote:
because void someproc2(int * e) is wrong
in the scope e is changed to point type !
so int size = sizeof(e)/sizeof(int); size will == 1


Please read the information below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 16 '06 #8
pr****@googlemail.com wrote:
So what should the signature of someproc2 be to make things right?


Please read the information below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 16 '06 #9
Py***********@gmail.com wrote:
because array formal argument decays to a pointer to its first element
and the array bound is ignored.


Please read the information below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 16 '06 #10

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

Similar topics

3
4161
by: Shawn Modersohn | last post by:
I've got <form name="form1> <select name="select1"> <option value="null" selected>Select A Team</option> <option value="Cougars">Cougars</option> <option value="Mavericks">Mavericks</option>...
4
1512
by: F Da Costa | last post by:
Hi, Small question re the use of an Array. I'v got an array with x rowObjects in it. When i get it the type of the container is Array and i can get a hold of the objects just fine. owever,...
7
39827
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
5
14097
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
15
6225
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
10
6664
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
1
57824
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
2
1921
by: stealth_spoof | last post by:
Hi People wondering if anyone can help me with a problem I'm having I'm trying to create an array with an unspecified length, the length is based on the result i get from another task in the code...
6
2684
by: foreverbored75 | last post by:
Hello All! I am just learning c++ in school and I have the following question: Is there a way for the user to input the length of an array (console application) without using another variable?...
14
16442
by: Richard Maher | last post by:
Hi, Can anyone please show me a regular expression (or function or other) for padding out a string to a fixed number of bytes? At the moment I've got a var initialized to N spaces, where 'N'...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
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,...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.