what to do with unused array elements

This is a discussion on what to do with unused array elements within the C Programming forums, part of the General Programming Boards category; Hello, I have some arrays in my code which will contain different messages of varying lengths, so I have sized ...

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    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

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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}

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    The third argument to memset is computed like (sizeof(array[0]) * MAX_NUMBER), because the function expects a number of bytes.

  4. #4
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    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 05:14 AM. Reason: extra info

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    You are most welcome Enjoy C/C++ programming

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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