473,608 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Incorre ct 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 14092
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("Incorre ct 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.c omwrites:
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("Incorre ct 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("Incorre ct 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("Incorre ct 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
2571
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. Thanks! -- ckroom http://nazaries.net/~ckroom
37
12139
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
10671
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 ASCII characters in the 0-127 range, although I had thought that I might want to use the 128-255 range for special symbols or foreign character codes. This all worked OK for a long time, but a recent update to the compiler on my system has...
11
1378
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 char sets are different system to system? How can over come this problem , that to view this symbol " ® " using the same kind of editor in any system.
26
11684
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
34
2677
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
20
3532
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
307
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
3038
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 surely this is possible? I could define an operator<< but for various reasons, I really want to convert to a 'const char*' (to embed into a string which becomes part of a window's caption, etc).
0
8067
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8483
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8349
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6820
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6015
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5479
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
1336
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.