Like Tree6Likes
  • 1 Post By camel-man
  • 1 Post By ch4
  • 1 Post By christop
  • 1 Post By jimblumberg
  • 2 Post By grumpy

Problem in understanding few things related to FUNCTION RETURNING POINTER

This is a discussion on Problem in understanding few things related to FUNCTION RETURNING POINTER within the C Programming forums, part of the General Programming Boards category; I am referring a book programming in c by Kochan and i am not getting few things on a particular ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    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
    printf ("Not found.\n");
    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. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    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.
    Last edited by camel-man; 11-03-2012 at 08:23 AM.
    prateek88 likes this.

  3. #3
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    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.
    prateek88 likes this.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by ch4 View Post
    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.
    prateek88 likes this.

  5. #5
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Last edited by ch4; 11-03-2012 at 09:31 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    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
    prateek88 likes this.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    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.

  8. #8
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Quote Originally Posted by jimblumberg View Post
    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.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by ch4 View Post
    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).

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    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.
    stahta01 and prateek88 like this.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    @grumpy
    You brought chaos in my mind.... but after reading your post to the end i grasped what you tried. Thanks a tone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer related problem
    By shashinitjsr in forum C Programming
    Replies: 2
    Last Post: 06-25-2011, 02:37 AM
  2. Returning pointer from function question
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 07-12-2007, 09:45 AM
  3. Need help understanding how to store things in C++
    By DemonSpeeding in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2006, 01:16 PM
  4. Just want some clarification on some game related things
    By Rumproast23 in forum Game Programming
    Replies: 5
    Last Post: 06-02-2006, 07:08 AM
  5. function returning pointer
    By blue_gene in forum C Programming
    Replies: 7
    Last Post: 04-19-2004, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21