Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the array size of TCHAR*

2,402 views
Skip to first unread message

a

unread,
Sep 25, 2008, 8:25:18 AM9/25/08
to
Hello

I get an arrary of TCHAR* as parameter

TCHAR* env[]

How can I get the entry count in this array? No param tells me the count
value.

Pavel A.

unread,
Sep 25, 2008, 8:30:12 AM9/25/08
to

The Great Chairman Mao taught us that something does not come from nothing.

You got to provide another parameter, or maybe
this is a zero terminated string.

--pa

Pavel A.

unread,
Sep 25, 2008, 8:39:25 AM9/25/08
to
Pavel A. wrote:
...

> You got to provide another parameter, or maybe
> this is a zero terminated string.

Sorry... the last line should be: NULL terminated array of pointers.

/* For the Great Chairman also taught to think twice and only then hit
the send key :( */

--pa

Duane Hebert

unread,
Sep 25, 2008, 8:46:08 AM9/25/08
to
"Pavel A." <pav...@NOfastmailNO.fm> wrote in message
news:O3u5evwH...@TK2MSFTNGP02.phx.gbl...


But he surely must have also taught that the smallest
measureable amount of time is that between hitting
the enter key and the "oh sh*t".

a

unread,
Sep 26, 2008, 1:09:23 AM9/26/08
to
Hello

But this does not answer my question. In vc console app, the main entry is
in the form of

int _tmain(int argc, _TCHAR* argv[])

Many people do not know there is the 3rd parameter:

int _tmain(int argc, _TCHAR* argv[], _TCHAR* env[])

env points to the env variables passed from the os loader to this process.
It does not have the count info of the env variables. How can get the count?

One method I'm using is to have a pointer enumerate the env (pointer ++)
until it sees a NULL. In the enumeration, I count the entry number (count
++). Is there a better way? Can I use the sizeof method, say sizeof(the
whole array)/sizeof(each element)?

"Duane Hebert" <sp...@flarn.com> wrote in message
news:eswzwyw...@TK2MSFTNGP06.phx.gbl...

Norman Bullen

unread,
Sep 26, 2008, 1:41:29 AM9/26/08
to
a wrote:
> Hello
>
> But this does not answer my question. In vc console app, the main entry
> is in the form of
>
> int _tmain(int argc, _TCHAR* argv[])
>
> Many people do not know there is the 3rd parameter:
>
> int _tmain(int argc, _TCHAR* argv[], _TCHAR* env[])
>
> env points to the env variables passed from the os loader to this
> process. It does not have the count info of the env variables. How can
> get the count?
>
> One method I'm using is to have a pointer enumerate the env (pointer ++)
> until it sees a NULL. In the enumeration, I count the entry number
> (count ++). Is there a better way? Can I use the sizeof method, say
> sizeof(the whole array)/sizeof(each element)?
>
That is exactly the only way to find the end of the env array.

From MSDN:
envp

A pointer to an array of environment strings. It can be declared as an
array of pointers to char (char *envp[ ]) or as a pointer to pointers to
char (char **envp). If your program uses wmain instead of main, use the
wchar_t data type instead of char. The end of the array is indicated by
a NULL pointer. The environment block passed to main and wmain is a
“frozen” copy of the current environment. If you subsequently change the
environment via a call to putenv or _wputenv, the current environment
(as returned by getenv/_wgetenv and the _environ/ _wenviron variable)
will change, but the block pointed to by envp will not change. This
argument is ANSI compatible in C, but not in C++.


--
Norm

To reply, change domain to an adult feline.

Sunny

unread,
Sep 26, 2008, 11:02:52 PM9/26/08
to
int nlen = _tcslen(env)/sizeof(TCHAR);

"a" <a...@a.com> ??????:eCZPHnwH...@TK2MSFTNGP02.phx.gbl...

Tim Roberts

unread,
Sep 27, 2008, 12:47:05 AM9/27/08
to
"a" <a...@a.com> wrote:
>
>One method I'm using is to have a pointer enumerate the env (pointer ++)
>until it sees a NULL. In the enumeration, I count the entry number (count
>++). Is there a better way? Can I use the sizeof method, say sizeof(the
>whole array)/sizeof(each element)?

You could, if you knew sizeof(the whole array). You don't.

Scan until NULL is the only way, exactly like they way you process argv.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts

unread,
Sep 29, 2008, 12:24:14 AM9/29/08
to
"Sunny" <sound_o...@hotmail.com> wrote:
>
>int nlen = _tcslen(env)/sizeof(TCHAR);

That won't compile, because env is a TCHAR **, not a TCHAR *.

However, even if you substituted _tcslen(*env), that would only get you the
length of the first variable in the environment. The environment is not
passed as one long string. It's passed as a list of pointers to strings,
one variable per entry. You have to stroll through the whole list. There's
no shortcut.

Ulrich Eckhardt

unread,
Sep 29, 2008, 4:07:14 AM9/29/08
to
Sunny wrote:
> int nlen = _tcslen(env)/sizeof(TCHAR);

Okay, and what is that supposed to be? The length in elements would be

size_t elements = _tcslen(str)+1;

including the null terminator while the length in bytes would be

size_t bytes = (_tcslen(str)+1) * (sizeof *str);

also including the trailing null. All of this of course assuming that 'str'
is a pointer to TCHAR. Further, it also doesn't take into account any
overallocation.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Ben Voigt [C++ MVP]

unread,
Oct 8, 2008, 5:34:13 PM10/8/08
to
Tim Roberts wrote:
> "a" <a...@a.com> wrote:
>>
>> One method I'm using is to have a pointer enumerate the env (pointer
>> ++) until it sees a NULL. In the enumeration, I count the entry
>> number (count ++). Is there a better way? Can I use the sizeof
>> method, say sizeof(the whole array)/sizeof(each element)?
>
> You could, if you knew sizeof(the whole array). You don't.
>
> Scan until NULL is the only way, exactly like they way you process
> argv.

Except argv has exactly argc entries, you don't need to go looking for a
NULL pointer. So you process envp differently from argv.


0 new messages