sizeof a char * [] passed to function 
Author Message
 sizeof a char * [] passed to function

Hi,
I've a function :
void myarray(char * r[]);
when this function recieves this array , i wanna know the
number of items in r. How do I do that ?

Thanx.



Sat, 26 Nov 2005 15:37:18 GMT  
 sizeof a char * [] passed to function

Quote:
>I've a function :
>void myarray(char * r[]);
>when this function recieves this array , i wanna know the
>number of items in r. How do I do that ?

You change the function to:

        void myarray(char * r[], int NumItems );

and pass the size with it.

unless you know something about the data that's being passed - such as
it is null terminated, in which case you can use strlen.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq



Sat, 26 Nov 2005 17:05:29 GMT  
 sizeof a char * [] passed to function
I knew I can pass the size along but thats not what I want
to do? Isnt there any way I could get it with a sizeof
operator?
What my confusion is that when I write:

char * array = {"cpp","C#"};

I can get the above arrays' size through sizeof
(array)/sizeof(array[0])

WHY cant I get the size this way when I recieve a char *
[] inside a function?????
Or is there any other way?

Thanx :)

Quote:
>-----Original Message-----
>>I've a function :
>>void myarray(char * r[]);
>>when this function recieves this array , i wanna know
the
>>number of items in r. How do I do that ?

>You change the function to:

>    void myarray(char * r[], int NumItems );

>and pass the size with it.

>unless you know something about the data that's being
passed - such as
>it is null terminated, in which case you can use strlen.

>Dave
>--
>MVP VC++ FAQ: http://www.mvps.org/vcfaq
>.



Sat, 26 Nov 2005 17:37:57 GMT  
 sizeof a char * [] passed to function

Quote:

> I knew I can pass the size along but thats not what I want
> to do? Isnt there any way I could get it with a sizeof
> operator?
> What my confusion is that when I write:

> char * array = {"cpp","C#"};

> I can get the above arrays' size through sizeof
> (array)/sizeof(array[0])

  You can't, because 'array' is not an array,
  but a 'char' pointer. What you mean is this:

    char* real_array[] = { "bla", "bla" };

Quote:
> WHY cant I get the size this way when I recieve a char *
> [] inside a function?????

  Because array are automatically converted
  to pointers when passed to a function.

Quote:
> Or is there any other way?

  I am far from beeing a language laywer, so I
  can't tell you what's needed in general. This,
  however, works:

    template< size_t size >
    inline std::size_t size( const char (&source)[size] )
    {
      return size;
    }

Quote:
> Thanx :)
> [...]

  HTH,

  Schobi

--

I'm Schobi at suespammers org



Sat, 26 Nov 2005 19:14:41 GMT  
 sizeof a char * [] passed to function
oops, I'm sorry actually I meant
char * array []= {"cpp","C#"};
i missed [] in my previous post.

Well what u r saying , is that the reason why the
void main(char * args[], int argscnt)
the main is always passed a size along the array because
its impossible in c++ to get the size of char * [] passed
to  a function?

One more question is that can u advise me some c++
programming books which discusses only the "most" advanced
concepts of c++? So that my all-time wish of becomming an
advanced c++ programmer comes true :)

thanx for the reply.

Quote:
>-----Original Message-----

>> I knew I can pass the size along but thats not what I
want
>> to do? Isnt there any way I could get it with a sizeof
>> operator?
>> What my confusion is that when I write:

>> char * array = {"cpp","C#"};

>> I can get the above arrays' size through sizeof
>> (array)/sizeof(array[0])

>  You can't, because 'array' is not an array,
>  but a 'char' pointer. What you mean is this:

>    char* real_array[] = { "bla", "bla" };

>> WHY cant I get the size this way when I recieve a char *
>> [] inside a function?????

>  Because array are automatically converted
>  to pointers when passed to a function.

>> Or is there any other way?

>  I am far from beeing a language laywer, so I
>  can't tell you what's needed in general. This,
>  however, works:

>    template< size_t size >
>    inline std::size_t size( const char (&source)[size] )
>    {
>      return size;
>    }

>> Thanx :)
>> [...]

>  HTH,

>  Schobi

>--

>I'm Schobi at suespammers org

>.



Sun, 27 Nov 2005 14:29:41 GMT  
 sizeof a char * [] passed to function

Quote:

> oops, I'm sorry actually I meant
> char * array []= {"cpp","C#"};
> i missed [] in my previous post.

  I thought so. :o>

Quote:
> Well what u r saying , is that the reason why the
> void main(char * args[], int argscnt)

  int main(int argc, char* argv[])

  There's no 'main()' in std C++ that returns
  'void'. It always returns 'int'. (Even though
  some compilers accept the 'void'.)

Quote:
> the main is always passed a size along the array because
> its impossible in c++ to get the size of char * [] passed
> to  a function?

  Yes.
  If you want to have the array and its
  size all in one place, use 'std::vector'.
  This will also take the burden of memory
  management from you.

Quote:
> One more question is that can u advise me some c++
> programming books which discusses only the "most" advanced
> concepts of c++? So that my all-time wish of becomming an
> advanced c++ programmer comes true :)

  Define "advanced".
  There's people that call themselves
  "advanced" because they know all the
  dirty tricks to make a hack working.
  OTOH, to me beeing "advanced" means to
  get along well without ever needing
  such stuff in 98% of all cases.

  If you already know C++ (as you do
  obviously), Koenig/Moo's "Accelerated
  C++" is an excellent book to pick up
  C++ as it should be used. (Actually,
  it's meant to be an introduction, but
  it is way to steep for beginners. I
  do C++ for about ten years, I even
  taught C++, and I enjoyed reading this
  book a lot.)
  There's quite a few others that you
  might be interested in Just google for
  this question in this ng as this comes
  up twice a month. Also, you should
  look at the book review section at the
  ACCU (www.accu.org). So far I found
  the reviews and recomandations to be
  justified in each and every case I had
  the chance to test it.

Quote:
> thanx for the reply.

  HTH,

Quote:
> [...]

  Schobi

--

I'm Schobi at suespammers org



Sun, 27 Nov 2005 19:30:51 GMT  
 sizeof a char * [] passed to function
Guru of the week at www.gotw.ca covers advanced C++ subjects that are
definitely "advanced", and often not for the faint-hearted.

Kevin.


Quote:

> > oops, I'm sorry actually I meant
> > char * array []= {"cpp","C#"};
> > i missed [] in my previous post.

>   I thought so. :o>

> > Well what u r saying , is that the reason why the
> > void main(char * args[], int argscnt)

>   int main(int argc, char* argv[])

>   There's no 'main()' in std C++ that returns
>   'void'. It always returns 'int'. (Even though
>   some compilers accept the 'void'.)

> > the main is always passed a size along the array because
> > its impossible in c++ to get the size of char * [] passed
> > to  a function?

>   Yes.
>   If you want to have the array and its
>   size all in one place, use 'std::vector'.
>   This will also take the burden of memory
>   management from you.

> > One more question is that can u advise me some c++
> > programming books which discusses only the "most" advanced
> > concepts of c++? So that my all-time wish of becomming an
> > advanced c++ programmer comes true :)

>   Define "advanced".
>   There's people that call themselves
>   "advanced" because they know all the
>   dirty tricks to make a hack working.
>   OTOH, to me beeing "advanced" means to
>   get along well without ever needing
>   such stuff in 98% of all cases.

>   If you already know C++ (as you do
>   obviously), Koenig/Moo's "Accelerated
>   C++" is an excellent book to pick up
>   C++ as it should be used. (Actually,
>   it's meant to be an introduction, but
>   it is way to steep for beginners. I
>   do C++ for about ten years, I even
>   taught C++, and I enjoyed reading this
>   book a lot.)
>   There's quite a few others that you
>   might be interested in Just google for
>   this question in this ng as this comes
>   up twice a month. Also, you should
>   look at the book review section at the
>   ACCU (www.accu.org). So far I found
>   the reviews and recomandations to be
>   justified in each and every case I had
>   the chance to test it.

> > thanx for the reply.

>   HTH,

> > [...]

>   Schobi

> --

> I'm Schobi at suespammers org



Sat, 03 Dec 2005 11:26:32 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Passing a char array[n] to a function expecting a char*

2. Passing char* to function returning null

3. Pass char * to function

4. Problem passing char * to a function where it is assigned its value

5. how to pass char [10][21] to function?

6. Modifying a char passed to a function

7. Can char*[] be explicitly passed to a function

8. renaming char arrays passed to functions

9. Pass CHAR * to Function called by CREATETHREAD

10. Pass CHAR * to Function called by CREATETHREAD

11. scope of local string on function stack, passed as const char *

12. Passing int vs. passing char

 

 
Powered by phpBB® Forum Software