Passing a char array to a routine
-
I need to pass an array into a procedure and return a string. The problem is the array could be 1, 2, 3 or 4 character strings. I am stuck as how to do it, see typical arrays below.
char *OPTIONS_ENUMS[4] = {
"OPT_1",
"OPT_2",
"OPT_3",
"OPT_4"};char *OPTIONS2_ENUMS[4] = {
"OPT_1",
"OPT_4"};char* GetOptions(short nStatus, char* szOptions)
{
int i, nOptions;
// How can I tell how many elements in the array?
nOptions = sizeof(ssmStatusOptions); // This will not work
for(i=0; i < nOptions; i++)
{
if(nStatus == 0)
{
if(strcmp(szOptions[i], "NULL") == 0)
{
return "NULL";
}
else if(strcmp(szOptions[i], "ITEM1") == 0)
{
return "ITEM1";
}
else if(strcmp(szOptions[i], "NO_DATA") == 0)
{
return "NO_DATA";
}
}
else if(nStatus == 1)
{
if(strcmp(szOptions[i], "OK") == 0)
{
return "OK";
}
else if(strcmp(szOptions[i], "ITEM2") == 0)
{
return "ITEM2";
}
else if(strcmp(szOptions[i], "NO_DATA") == 0)
{
return "NO_DATA";
}
}
}
}How to call the procedure e.g.
GetOptions(1, OPTIONS2_ENUMS);
where OPTIONS2_ENUMS is
char \*OPTIONS\_ENUMS\[4\] = { "OPT\_1", "OPT\_2", "OPT\_3", "OPT\_4"};
I need to know how many elements in the array and how to pass over the array. Its a c code project. Many thanks.
-
I need to pass an array into a procedure and return a string. The problem is the array could be 1, 2, 3 or 4 character strings. I am stuck as how to do it, see typical arrays below.
char *OPTIONS_ENUMS[4] = {
"OPT_1",
"OPT_2",
"OPT_3",
"OPT_4"};char *OPTIONS2_ENUMS[4] = {
"OPT_1",
"OPT_4"};char* GetOptions(short nStatus, char* szOptions)
{
int i, nOptions;
// How can I tell how many elements in the array?
nOptions = sizeof(ssmStatusOptions); // This will not work
for(i=0; i < nOptions; i++)
{
if(nStatus == 0)
{
if(strcmp(szOptions[i], "NULL") == 0)
{
return "NULL";
}
else if(strcmp(szOptions[i], "ITEM1") == 0)
{
return "ITEM1";
}
else if(strcmp(szOptions[i], "NO_DATA") == 0)
{
return "NO_DATA";
}
}
else if(nStatus == 1)
{
if(strcmp(szOptions[i], "OK") == 0)
{
return "OK";
}
else if(strcmp(szOptions[i], "ITEM2") == 0)
{
return "ITEM2";
}
else if(strcmp(szOptions[i], "NO_DATA") == 0)
{
return "NO_DATA";
}
}
}
}How to call the procedure e.g.
GetOptions(1, OPTIONS2_ENUMS);
where OPTIONS2_ENUMS is
char \*OPTIONS\_ENUMS\[4\] = { "OPT\_1", "OPT\_2", "OPT\_3", "OPT\_4"};
I need to know how many elements in the array and how to pass over the array. Its a c code project. Many thanks.
You could either specify the array size to the function as an additional parameter, or you could terminate your array with NULL and in the function iterate until you get a NULL. Btw, shouldn't this:
Andy202 wrote:
char* GetOptions(short nStatus, char* szOptions)
be instead this:
char* GetOptions(short nStatus, char **szOptions)
?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
You could either specify the array size to the function as an additional parameter, or you could terminate your array with NULL and in the function iterate until you get a NULL. Btw, shouldn't this:
Andy202 wrote:
char* GetOptions(short nStatus, char* szOptions)
be instead this:
char* GetOptions(short nStatus, char **szOptions)
?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-
char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
// Would this work
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
} -
Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-
char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
// Would this work
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}No, this will (probably) fail; you only have 4 elements in your array so whatever exists at the next word
OPTIONS_ENUMS[4]
is not guaranteed, it may or may not beNULL
. It is a simple matter to declare an array like this:char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4", NULL};
and let the compiler work out how big it needs to be.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-
char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
// Would this work
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}I agree with Richard's answer - but in some cases, it may not make sense to use NULL as a sentinal marking end of the list, sometimes you may end up defining a non NULL sentinal .. for example, in your list, as long as "EOL" or "End-List" we guaranteed never to be part of your data, you could look through the list until "EOL" or "End-List" were found char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4", "EOL"}; but I'd stick with NULL unless you had a good reason (eg, NULL could be part of the data if you were specifying an empty option/slot) 'g'
-
Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-
char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
// Would this work
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}No it will crash, but :)
char *OPTIONS_ENUMS[5] = {"OPT_1","OPT_2","OPT_3","OPT_4"}; // This works
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}EDIT: As Richard says in next post this may be error prone and is more a joke than a solution. With C++0x (gcc 4.5 only for now) we can use the standard collections with initializer lists like:
std::vector<LPCSTR> OPTIONS_ENUMS = {"OPT_1","OPT_2","OPT_3","OPT_4"};
When not available (MS compilers for now) we must stay in C world and use a macro:
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
for (j = 0; j < ARRAY_SIZE(OPTIONS_ENUMS); j++)
{
if(OPTIONS_ENUMS[j] == NULL) // will never happen
return j;// do something with OPTIONS\_ENUMS\[j\]
}
cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Thursday, October 21, 2010 6:50 PM
-
No it will crash, but :)
char *OPTIONS_ENUMS[5] = {"OPT_1","OPT_2","OPT_3","OPT_4"}; // This works
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}EDIT: As Richard says in next post this may be error prone and is more a joke than a solution. With C++0x (gcc 4.5 only for now) we can use the standard collections with initializer lists like:
std::vector<LPCSTR> OPTIONS_ENUMS = {"OPT_1","OPT_2","OPT_3","OPT_4"};
When not available (MS compilers for now) we must stay in C world and use a macro:
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
for (j = 0; j < ARRAY_SIZE(OPTIONS_ENUMS); j++)
{
if(OPTIONS_ENUMS[j] == NULL) // will never happen
return j;// do something with OPTIONS\_ENUMS\[j\]
}
cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Thursday, October 21, 2010 6:50 PM
Both yours and Garth's answers have the array size specified in the declaration, which I always feel is prone to errors in the future. If you add or remove an element or elements and forget to amend the size then your program starts misbehaving. Stick with my solution and let the compiler do the work for you.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
I agree with Richard's answer - but in some cases, it may not make sense to use NULL as a sentinal marking end of the list, sometimes you may end up defining a non NULL sentinal .. for example, in your list, as long as "EOL" or "End-List" we guaranteed never to be part of your data, you could look through the list until "EOL" or "End-List" were found char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4", "EOL"}; but I'd stick with NULL unless you had a good reason (eg, NULL could be part of the data if you were specifying an empty option/slot) 'g'
-
Both yours and Garth's answers have the array size specified in the declaration, which I always feel is prone to errors in the future. If you add or remove an element or elements and forget to amend the size then your program starts misbehaving. Stick with my solution and let the compiler do the work for you.
Just say 'NO' to evaluated arguments for diadic functions! Ash
It was more a joke than an answer, I edited it to a real answer. cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
I need to pass an array into a procedure and return a string. The problem is the array could be 1, 2, 3 or 4 character strings. I am stuck as how to do it, see typical arrays below.
char *OPTIONS_ENUMS[4] = {
"OPT_1",
"OPT_2",
"OPT_3",
"OPT_4"};char *OPTIONS2_ENUMS[4] = {
"OPT_1",
"OPT_4"};char* GetOptions(short nStatus, char* szOptions)
{
int i, nOptions;
// How can I tell how many elements in the array?
nOptions = sizeof(ssmStatusOptions); // This will not work
for(i=0; i < nOptions; i++)
{
if(nStatus == 0)
{
if(strcmp(szOptions[i], "NULL") == 0)
{
return "NULL";
}
else if(strcmp(szOptions[i], "ITEM1") == 0)
{
return "ITEM1";
}
else if(strcmp(szOptions[i], "NO_DATA") == 0)
{
return "NO_DATA";
}
}
else if(nStatus == 1)
{
if(strcmp(szOptions[i], "OK") == 0)
{
return "OK";
}
else if(strcmp(szOptions[i], "ITEM2") == 0)
{
return "ITEM2";
}
else if(strcmp(szOptions[i], "NO_DATA") == 0)
{
return "NO_DATA";
}
}
}
}How to call the procedure e.g.
GetOptions(1, OPTIONS2_ENUMS);
where OPTIONS2_ENUMS is
char \*OPTIONS\_ENUMS\[4\] = { "OPT\_1", "OPT\_2", "OPT\_3", "OPT\_4"};
I need to know how many elements in the array and how to pass over the array. Its a c code project. Many thanks.
I might be really lazy but I'd be tempted to do one of two things: - Pass another parameter into the function that says how big the array is - Bundle the pointer into a structure with the number of entries and use that That way you don't have to worry as much about pointers to pointers and other things that can make you go cross eyed. You're also half way to adding an abstract data type for your options handling as well. From reading your code I'm not sure you're sure you know what you want to achive. The way you've written is it's "return the first array member that matches one of these strings." You've also not got a return value for when you can't find anything. It's been a while since I've programmed in C but I'd have thought your compiler would have a whinge about that! Cheers, Ash
-
Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-
char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
// Would this work
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}Sorry for the late answer (went to bed, was night here), i agree with the others, use NULL or some special string (or a special pointer) to specify the end of the array. Btw, you can also do this:
char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4", NULL};
this way you don't have to specify the array size, your array's size will be determined by the items you enumerated.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <
-
No it will crash, but :)
char *OPTIONS_ENUMS[5] = {"OPT_1","OPT_2","OPT_3","OPT_4"}; // This works
for(int j = 0; j < 100; j++)
{
if(OPTIONS_ENUMS[j] == NULL)
return j;
}EDIT: As Richard says in next post this may be error prone and is more a joke than a solution. With C++0x (gcc 4.5 only for now) we can use the standard collections with initializer lists like:
std::vector<LPCSTR> OPTIONS_ENUMS = {"OPT_1","OPT_2","OPT_3","OPT_4"};
When not available (MS compilers for now) we must stay in C world and use a macro:
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
for (j = 0; j < ARRAY_SIZE(OPTIONS_ENUMS); j++)
{
if(OPTIONS_ENUMS[j] == NULL) // will never happen
return j;// do something with OPTIONS\_ENUMS\[j\]
}
cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Thursday, October 21, 2010 6:50 PM
Thanks I just tested this and it did not work. The size is always 1. Have I implemented it correct?
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS1_ENUMS[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
char *OPTIONS2_ENUMS[2] = {"OPT_1", "OPT_4"};
char *OPTIONS3_ENUMS[1] = {"OPT_1234"};char* GetOptions(short enumValue, char** szOptions)
{
int i, nOptions;
short size;
nOptions = 1;
size = ARRAY_SIZE(szOptions);printf("Array Size = %d\\n", size); // do something with OPTIONS\_ENUMS\[j\]
}
// In the Main program call GetOptions() for 3 different sized arrays
GetOptions(1, OPTIONS1_ENUMS);
GetOptions(2, OPTIONS2_ENUMS);
GetOptions(3, OPTIONS3_ENUMS); -
Thanks I just tested this and it did not work. The size is always 1. Have I implemented it correct?
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS1_ENUMS[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
char *OPTIONS2_ENUMS[2] = {"OPT_1", "OPT_4"};
char *OPTIONS3_ENUMS[1] = {"OPT_1234"};char* GetOptions(short enumValue, char** szOptions)
{
int i, nOptions;
short size;
nOptions = 1;
size = ARRAY_SIZE(szOptions);printf("Array Size = %d\\n", size); // do something with OPTIONS\_ENUMS\[j\]
}
// In the Main program call GetOptions() for 3 different sized arrays
GetOptions(1, OPTIONS1_ENUMS);
GetOptions(2, OPTIONS2_ENUMS);
GetOptions(3, OPTIONS3_ENUMS);Andy202 wrote:
Thanks I just tested this and it did not work. The size is always 1. Have I implemented it correct?
Nope :) Use it as follows:
#define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer
char *OPTIONS1_ENUMS[] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
char *OPTIONS2_ENUMS[] = {"OPT_1", "OPT_4"};
char *OPTIONS3_ENUMS[] = {"OPT_1234"};void GetOptions(short enumValue, char** szOptions, int Size)
{
printf("Array Size = %d\n", Size);
}
int main()
{
// In the Main program call GetOptions() for 3 different sized arrays
GetOptions(1, OPTIONS1_ENUMS, ARRAY_SIZE(OPTIONS1_ENUMS));
GetOptions(2, OPTIONS2_ENUMS, ARRAY_SIZE(OPTIONS2_ENUMS));
GetOptions(3, OPTIONS3_ENUMS, ARRAY_SIZE(OPTIONS3_ENUMS));
}BTW you should not call your arrays
OPTIONSx_ENUMS
: - as capitalized names are conventionnally reserved for C macros, and - they are notenums
but C arrays ofconst char*
. cheers, ARWhen the wise (person) points at the moon the fool looks at the finger (Chinese proverb)