473,508 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert string to pointer to pointer?

Hi i converted a program to a lib. The parameters of main are
maincommand(int argc, char **argv)
I want to use main a s function that takes a string how can i convert
a char * to a char ** or even better how should i substitute the parameters
for a string. thanks
Nov 14 '05 #1
4 9956

On Thu, 8 Jan 2004, Joe H wrote:

Hi i converted a program to a lib. The parameters of main are
maincommand(int argc, char **argv)
I want to use main a s function that takes a string how can i convert
a char * to a char ** or even better how should i substitute the parameters
for a string. thanks


How many parameters, how organized, etc.? For example, if you're
used to calling the old program at the command line like this:

% myprog 100
% myprog 42
% myprog -8192

then you might prototype the new library function as

int myprog(int arg);

But if you write big complicated programs that you call like this:

% myprog -X 310 -Y 100 -bc -g 5
% myprog -X 80 -Y 80 -b -g 10 -k1 1.5 -k2 .3
% myprog -c -k2 50

then you might be better off with a prototype that requires the client
to explicitly state each possible argument's value:

int myprog(int X, int Y, int g, double k1, double k2, int B, int C);

Finally, if you are into clever little parsing algorithms, you
might write programs that get called like this:

% myprog "echome"
% myprog -o foo bar
% myprog recursive -o foo

and then maybe it would make sense to let your library function do
its own splitting-up-of-arguments, and just take a single string
with all the arguments glommed together, like this:

int myprog(const char *all_the_arguments);

It really depends on what you're trying to do.

If you want to pass a single string to a 'main'-like function
that takes (int, char **), you can do it like this:

int callmain(const char *p)
{
int my_argc;
char *my_argv[100];

my_argc = 2;
my_argv[0] = "program name goes here, usually";
my_argv[1] = (char *)p; /* EVIL CAST! EVIL EVIL EVIL! */
my_argv[2] = NULL;
return fake_main(my_argc, my_argv);
}

Notice the presence of an EVIL EVIL EVIL cast to (char *). That's
only necessary because I made 'callmain' take a (const char *)
parameter. I did that only because it seems nicer to the user. It
works as long as 'fake_main' behaves itself and doesn't go modifying
'p' behind our backs. Maybe it would be better to remove the 'const'
altogether... it depends.

HTH,
-Arthur

Nov 14 '05 #2
joe
thanks for your reponse ill try to make sense out of that
the program takes the pointer to pointer and the int and gets its
parameters like this

while ((ch = getopt(argc, argv,
"b:c:Dde:f:F:hHk:lmn:o:pPr:sS:tT:uUv:x:z:")) != -1)

i would like to give it a string rather than the pointer to pointer. i have
done this in the main program and it compiles but i cant get the function
to run

#include "zib.h"
int main(int argc, char* argv[])
{
char line[100] = "-s -T 1002\n";

int t = zcommand(1, &line);

return 1;

}

Nov 14 '05 #3

On Thu, 8 Jan 2004, joe wrote:

thanks for your reponse ill try to make sense out of that
the program takes the pointer to pointer and the int and gets its
parameters like this

while ((ch = getopt(argc, argv,
"b:c:Dde:f:F:hHk:lmn:o:pPr:sS:tT:uUv:x:z:")) != -1)
'getopt' is not a standard C function, and so it's off-topic here.
That's not just zealous topicality guidance, either -- I really have
no idea how 'getopt' works, and I don't care. But I can make some
assumptions, most of them implicit, and let you puzzle out what works
and what doesn't.
i would like to give it a string rather than the pointer to pointer. i have
done this in the main program and it compiles but i cant get the function
to run int main(int argc, char* argv[])
{
char line[100] = "-s -T 1002\n";

int t = zcommand(1, &line);


Where 'zcommand' is the old 'main' function, no doubt. Okay, I'm
not surprised that it didn't work. I recommend that you take a good
look at the last public C draft standard, N869 (which you can Google
for; it comes in plain text as n869.txt[.gz]). It has a section
somewhere detailing exactly what the implementation guarantees to
be present in 'argc' and 'argv' -- and thus what *you* need to
guarantee will be present in *your* "fake" argc and argv.
I would quote N869 right here, but I'm on a slow connection this
week. Maybe someone else will. Anyway, search the file for the
words 'argc' and 'argv' and I bet you'll find it.

You [probably] need to split up those arguments in that string
literal, like this [but I don't know how lenient 'getopt' might
be on your platform -- maybe it's a non-issue]:

char *line[] = {"dummy", "-s", "-T", "1002", NULL};

int t = zcommand(4, line);

If this *is* the issue [consult your 'getopt' documentation, such
as "man getopt" or your compiler manual], then you might need to
write some code to split up the "command-line arguments" yourself.
That is, a function that can take the input
"-s -T 1002\n"
and produce the output
{"dummy", "-s", "-T", "1002", NULL}

If it turns out you do need such a function, check Google for
"command line argument parsing" or post to a group like
comp.sources.wanted. (comp.lang.c is *not* the place to ask other
people to write whole big functions for you.)

HTH,
-Arthur
Nov 14 '05 #4
joe


thanks alot, that was great
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7546
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
8
12129
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to...
4
1975
by: Al Reid | last post by:
I have a simple function that I use in many of my applications. It allows one to update the "Status" panel of a status bar and optionally set the MousePointer. This is useful if the status is...
3
16768
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
10
5607
by: rodrigo.gloria | last post by:
I am trying to convert a integer to an address of a function pointer. I want to encrypt the pointer and then do some validation, afterwards i will decrpyt the pointer back to an address. Can...
5
4424
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
5
6591
by: Gary Wessle | last post by:
Hi is there a way to convert a string to a function name and fire it. like void his_fun(){ cout << "his is here" << endl; } vector<stringvec; vec.push_back("his");
4
25038
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
4
8425
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
14
13428
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.