Thread: Problem in understanding few things related to FUNCTION RETURNING POINTER

1. Problem in understanding few things related to FUNCTION RETURNING POINTER

I am referring a book programming in c by Kochan and i am not getting few things on a particular code.

Code:
```#include <stdio.h>
struct entry
{
int value;
struct entry *next;
};
struct entry *findEntry (struct entry *listPtr, int match)
{
while ( listPtr != (struct entry *) 0 )
if ( listPtr->value == match )
return (listPtr);
else
listPtr = listPtr->next;
return (struct entry *) 0;
}
int main (void)
{
struct entry *findEntry (struct entry *listPtr, int match);
struct entry n1, n2, n3;
struct entry *listPtr, *listStart = &n1;
int search;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = 0;
printf ("Enter value to locate: ");
scanf ("%i", &search);
listPtr = findEntry (listStart, search);
if ( listPtr != (struct entry *) 0 )
printf ("Found %i.\n", listPtr->value);
else
return 0;
}```
First thing is the (struct entry *) 0 expression in while loop and other one is return (struct entry *) 0;

Also does the struct entry *findEntry and struct entry * findEntry(with space asterik and function name) makes any difference

2. I am pretty sure that (struct entry *) 0 is just another way of saying NULL, However I am not nearly as experienced as a lot of members on here, they will be better suited to answer this question. I do not know why they return (struct entry *) 0 if it is NULL.

Also does the struct entry *findEntry and struct entry * findEntry(with space asterik and function name) makes any difference
No It does not make a difference. They both are returning a pointer, regardless of the white space.

3. 1. Here's the deal. As you propably know all pointers pointing somewhere in memory that's why are holding mysterious numbers. If you need a pointer to point to nowhere or you want to initialize it or pointers operation fail (malloc ..etc) or you want to mark specially a pointer (such like the last pointer of a list), you make it point to NULL.
On almost every compilation NULL is the definition of zero value.
In your case zero acting like NULL. (struct entry *) is cast just to get away from compilers warnings.

4. Originally Posted by ch4
On almost every compilation NULL is the definition of zero value.
In your case zero acting like NULL. (struct entry *) is cast just to get away from compilers warnings.
On all conforming implementations a 0 in a pointer context is always a null pointer. The compiler, I believe, does not have to print a warning when a plain 0 is implicitly converted (without a cast) to a pointer.

The actual bit pattern of a null pointer in memory or in a register is not necessarily all zeroes, but that doesn't matter to (most) C programmers.

5. Be careful with that first link, C++ doesn't use the same value for NULL as used in C. And in C NULL is equivalent to 0 only when dealing with pointers.

Jim

6. well, i guess its just for casting purpose, but my compiler(gcc) doesn't show any warnings without using the cast either. So, don't know if there is a certain reason to include that.

7. Originally Posted by jimblumberg
Be careful with that first link, C++ doesn't use the same value for NULL as used in C. And in C NULL is equivalent to 0 only when dealing with pointers.
That's why we don't have to care about the value of NULL. Just use it as "NULL" Simple as pie.
In reality i don't know if NULL is being used for other purposes except pointers. Not sure.

8. Originally Posted by ch4
In reality i don't know if NULL is being used for other purposes except pointers. Not sure.
It shouldn't be used for anything except pointers. For example, the string "null terminator" also has a value of 0, and using NULL in that context would work, but it would be very bad style (it might belong on IOCCC).

9. Hmmm. This sort of discussion is just more evidence that the notion of null versus NULL pointers is unnecessarily complicated in C.

I'll offer some comments in the hope of clarifying. Having seen numerous religious debates (some technically sound, some flawed, all emotive) over such things, however, I won't hold my breath in the hope that I will succeed in clarifying the matter.

A null pointer is a pointer with a unique value that is guaranteed to not be equal to the address of any valid object or function. So a null pointer can only compare equal with another null pointer. The unary & (aka "address of") operator also can never yield a null pointer. A malloc() call is guaranteed to produce a non-null pointer if it succeeds, and a null pointer if it fails (eg if it cannot allocate requested memory).

In C, converting a zero value to a pointer is one way of producing a null pointer. This is a convention (supported by the compiler) and does not mean a null pointer has a value of zero. It does mean a test of the form "pointer == 0" yields a true result (since the 0 will be converted implicitly to the pointer type, before doing the comparison). This is also the reason that "(struct entry *)0" produces a null pointer.

The NULL macro is simply a utility of convenience introduced in the C library. It is guaranteed to yield a value of zero, that can be converted to any pointer type. The intent of the NULL macro is that it be used in pointer expressions. However, as specified, it can also often be used in non-pointer expressions (eg it can simply expand to a value of zero). It is considered poor style in C to use the NULL macro in anything other than pointer expressions. For this reason some - but not all - compilers and libraries are constructed so using NULL in anything except a pointer expression yields a compiler warning. Some compilers and libraries are more aggressive, so code that uses NULL in anything except a pointer expression will cause a compilation error.

Some C programmers also consider that converting zero to a pointer (e.g. "(struct entry *)0") is poor style, and encourage using NULL instead "(struct entry *)NULL". Other C programmers prefer the opposite.

In the C++ world, however, using the NULL macro is also often considered poor style (some go so far as to describe it as an unnecessary anachronism). So the encouraged way of producing a null pointer has been to convert a zero literal to a pointer (e.g. "(struct entry *)0"). Some C++ programmers think this is not typesafe, so (in the new C++ standard released in 2011) a new keyword (nullptr) has been introduced, which is guaranteed to produce a null pointer, and will trigger a compiler error if used as anything other than a pointer.

10. @grumpy
You brought chaos in my mind.... but after reading your post to the end i grasped what you tried. Thanks a tone.