From: DS on 27 Jan 2010 18:12 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
From: Igor Tandetnik on 27 Jan 2010 18:26 DS <dsutNOSPAMter(a)tc3NOSPAMnet.com> wrote: > 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'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() > Now I tried: > 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
From: DS on 27 Jan 2010 18:44 Thanks for the quick reply. I will delve in to debugging then :) Thanks for the confidence in "char**p;" Dan "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message news:uyHLvg6nKHA.3948(a)TK2MSFTNGP06.phx.gbl... DS <dsutNOSPAMter(a)tc3NOSPAMnet.com> wrote: >> 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.) >> 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. >> Now I tried: >> 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. >-- >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
From: Igor Tandetnik on 27 Jan 2010 18:58 DS <dsutNOSPAMter(a)tc3NOSPAMnet.com> wrote: > "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message > news:uyHLvg6nKHA.3948(a)TK2MSFTNGP06.phx.gbl... >> 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. >> 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
From: Stephan T. Lavavej [MSFT] on 27 Jan 2010 19:47
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 "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message news:u2T2ty6nKHA.1928(a)TK2MSFTNGP05.phx.gbl... DS <dsutNOSPAMter(a)tc3NOSPAMnet.com> wrote: > "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message > news:uyHLvg6nKHA.3948(a)TK2MSFTNGP06.phx.gbl... >> 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. >> 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 |