C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 22nd, 2010, 07:20 AM
andy101 andy101 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 196 andy101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);

Reply With Quote
  #2  
Old October 22nd, 2010, 07:27 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #3  
Old October 22nd, 2010, 07:40 AM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,477 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 29 m 54 sec
Reputation Power: 2194
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!

Reply With Quote
  #4  
Old October 22nd, 2010, 07:59 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.

Reply With Quote
  #5  
Old October 22nd, 2010, 08:00 AM
andy101 andy101 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: UK
Posts: 196 andy101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #6  
Old October 22nd, 2010, 08:31 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.

Reply With Quote
  #7  
Old October 22nd, 2010, 04:33 PM
LittleGrin LittleGrin is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2007
Posts: 921 LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 11 h 50 m 18 sec
Reputation Power: 535
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)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array Size Problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap