The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Array Size Problem
Discuss Array Size Problem in the C Programming forum on Dev Shed. Array Size Problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 22nd, 2010, 07:20 AM
|
|
Contributing User
|
|
Join Date: Nov 2004
Location: UK
Posts: 196
Time spent in forums: 1 Day 10 h 57 m 49 sec
Reputation Power: 9
|
|
|
Array Size Problem
From a database I get a collection of character arrays of different sizes. I need to process some of these arrays and to do this I need to be able to determine the size of each array.
If this cannot be done then I may need to export out of the database a series of defines, one for each array. This is more work, do I need to do this or can I find out the size?
Sample code of problem below:-
Code:
char *OPTIONS1[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
char *OPTIONS2[2] = {"OPT_1", "OPT_4"};
char *OPTIONS3[1] = {"OPT_1234"};
char* GetOptions(short enumValue, char** szOptions)
{
int i, nOptions;
short size;
nOptions = 1;
size = HOW TO DETERMINE THE SIZE OF ARRAY szOptions !!!!!!!!!
printf("Array Size = %d\n", size);
for(i = 0; i < size; i++)
{
// do something with OPTIONS[j]
...
...
}
return szResult;
}
// In the Main program call GetOptions() for 3 different sized arrays
option = GetOptions(1, OPTIONS1);
option = GetOptions(2, OPTIONS2);
option = GetOptions(3, OPTIONS3);
|

October 22nd, 2010, 07:27 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
Your question is database and database interface specific, not really related to C/C++. You should try a database forum.
Having said that, basic arrays in C/C++ always lack any notion of their size and the size MUST be supplied by the code that creates/fills the array. I am certain that that facility is available in the access library you are using.
|

October 22nd, 2010, 07:40 AM
|
|
|
If your arrays are truly created at compile time, you can use:
Code:
sizeof(OPTIONS1)/sizeof(*OPTIONS1)
to determine the size of the array to be passed to the function. It must be passed to the function however, as that will not work inside the function. Also, if the above is true, your arrays should be declared as const char *:
Code:
const char *OPTIONS1[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
const char *OPTIONS2[2] = {"OPT_1", "OPT_4"};
const char *OPTIONS3[1] = {"OPT_1234"};
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!
|

October 22nd, 2010, 07:59 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
I thought you were asking how to know how many elements were returned in an array from a database call. For data in a program you can use a 'trick' where you have some gate value as the last element of the array. For instance:
Code:
const char *OPTIONS1[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4", NULL};
for (int i=0; OPTIONS[i] != NULL; ++i){
...do something
}
will work. However this 'trick' is not very reliable in integer/float arrays unless you can know with absolute certainty that a given value will never be represented in the data. This does work quite well in pointer arrays as NULL is indeed guaranteed to be an invalid value for a pointer.
|

October 22nd, 2010, 08:00 AM
|
|
Contributing User
|
|
Join Date: Nov 2004
Location: UK
Posts: 196
Time spent in forums: 1 Day 10 h 57 m 49 sec
Reputation Power: 9
|
|
Quote: | Originally Posted by mitakeet Your question is database and database interface specific, not really related to C/C++. You should try a database forum.
Having said that, basic arrays in C/C++ always lack any notion of their size and the size MUST be supplied by the code that creates/fills the array. I am certain that that facility is available in the access library you are using. |
This is a striaight c-code question, the export from the database generates the following arrays (I will make them const) as ptr2void suggested.
Code:
const char *OPTIONS1[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
const char *OPTIONS2[2] = {"OPT_1", "OPT_4"};
const char *OPTIONS3[1] = {"OPT_1234"};
Perhaps I need to do the following:-
Code:
const char *OPTIONS1[5] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4", ""};
const char *OPTIONS2[3] = {"OPT_1", "OPT_4", ""};
const char *OPTIONS3[1] = {"OPT_1234", ""};
Not sure if the last item is best suited to "".
Then I could test for OPTIONS1[5] != NULL
Any comments?
|

October 22nd, 2010, 08:31 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
You could do while (strlen(OPTIONS[i]) > 0)..., but NULL is the more commonly accepted approach.
If you are not stuck with C, go ahead and use vectors. Vectors always know their size.
|

October 22nd, 2010, 04:33 PM
|
|
|
Quote: | Originally Posted by mitakeet You could do while (strlen(OPTIONS[i]) > 0)..., but NULL is the more commonly accepted approach. |
strlen(OPTIONS[i]) gives undefined behavior if OPTIONS[i] is NULL, which - if the end of the OPTIONS array is reached - is the case here.
__________________
Right 98% of the time, and don't care about the other 3%.
“It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets.” (Alan Cooper)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|