views:

261

answers:

7

Is there a way to know whether the element in a string in C has a value or not? I have tried using NULL, '', and ' ', but they don't seem to be working. I need to shift the characters down to index 0 without using stdlib functions.

#include <stdio.h>

int main()
{  
   char literal[100];

   //literal[99] = '\0'
   literal[98] = 'O';
   literal[97] = 'L';
   literal[96] = 'L';
   literal[95] = 'E';
   literal[94] = 'H';

   int index = 0;

   while(literal[index] != '\0')
   {
      if(literal[index] == NULL)   // does not work
         printf("Empty");              

      else
         printf("%c", literal[index]);

      ++index;                  
   }

   getchar();
   return 0;

}

+3  A: 

No, you can't do that. This is because it will have a value - there is just no way of knowing what that value is. This is why it is essential to initialise things to known values.

anon
+1  A: 

C does not default initialize anything. Therefore the contents in your string are whatever garbage was in that memory by whatever last used it on the stack. You need to explicitly set each literal value to a value that means "unset" to you.

Doug T.
+1  A: 

No, there is no way of knowing what value the array has. You can, however, initialize it with a chosen "default" value of your choice and later check against that.

Håvard S
+1  A: 

You need to set the end of the string to 0 (zero) or '\0' - C only does this for you automatically for string literals, not local variables on the stack

Try

memset(&literal, 0, 100);

Or just uncomment your line that sets literal at index 99 to '\0'

Sam Post
`NULL` is not `'\0'` - the former is the null pointer constant, the latter is an `int` equal to 0.
Alok
Alok is right. On most (all?) current architectures they have compatible representation, but that is not guaranteed and should never be assumed.
dmckee
Great point thanks for the reminder, I'll edit my answer
Sam Post
+6  A: 

No. Since literal has automatic storage, its elements will not be initialized, the values in the array is undefined.

You could initialize every element to something special and check for that value. e.g. you could change

char literal[100];

char literal[100] = {0};

to initialize every element to 0. You'd have to change your while loop termination check to

while(index < 100) {
  if(literal[index] == 0) 
         printf("Empty"); 
   ...
  }
}

That might not be optimal if you need to perform more string manipulation on the array though, as 0 now means empty element and also 'end of string'.

nos
A: 

A c string is just a bunch of memory locations and a convention that '\0' marks the end. There is no compiler enforcement, and no attached meta data (unless you build a structure to provide it).

Every cell in memory, always has a value, so every string always has a value, you just can't guarantee that it is sensible or even that it ends in the allotted space.

Insuring that there is meaningful data in there is your responsibility, which suggests that you should initialize all strings either at declaration time or immediately after allocation. Exception to the rule are rare and are undertaken at your own risk.

dmckee
A: 

No, that is undefined behaviour as the runtime, for all we care could shove in a few binary ASCII characters, you really do not want to get into that. The best way to deal with it is to use a for loop and iterate through it or use calloc which initializes a pointer but sets it to 0.

for (i = 0; i < 100; i++) literal[i] = '\0';

OR

char *literalPtr = (char*)calloc(100, sizeof(char)); // Array of 99 elements plus 1 for '\0'

There is absolutely no guarantee in doing that. Hence it would be classified as undefined behaviour as it is dependent on the compiler and runtime implementation.

Hope this helps, Best regards, Tom.

tommieb75