How to check whether an array element (position) is empty...

Hey there!,
How would I go about and what would be the condition to check whether an array position is empty in C. Would I compare with another string using strcmp, or are empty array elements == NULL??
I was told that unitialised Int arrays have their values as 0, but what would be the value of a char array? And how would I check whether it is empty so I can store my required string in it?
Thanks for any solution!
Colonel Mustard (who did it in the study)



Comments

  • : Hey there!,
    : How would I go about and what would be the condition to check whether an array position is empty in C. Would I compare with another string using strcmp, or are empty array elements == NULL??
    : I was told that unitialised Int arrays have their values as 0, but what would be the value of a char array? And how would I check whether it is empty so I can store my required string in it?
    : Thanks for any solution!
    : Colonel Mustard (who did it in the study)
    :
    :
    Depending on your complier, VARIABLES ARE NOT INITIALIZED IN C. Therefore, you would have to declare the array:

    int my_array[50];

    And then initalize it yourself:

    int element;
    for (element = 0; element <= 50; element++)
    my_array[element] = NULL;

    Then you could do your little thing with comparing it to NULL.

    For char arrays, the idea is the same. Unlike BASIC, C treats char as numbers. As you might know, each letter has a value that corresponds to it called the ASCII code. You can do the same thing as above, just declare the array as a char type instead.

    char my_array[50]; //Yeah, I happen to like the number 50.

    And this may be more information than you want, but because C does not initalize varaibles, the value in it is the value left in memory from some other program.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories