开发者

How do we know that a string element in C is uninitialized?

开发者 https://www.devze.com 2022-12-18 21:21 出处:网络
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 ind

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] =开发者_StackOverflow中文版 '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;

}


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'.


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.


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.


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.


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'


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消