By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,581 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,581 IT Pros & Developers. It's quick & easy.

what is the meaning of "char **option"?

P: n/a
what is meaning os "char **option meet"?
I get a function like this:
__________________________________
int getchoice(char *greet, char *choices[])
{
int chosen = 0;
int selected;
char **option; // what is this line mean?
do {
printf("Choice: %s\n", greet);
option = choices;
while (*option) {
printf("%s\n", *option);
option++;
}
selected = getchar();
option = choices;
while (*option) {
if (selected == *option[0]) {
chosen = 1;
break;
}
}
if (!chosen) {
printf("Incorrect choice, select again\n");
}
} while (!chosen);
return selected;
}
__________________________________

I know "char *option" is a pointer. But what is the ** mean? If i
delete one *, this function can't be compiled!
PS: i compile it by GCC 4 on ArchLinux

Oct 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Omats.Z wrote:
what is meaning os "char **option meet"?
I get a function like this:
__________________________________
int getchoice(char *greet, char *choices[])
{
int chosen = 0;
int selected;
char **option; // what is this line mean?
ptr-to-ptr-to-char

a pointer is typically implemented as an address so option will contain
the
address of a location that holds the address of a location of a char.

option is of type char**
*option is of type char* (ptr-to-char)
**option is of type char
do {
printf("Choice: %s\n", greet);
option = choices;
option now points at choices
while (*option) {
*option points to the first entry of the choices array, the loop
continues so
long as the entry is not NULL
printf("%s\n", *option);
option++;
this advances to the next entry in the array
}
selected = getchar();
option = choices;
while (*option) {
if (selected == *option[0]) {
chosen = 1;
break;
}
}
if (!chosen) {
printf("Incorrect choice, select again\n");
}
} while (!chosen);
return selected;
}
__________________________________

I know "char *option" is a pointer. But what is the ** mean? If i
delete one *, this function can't be compiled!
PS: i compile it by GCC 4 on ArchLinux
a good text book should explain all this

--
Nick Keighley

Oct 16 '06 #2

P: n/a
char is char
char* is ptr to char (widely used to access array of character)
char* = string (I am brave...some purist catch my neck...!!!)
char ** = ptr to ptr to char = ptr to string (widely used to access
array of String)

Your compiler is Ok,
Your code may contain memory leak...!!!

if you are Omzts.Z Dont read following line,

sorry for dumb answer :)

--raxit

Oct 16 '06 #3

P: n/a
"Omats.Z" <om****@gmail.comwrites:
what is meaning os "char **option meet"?
I get a function like this:
__________________________________
int getchoice(char *greet, char *choices[])
{
int chosen = 0;
int selected;
char **option; // what is this line mean?
do {
printf("Choice: %s\n", greet);
option = choices;
while (*option) {
printf("%s\n", *option);
option++;
}
selected = getchar();
option = choices;
while (*option) {
if (selected == *option[0]) {
chosen = 1;
break;
}
}
if (!chosen) {
printf("Incorrect choice, select again\n");
}
} while (!chosen);
return selected;
}
__________________________________

I know "char *option" is a pointer. But what is the ** mean? If i
delete one *, this function can't be compiled!
PS: i compile it by GCC 4 on ArchLinux
others will answer this : but really you should find this in any half
decent C book.

I would be more worried about your second "while(*option)"
loop. Something tells me you have only been choosing option number 1 in
the list .....
Oct 16 '06 #4

P: n/a

Omats.Z wrote:
what is meaning os "char **option meet"?
char c; // c is a single char;
char *pc = &c; // pc is a pointer to char, initialized to point
to c
char **ppc = &pc; // ppc is a pointer to pointer to char, initialized
to point to pc
I get a function like this:
__________________________________
int getchoice(char *greet, char *choices[])
Note that char *choices[] is a synonym for char **choices.

Remember that when you pass an array as an argument to a function, what
actually happens is that a pointer to the first element of the array
gets passed. So if you're passing an array of char* (pointer to char),
what actually gets passed is a char ** (pointer to pointer to char).

It's fairly clear from the context of the code that choices is an array
of character strings, and this function allows you to choose one of
them based on the first character in the string. This code prints out
all of the strings in choices, and asks you to enter a single
character. It then attempts to match that character to the first
character in each of the choices. For example, if choices contained
the elements "foo" and "bar", the code would print out

foo
bar

and ask you to pick one based on the first letter. If you enter
anything other than an 'f' or a 'b', the function will print an error
message and ask you to choose again.

The statement "option = choices" is synonymous with "option =
&choices[0]".
{
int chosen = 0;
int selected;
char **option; // what is this line mean?
do {
printf("Choice: %s\n", greet);
option = choices;
The following code cycles through the choices array, printing each
element, until it hits a NULL pointer. It uses the C idiom of using a
pointer to cycle through an array rather than using an array index.
while (*option) {
printf("%s\n", *option);
option++;
}
The above code is equivalent to the following:

int i = 0;
....
while (choices[i]) // while choices[i] is not NULL
{
printf("%s\n", choices[i]);
i++;
}
selected = getchar();
option = choices;
while (*option) {
if (selected == *option[0]) {
chosen = 1;
break;
}
}
if (!chosen) {
printf("Incorrect choice, select again\n");
}
} while (!chosen);
return selected;
}
__________________________________

I know "char *option" is a pointer. But what is the ** mean? If i
delete one *, this function can't be compiled!
Right, because you're dealing with a pointer to a pointer, not a simple
pointer.
PS: i compile it by GCC 4 on ArchLinux
Oct 16 '06 #5

P: n/a
Thank you everyone. I get my answer from you reply.
John Bode wrote:
Omats.Z wrote:
what is meaning os "char **option meet"?

char c; // c is a single char;
char *pc = &c; // pc is a pointer to char, initialized to point
to c
char **ppc = &pc; // ppc is a pointer to pointer to char, initialized
to point to pc
I get a function like this:
__________________________________
int getchoice(char *greet, char *choices[])

Note that char *choices[] is a synonym for char **choices.

Remember that when you pass an array as an argument to a function, what
actually happens is that a pointer to the first element of the array
gets passed. So if you're passing an array of char* (pointer to char),
what actually gets passed is a char ** (pointer to pointer to char).

It's fairly clear from the context of the code that choices is an array
of character strings, and this function allows you to choose one of
them based on the first character in the string. This code prints out
all of the strings in choices, and asks you to enter a single
character. It then attempts to match that character to the first
character in each of the choices. For example, if choices contained
the elements "foo" and "bar", the code would print out

foo
bar

and ask you to pick one based on the first letter. If you enter
anything other than an 'f' or a 'b', the function will print an error
message and ask you to choose again.

The statement "option = choices" is synonymous with "option =
&choices[0]".
{
int chosen = 0;
int selected;
char **option; // what is this line mean?
do {
printf("Choice: %s\n", greet);
option = choices;

The following code cycles through the choices array, printing each
element, until it hits a NULL pointer. It uses the C idiom of using a
pointer to cycle through an array rather than using an array index.
while (*option) {
printf("%s\n", *option);
option++;
}

The above code is equivalent to the following:

int i = 0;
...
while (choices[i]) // while choices[i] is not NULL
{
printf("%s\n", choices[i]);
i++;
}
selected = getchar();
option = choices;
while (*option) {
if (selected == *option[0]) {
chosen = 1;
break;
}
}
if (!chosen) {
printf("Incorrect choice, select again\n");
}
} while (!chosen);
return selected;
}
__________________________________

I know "char *option" is a pointer. But what is the ** mean? If i
delete one *, this function can't be compiled!

Right, because you're dealing with a pointer to a pointer, not a simple
pointer.
PS: i compile it by GCC 4 on ArchLinux
Oct 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.