length of array

P

prog00

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.
 
C

chenchen

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
 
P

Python.LeoJay

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...


}
 
K

Kai-Uwe Bux

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.
 
T

Tomás

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))
{
int size = i;
}


-Tomás
 
D

Default User

chenchen said:
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
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top