Discussion:
Declaring a dynamic pointer to an array of char pointers
(too old to reply)
DS
2010-01-27 23:12:17 UTC
Permalink
Hi,
I am troubled...

I would like to declare a pointer to an array of char pointers, that
I can allocate at some time during a run.
I'll expect a variable length string of tokens seperated by white space chars.
I would like to search for each token head and store it's address in a sequential
array of pointers, Then insert a NULL at each tokens end.
ie: string = "Cmd Arg1 Arg2 Arg3..."

Now I tried:
char ** PtrArray;
But the program hanged.

char * PtrArray[];
The declaration compiled but I get errors on:
if ( PtrArray == (char *[]) NULL)
or if( PtrArray == (char *)NULL


char * PtrArray[1];
The declaration compiled but I get errors on:
if ( PtrArray == (char *[]) NULL)
or if( PtrArray == (char *)NULL

I have turned my "C Reference Manual inside and out.
I have read several web documents, pointer tutorials...
Please help,
Dan
Igor Tandetnik
2010-01-27 23:26:40 UTC
Permalink
Post by DS
I would like to declare a pointer to an array of char pointers, that
I can allocate at some time during a run.
char** p;

But I respectfully submit you'd be much better off with something like vector<string> .
Post by DS
I'll expect a variable length string of tokens seperated by white space chars.
I would like to search for each token head and store it's address in
a sequential array of pointers, Then insert a NULL at each tokens end.
This sounds suspciously like strtok()
Post by DS
char ** PtrArray;
But the program hanged.
Surely not on this line? I confidently predict there is a problem with the code you haven't shown.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
DS
2010-01-27 23:44:56 UTC
Permalink
Thanks for the quick reply.
I will delve in to debugging then :)
Thanks for the confidence in "char**p;"
Dan
Post by Igor Tandetnik
Post by DS
I would like to declare a pointer to an array of char pointers, that
I can allocate at some time during a run.
char** p;
But I respectfully submit you'd be much better off with something like vector<string> .
(I hoped this was a 'C' group.)
Post by Igor Tandetnik
Post by DS
I'll expect a variable length string of tokens seperated by white space chars.
I would like to search for each token head and store it's address in
a sequential array of pointers, Then insert a NULL at each tokens end.
This sounds suspciously like strtok()
Sure, but it(strtok()) may not be available in my environment.
Post by Igor Tandetnik
Post by DS
char ** PtrArray;
But the program hanged.
Surely not on this line? I confidently predict there is a problem with the code you haven't shown.
Thanks Igor.
Post by Igor Tandetnik
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are
going to land, and it could be dangerous sitting >under them as they fly overhead. -- RFC 1925
Igor Tandetnik
2010-01-27 23:58:51 UTC
Permalink
Post by DS
Post by Igor Tandetnik
But I respectfully submit you'd be much better off with something like vector<string> .
(I hoped this was a 'C' group.)
There is no group dedicated to C in microsoft.public.* hierarchy. I suppose if such a group existed, it would be pretty lonely there. People here generally don't mind pure C questions though, but you should note in your posts that you are thus limited: C++ is assumed by default.
Post by DS
Post by Igor Tandetnik
This sounds suspciously like strtok()
Sure, but it(strtok()) may not be available in my environment.
If you say so, though I'd like to mention that strtok is part of the standard C library. In any case, there are plenty of implementations of strtok published under various licenses: perhaps you can borrow one.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Stephan T. Lavavej [MSFT]
2010-01-28 00:47:58 UTC
Permalink
strtok() is terrible. Instead, use regex_token_iterator to perform field
splitting:

C:\Temp>type meow.cpp
#include <iostream>
#include <ostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
using namespace std::tr1;

int main() {
const string s("I have 1729 cute fluffy kittens");

const regex r("\\s+");

vector<string> v;

for (sregex_token_iterator i(s.begin(), s.end(), r, -1), end; i != end;
++i) {
v.push_back(*i);
}

for (vector<string>::const_reverse_iterator i = v.rbegin(); i !=
v.rend(); ++i) {
cout << *i << ";";
}

cout << endl;
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp

C:\Temp>meow
kittens;fluffy;cute;1729;have;I;

Stephan T. Lavavej
Visual C++ Libraries Developer
Post by DS
Post by Igor Tandetnik
But I respectfully submit you'd be much better off with something like vector<string> .
(I hoped this was a 'C' group.)
There is no group dedicated to C in microsoft.public.* hierarchy. I suppose
if such a group existed, it would be pretty lonely there. People here
generally don't mind pure C questions though, but you should note in your
posts that you are thus limited: C++ is assumed by default.
Post by DS
Post by Igor Tandetnik
This sounds suspciously like strtok()
Sure, but it(strtok()) may not be available in my environment.
If you say so, though I'd like to mention that strtok is part of the
standard C library. In any case, there are plenty of implementations of
strtok published under various licenses: perhaps you can borrow one.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925
Leigh Johnston
2010-01-28 01:26:09 UTC
Permalink
Post by Stephan T. Lavavej [MSFT]
strtok() is terrible. Instead, use regex_token_iterator to perform field
Although horrid and old fashioned I suspect using strtok is ten times faster
than using regex to do basic tokenisation! Perhaps boost.tokenizer would be
a better recommendation although I have not used it myself. I have written
my own tokenizer to do such things (they are not exactly hard to write).

/Leigh
Igor Tandetnik
2010-01-28 01:37:07 UTC
Permalink
Post by Leigh Johnston
Post by Stephan T. Lavavej [MSFT]
strtok() is terrible. Instead, use regex_token_iterator to perform field
Although horrid and old fashioned I suspect using strtok is ten times faster
than using regex to do basic tokenisation! Perhaps boost.tokenizer would be
a better recommendation although I have not used it myself. I have written
my own tokenizer to do such things (they are not exactly hard to write).
In any case, the OP has indicated that he is, unenviably, limited to C, so neither suggestion is probably going to be of much help.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Bo Persson
2010-01-28 16:49:25 UTC
Permalink
Post by Leigh Johnston
Post by Stephan T. Lavavej [MSFT]
strtok() is terrible. Instead, use regex_token_iterator to
Although horrid and old fashioned I suspect using strtok is ten
times faster than using regex to do basic tokenisation! Perhaps
boost.tokenizer would be a better recommendation although I have
not used it myself. I have written my own tokenizer to do such
things (they are not exactly hard to write).
/Leigh
Yes, so you can parse the user input in 10 ns instead of 100? :-)


Bo Persson
Tim Roberts
2010-01-29 04:45:11 UTC
Permalink
Post by Leigh Johnston
Post by Stephan T. Lavavej [MSFT]
strtok() is terrible. Instead, use regex_token_iterator to perform field
Although horrid and old fashioned I suspect using strtok is ten times faster
than using regex to do basic tokenisation! Perhaps boost.tokenizer would be
a better recommendation although I have not used it myself. I have written
my own tokenizer to do such things (they are not exactly hard to write).
Yes. Note, however, that the ATL CString implementation contains
CString::Tokenize, which serves the same purpose safely. Not as quickly as
strtok, but faster than a regex.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
DS
2010-01-28 01:40:10 UTC
Permalink
Well I had this statement and was wondering why it hanged :)

while ( isgraph( sUserCmd[i] ) ); i++;

Dan
(too many semicolons)
(PS: Thought I'd sent it to the group.)

----- Original Message -----
From: "Igor Tandetnik" <***@mvps.org>
Newsgroups: microsoft.public.vc.language
Sent: Wednesday, January 27, 2010 6:58 PM
Subject: Re: Declaring a dynamic pointer to an array of char pointers
Post by DS
Post by Igor Tandetnik
But I respectfully submit you'd be much better off with something like vector<string> .
(I hoped this was a 'C' group.)
There is no group dedicated to C in microsoft.public.* hierarchy. I suppose if such a group existed, it would be pretty lonely
there. People here generally don't mind
pure C questions though, but you should note in your posts that you are thus limited: C++ is assumed by default.
Post by Igor Tandetnik
But I respectfully submit you'd be much better off with something like vector<string> .
(I hoped this was a 'C' group.)
There is no group dedicated to C in microsoft.public.* hierarchy. I suppose if such a group existed, it would be pretty lonely
there. People here generally don't mind pure C questions though, but you should note in your posts that you are thus limited: C++ is
assumed by default.
Post by DS
Post by Igor Tandetnik
This sounds suspciously like strtok()
Sure, but it(strtok()) may not be available in my environment.
If you say so, though I'd like to mention that strtok is part of the standard C library. In any case, there are plenty of
implementations of strtok published under various licenses: perhaps you can borrow one.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going
to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Giovanni Dicanio
2010-01-28 16:24:01 UTC
Permalink
Post by DS
I would like to declare a pointer to an array of char pointers, that
I can allocate at some time during a run.
I'll expect a variable length string of tokens seperated by white space chars.
I would like to search for each token head and store it's address in a sequential
array of pointers, Then insert a NULL at each tokens end.
ie: string = "Cmd Arg1 Arg2 Arg3..."
char ** PtrArray;
But the program hanged.
char** would be just fine in pure C.
Post by DS
I have turned my "C Reference Manual inside and out.
I have read several web documents, pointer tutorials...
Please help,
Considering that you are asking for a pure C solution, you may find useful a
simple C tokenzier I wrote and attached here.
It seems to work in some tests, but it needs more verification.

Note that if you could use C++, vector<CString> would be better choice than
raw C-like array of pointers.

(To my limited knowledge, there are few cases in which you must use pure C
instead of C++, like e.g. developing device drivers in kernel mode; if you
aren't in this elite, C++ could make your life easier.)

HTH,
Giovanni
DS
2010-01-28 22:33:49 UTC
Permalink
Grazi Giovanni,
The file is very helpful.
DS
Post by Giovanni Dicanio
Post by DS
I would like to declare a pointer to an array of char pointers, that
I can allocate at some time during a run.
I'll expect a variable length string of tokens seperated by white space chars.
I would like to search for each token head and store it's address in a sequential
array of pointers, Then insert a NULL at each tokens end.
ie: string = "Cmd Arg1 Arg2 Arg3..."
char ** PtrArray;
But the program hanged.
char** would be just fine in pure C.
Post by DS
I have turned my "C Reference Manual inside and out.
I have read several web documents, pointer tutorials...
Please help,
Considering that you are asking for a pure C solution, you may find useful a
simple C tokenzier I wrote and attached here.
It seems to work in some tests, but it needs more verification.
Note that if you could use C++, vector<CString> would be better choice than
raw C-like array of pointers.
(To my limited knowledge, there are few cases in which you must use pure C
instead of C++, like e.g. developing device drivers in kernel mode; if you
aren't in this elite, C++ could make your life easier.)
HTH,
Giovanni
Giovanni Dicanio
2010-01-28 22:38:26 UTC
Permalink
Post by DS
Grazi Giovanni,
The file is very helpful.
DS: You are welcome.

Giovanni
Continue reading on narkive:
Loading...