3

User will pass char pointer to my function printString(), I need to check if its valid string or not, before I print it to the screen or process the string

   void printString(char *str)
   {
       print("%s",str); // in this case it will print grabage
   }
   void main()
   {
       char temp[100];
       printString(temp);
   }

The solution I need is:

 void printString(char *str)
 {
    if str is valid:
         print("%s",str);
    else
    {
         str[0] = '\0';  //or using memset() make it valid
         print("%s",str);
    }
 }
Bulat
  • 720
  • 7
  • 15
masternone
  • 429
  • 5
  • 13
  • 9
    You cannot. Sorry about that. – n. m. could be an AI Jun 06 '19 at 05:01
  • 2
    This is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). The Y part ("solution") you're asking about just happens to be impossible in this case. – melpomene Jun 06 '19 at 05:09
  • As @n.m. said, this is impossible because c does not know the type of anything **but** you can check if it is a `NULL` pointer or if there a `\0` somewhere. (But it may exist somewhere if it's not a String too) You can also check it before calling the function. – dan1st Jun 06 '19 at 05:09
  • What do you mean by valid string here? Explain. – shubhamr238 Jun 06 '19 at 05:12
  • 2
    @dan1st "*you can check if it is a `NULL` pointer*" - yes. "*or if there a `\0` somewhere*" - no, you can't. "*But it may exist somewhere if it's not a String too*" - no, if it has a `'\0'`, it is a valid string by definition. – melpomene Jun 06 '19 at 05:12
  • @melpomene I meant that there may be a `'\0'` randomly. – dan1st Jun 06 '19 at 05:13
  • @dan1st ... which makes it a string. C does know the type of everything; it's just that "string" is not a type in C. I agree that knowing if it is a string would not help OP; they're actually looking for an "is this a not-garbage string?" test, which is impossible. – melpomene Jun 06 '19 at 05:14
  • @JosephSible No, our OP is trying to test the contents of the pointed-to memory (without knowing how big they are), not the pointer itself. – melpomene Jun 06 '19 at 05:15
  • I know but I think the author of the question wanted to know if it is a garbage (String) – dan1st Jun 06 '19 at 05:15
  • @dan1st Yeah, I just edited my comment. I actually agree with you. – melpomene Jun 06 '19 at 05:16
  • thankyou for all, i came to conclusion, it is not possible, i'll ask user to check it before calling the function or add additional parameter in the function to denote validity – masternone Jun 06 '19 at 05:17
  • `void main()` needs to be `int main(void)`. In response to your latest comment, what's the point of asking the user whether the argument is valid? Why not just require it to be valid? – Keith Thompson Jun 06 '19 at 05:36
  • @KeithThompson What's the point... of validating input data? That is your question? – Andrew Koster Oct 23 '21 at 21:43
  • @AndrewKoster That was more than two years ago, but ... the OP is talking about "validating" the input data by asking the user whether it's valid. – Keith Thompson Oct 23 '21 at 22:18
  • @KeithThompson It just blows my mind that when someone asks "how do I validate input data", the consensus answer is "you physically can't, and why would you want to?" Everyone's correct that it's physically impossible in this case, but there are a lot of obvious reasons why someone would want to do it. – Andrew Koster Oct 23 '21 at 22:44
  • @AndrewKoster I never said that validating input data is pointless. I said that validating input data *by asking the user whether it's valid* is pointless. – Keith Thompson Oct 24 '21 at 02:43

1 Answers1

4

You cannot do this.

You can determine whether a char* value is a null pointer or not, but if it's non-null, you cannot determine whether it's a valid pointer. You certainly can't do so portably; you probably can't do it non-portably either.

For example:

char *ptr = malloc(6);
strcpy(ptr, "hello");
// now ptr points to a valid string
free(ptr);
// now ptr doesn't point to a valid string -- but there's no way to test it

If you happen to know that the pointer is valid (or is either valid or null), you still can't do it. For example:

char buf[5] = "hello"; // not a string, since there's no '\0'
char *ptr = buf;       // ptr points to valid data, but it's not a string

You can scan memory starting at *ptr looking for a '\0' string terminator, but you can't know when to stop looking. In this case, you might look at 5 bytes of memory and not find a '\0' -- but what if ptr pointed to a valid string that's 1000 characters long?

And in this case, it's entirely possible that there happens to be a '\0' byte immediately after the array, which could make it appear that ptr points to a valid string. (Accessing that 6th byte has undefined behavior. The worst consequence of undefined behavior is that it can appear to "work", which just means it's more difficult to track down the bug.)

The only way to determine whether a pointer points to a valid string is to write your code in a way that guarantees that it does. If it doesn't, it's not a problem you can detect after it occurred.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631