Thread: Array Question

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Array Question

    question:

    When using arrays initialized as char, how would I check to see if there is anything in the array.

    example:

    char test[N]

    if(test[N] == '??????')


    Would there be a '\0' or would there just be a zero in the array even though it is declared as a char?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    First off, in the example the array is not very likely even initalized. Secondly, that is not how you compare strings in C.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Hmmm. Depends on where the array was defined. If outside of main (in global), it will be initialized to all zeroes. No matter if the array is char, int, float. If the array is defined inside a function there could be any sort of garbage in there. Do not rely on any value being there if you didn't initialize it yourself.

    Yes that is the correct way to compare a single character in the array. But if array is of size N (fixed size), then element N is past the end of it. But I think you meant 'N' as some generic value and not necessarily a common variable.
    Last edited by nonoob; 09-29-2008 at 07:01 PM.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    yeah that is correct, N is just an arbitrary number. Thanks, yeah the way I have it in my code that the variable is global.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah good catch. I thought this was relevant to his other question, so I guess upon answering it I was keeping his other code in mind. I don't know what I would do without you today, nonoob. His sample array, if it came from his other code is not a global.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM