0

I have a homework in C for this semester, and the teacher said I should write this function not that complicated. I'm new to programming, and I can't seem to find the answer. He said we did some of this type of stuff in class, but I couldn't find anything like this.

int checkforerror(int argc){
    if (argc != 3) {
        fputs("Too little or too many arguments!\n", stderr);
        exit(EXIT_FAILURE);
    }
}

Also he pointed out, that I have to check for nullpointer in a function. I found some nullpointer check in some programs which we wrote in class, but I'm not sure it's good this way.

int mycmp(char *s1, char *s2){

    if (!(s1 && s2))
       return EXIT_FAILURE;

So we did something like this in lessons. Also I tought about this:

if (s1 == NULL && s2 == NULL)
   return EXIT_FAILURE;

I don't really know whether they are really null pointer checks and which should I use.

Can you please help me with these?

delirium
  • 868
  • 6
  • 20
Alex Bese
  • 11
  • 1
  • 3
  • 1
    `if (s1 == NULL && s2 == NULL) return EXIT_FAILURE;` **-->>** `if (!s1 || !s2 ) return EXIT_FAILURE;` – joop Dec 14 '18 at 12:02
  • I believe what you seek was already answered [here](https://stackoverflow.com/questions/3825668/checking-for-null-pointer-in-c-c). – delirium Dec 14 '18 at 12:03
  • The most readable version is `if (s1 == NULL || s2 == NULL)`. – Lundin Dec 14 '18 at 13:43

4 Answers4

2

According to De_Morgan's_laws

(not (A and B) = not A or not B)

Your condition

if (!(s1 && s2)) 

is the same as

 if (!s1 || !s2))  //same as if (s1 == NULL || s2 == NULL)

Here you are checking if any of s1 or s2 is NULL and if so you exit.


But in second one

if (s1 == NULL && s2 == NULL)
   return EXIT_FAILURE;

if both s1 and s2 are NULL then you are exiting.

If any of s1 or s2 are not NULL you will proceed further and will try to access them, thus invoking undefined behavior.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
1

If you want to return EXIT_FAILURE when any of the pointers is NULL, use the first option, i.e.,

if (!(s1 && s2))
       return EXIT_FAILURE;

If you want to return EXIT_FAILURE when both pointers are NULL, use the second option, i.e.,

if (s1 == NULL && s2 == NULL)
   return EXIT_FAILURE;

The actual value of NULL is implementation defined. So it is better not to perform logical operations that assume the value of NULL to be a particular value.

P.W
  • 26,289
  • 6
  • 39
  • 76
1

Just to complete other answers, it seems you are confused with the definition of null pointers (and perhaps pointers at all).

Briefly going through those two points: A pointer, in C, is a variable that points to another "object". More specifically, a pointer is a variable that stores a memory address, in which you have some other data. Check [1] for more examples. A null pointer, in the context of pointers, indicates that a pointer variable is pointing "nowhere" [2]. This is generally used to distinguish whether you are pointing to some valid (i.e., not null) or invalid (i.e., null) data.

delirium
  • 868
  • 6
  • 20
  • 1
    Now one must also distinguish wild- and dangling-pointers from those. And you probably meant *object* instead of *"variable"*. – Deduplicator Dec 14 '18 at 12:41
  • Indeed for both your remarks! On your first point, I tried to provide a *short* answer as the question was not *well-defined* (even though those are important concepts to solve his doubts). – delirium Dec 14 '18 at 13:03
0

In addition to what others have said about using if (s1 == NULL || s2 == NULL), I also wanted to make mention of the next line i.e. the return EXIT_FAILURE. I'm not sure why you are doing this.

EXIT_FAILURE, and EXIT_SUCCESS are macro constants usually used with the exit function to indicate failure or success (respectively) of a program. Returning them in a function which looks like it is doing string comparison, just doesn't make sense. Do you even know what the value of those constants are? EXIT_SUCCESS may be zero, but what is the value of EXIT_FAILURE

You should consider using values that actually make sense for that function i.e.

  • 0 for equal
  • 1 if s1 is greater than s2
  • -1 otherwise.
smac89
  • 39,374
  • 15
  • 132
  • 179