473,387 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
5 14069
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
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
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ckroom | last post by:
Hi, I have a vector of chars: "FFFFFF" Each 2 chars from the vector in hex represents a decimal number, how can I convert the vector to 3 decimal values (char *)"FF"->(int)256. I have no idea. ...
37
by: Debajit Adhikary | last post by:
Now I know (from Stroustrup's site) that "char" is pronounced "tchar" and not "kar". Does the same hold true largely in the C world?
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
11
by: sweety | last post by:
Hi, The charecter/symbol " ® " writen in a file able to get only in that system. When the file opened in other system, not able to see that charecter, insted its viewd as " ? ". So, the special...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
14
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.