sizeof (pointer to array)/sizeof (p[0]) how to use ? 
Author Message
 sizeof (pointer to array)/sizeof (p[0]) how to use ?

I have :
struct TFuncPrm
{
 LPCTSTR szPrmName;
 bool bMandatory;
 LPCTSTR szDefault;
Quote:
};

const static TFuncPrm str_call_3log[]=
{
 { "line"     ,false, "" },
 { "line_type",false, "" },
 { "mmu_id"   ,false, "" },
Quote:
};

const TFuncPrm *pFuncName;
pFuncName = str_call_3log;

Now to make a loop i want
 int nCountOf = sizeof(pFuncName)/sizeof(pFuncName[0]);
 for (int iItem=0 ; iItem < nCountOf ; ++iItem)
 {
  m_lstPrms.InsertItem(iItem,pFuncName[iItem].szPrmName);
  m_lstPrms.SetItemText(iItem,1,pFuncName[iItem].szDefault);
 }
But sizeof(pFuncName) != sizeof(str_call_3log);

Any help will be appreciated
Alex.



Thu, 17 Jan 2002 03:00:00 GMT  
 sizeof (pointer to array)/sizeof (p[0]) how to use ?

pFuncName is a pointer. The size of a pointer is 4 bytes or so.

So when you do:
sizeof(pFuncName)/sizeof(pFuncName[0]);

you are trying to divide 4 bytes by   sizeof TFuncPrm which is
greater than 4 bytes.

I think what you want is:
sizeof(*pFuncName) //this will be sizeof str_call_3log
divided by sizeof(pFuncName[0])// size of TFuncPrm

or to put it another way:
sizeof (str_call_3log)/sizeof (TFuncPrm)

Quote:

>I have :
>struct TFuncPrm
>{
> LPCTSTR szPrmName;
> bool bMandatory;
> LPCTSTR szDefault;
>};
>const static TFuncPrm str_call_3log[]=
>{
> { "line"     ,false, "" },
> { "line_type",false, "" },
> { "mmu_id"   ,false, "" },
>};
>const TFuncPrm *pFuncName;
>pFuncName = str_call_3log;

>Now to make a loop i want
> int nCountOf = sizeof(pFuncName)/sizeof(pFuncName[0]);
> for (int iItem=0 ; iItem < nCountOf ; ++iItem)
> {
>  m_lstPrms.InsertItem(iItem,pFuncName[iItem].szPrmName);
>  m_lstPrms.SetItemText(iItem,1,pFuncName[iItem].szDefault);
> }
>But sizeof(pFuncName) != sizeof(str_call_3log);

>Any help will be appreciated
>Alex.

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.codeguru.com/    The website for Visual C++ programmers.


Thu, 17 Jan 2002 03:00:00 GMT  
 sizeof (pointer to array)/sizeof (p[0]) how to use ?

Quote:

>pFuncName is a pointer. The size of a pointer is 4 bytes or so.

>So when you do:
>sizeof(pFuncName)/sizeof(pFuncName[0]);

>you are trying to divide 4 bytes by   sizeof TFuncPrm which is
>greater than 4 bytes.

>I think what you want is:
>sizeof(*pFuncName) //this will be sizeof str_call_3log
>divided by sizeof(pFuncName[0])// size of TFuncPrm

sizeof(*pFuncName) is sizeof(TFuncPrm), because pFuncName is a pointer to
TFuncPrm.

Quote:
>or to put it another way:
>sizeof (str_call_3log)/sizeof (TFuncPrm)

That's what he needs, and to put it in terms of what he wanted to do:

 sizeof(str_call_3log)/sizeof(str_call_3log[0])

--
Doug Harrison

Visual C++ MVP



Thu, 17 Jan 2002 03:00:00 GMT  
 sizeof (pointer to array)/sizeof (p[0]) how to use ?
If you look into <afximpl.h> you will find the following macro
#define _countof(array) = sizeof(array)/sizeof(array[0])

Hope this helps.



Fri, 18 Jan 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. sizeof and arrays/pointers question

2. how to modify a pointer to an array one sizeof(int)

3. sizeof an array referenced by pointer

4. Determine length of array using sizeof

5. Using sizeof() on a parameter array

6. relation between sizeof(void*), sizeof(int)

7. sizeof (struct) ! = sizeof struct elements

8. sizeof without sizeof

9. sizeof a/sizeof *a

10. sizeof(foo) -1 or sizeof(foo)

11. sizeof(void*) versus sizeof(foo*)

12. sizeof(int) sizeof(long) etc.

 

 
Powered by phpBB® Forum Software