Portable way to check if a char* pointer is a null-terminated string

后端 未结 5 1133
感情败类
感情败类 2020-12-11 07:32

I have a C function that takes in a char* pointer. One of the function\'s preconditions is that the pointer argument is a null-terminated string

void foo(cha         


        
5条回答
  •  醉话见心
    2020-12-11 07:53

    As others have pointed out, there is no portable way to do this. The reason is that it isn't useful.

    Normal semantics are to check for NULL only, and assume if a non-NULL is passed, it's valid. After all, there's likely to be a NULL somewhere after your pointer. The only other possibility is that you run into unmapped memory. It's more likely however that even with a bogus pointer, you find a NULL. That means a bogus 2000 character string will still get past the check.

提交回复
热议问题