C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-05-2009, 05:17 AM   #1
Embedded in C...
 
Join Date: Sep 2008
Location: Basingstoke, Hampshire
Posts: 65
Question what to do with unused array elements

Hello,

I have some arrays in my code which will contain different messages of varying lengths, so I have sized my arrays to take the longest ones (plus 1 or two elements on the end), but how do I ensure that if the message is shorter than maximum, there is not junk in the unused elements which would be sent out my serial port?

Thanks
Dave
droseman is offline   Reply With Quote
Old 11-05-2009, 05:32 AM   #2
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
before filling the value in your array just
Code:
memset(array, 0,  sizeof(size) * MAX_NUMBER)
and initailize your array like
Code:
array[MAX_NUMBER] = {0}
RockyMarrone is offline   Reply With Quote
Old 11-05-2009, 05:44 AM   #3
Registered User
 
Join Date: Apr 2006
Location: United States
Posts: 3,202
The third argument to memset is computed like (sizeof(array[0]) * MAX_NUMBER), because the function expects a number of bytes.
__________________
Os iusti meditabitur sapientiam
Et lingua eius loquetur indicium

"There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii)

http://www.myspace.com/whiteflags99
whiteflags is offline   Reply With Quote
Old 11-05-2009, 06:12 AM   #4
Embedded in C...
 
Join Date: Sep 2008
Location: Basingstoke, Hampshire
Posts: 65
Thanks for that info Rocky, that sounds like just what I need. Just to clarify, I invoke this function when I am declaring my arrays? Also, can I insert this code into a struct member declaration, or would I have to declare my arrays separately first?

--dave

Last edited by droseman; 11-05-2009 at 06:14 AM. Reason: extra info
droseman is offline   Reply With Quote
Old 11-05-2009, 06:30 AM   #5
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
You are most welcome Enjoy C/C++ programming
RockyMarrone is offline   Reply With Quote
Reply

Tags
array

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
coping elements not in array lord C++ Programming 2 08-04-2008 07:53 PM
way to check 3 elements of array and set 4th Syneris C++ Programming 3 01-09-2006 11:30 AM
Class Template Trouble pliang C++ Programming 4 04-21-2005 04:15 AM
Struct *** initialization Saravanan C Programming 20 10-09-2003 12:04 PM
A simple question about selecting elements in an array Unregistered C++ Programming 1 08-30-2001 10:37 PM


All times are GMT -6. The time now is 08:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22