0

I am working on a Circularly-Doubly-Linked List with a sentinel in the C language with the 99 standard. The headers I am allowed to use for class are stdio.h, stdlib.h, and assert.h only.

The sentinel is a struct and the structs have a value data member which is a double value. I was wondering if there is a substitute for NULL with doubles? It appears NULL can be used for integer data types but not doubles. I tried using Nan and NAN but the compiler is not accepting these. I considered using an arbitrary number, but wanted to know if there was a specific stand-in for NULL in this particular situation?

  • 1
    Do you realize that "NULL" is just 0 casted to (void *)? Ergo the equivalent for double would be 0.0 -- you could theoretically use NULL for a double it would just throw implicit type conversion warnings, as long as that NULL is actually 0 on your system. – user6567423 Feb 11 '19 at 22:00
  • 6
    I very much doubt you want to use `NULL`. What are you actually trying to accomplish here? In what context are you trying to use NAN? It would probably be helpful to post some code as it is much easier to make sense of that than it is paragraphs of text describing code. – Christian Gibbons Feb 11 '19 at 22:02
  • Also can you clarify what you mean by "It appears NULL can be used for integer data types but not doubles." -- Why not? I can assign NULL to a double just fine with some explicit casts: double d = (double)(size_t)NULL; Obviously this is a deeper issue of not knowing how to handle what you're trying to do, and assuming that using 'NULL for a double' is the solution. I suggest you try to post exactly what you're trying to achieve and what you've tried so far. – user6567423 Feb 11 '19 at 22:03
  • 3
    `NULL` is intended to be used for pointers, not `int`s – Govind Parmar Feb 11 '19 at 22:04
  • Do you really need sentinel nodes? Would you be better off using null pointers instead? If you can’t get a NaN any other way, convert the string with `strtod()` from ``. Note that NULL is not always suitable for assigning to integers. One of the many banes of my life is code equivalent to `char c = NULL;` which generates warnings about casting a pointer to an integer of a different size. – Jonathan Leffler Feb 11 '19 at 22:04
  • NULL is typically used to denote either a zero address or as 'string terminator'(null character). Typically, NULL is zero. If you are storing a double value, then you could simply store 0.0. However, 0.0 is a valid double value and that could possibly be a value in your test datasets. What is the purpose of a storing a 'NULL' double value. Normally a node in a circular doubly linked list would contain a 'next' pointer, a 'previous' pointer, and a value. A list sentinal minimally stores a pointer to a node – Tom Drake Feb 11 '19 at 22:05
  • Is DBL_TRUE_MIN defined on your platform? If not, you can probably define it locally yourself: `#define DBL_TRUE_MIN 4.9406564584124654e-324 // min positive value` … but I'm not entirely sure I know what you are doing. Consider posting some code to show what you've tried or are proposing. – selbie Feb 11 '19 at 22:05
  • Possible duplicate of [NaN Literal in C?](https://stackoverflow.com/questions/5714131/nan-literal-in-c) – Andrew Sun Feb 11 '19 at 22:21
  • Why not just add an `isSentinel` member to the struct? – user3386109 Feb 11 '19 at 22:21
  • Why do you need to recognize the sentinel structure by *any* value, or even by a flag? The list is circular, so you ought always to be able to know which element it is by *position*. Knowing that, its value is irrelevant. – John Bollinger Feb 11 '19 at 22:54
  • @ChristianGibbons: Of course OP does not want to use `NULL`. They request a substitute for `NULL`, not `NULL`. This is stated several times in the question and its title. And they tell you want they want to accomplish: They want a sentinel value, a value that will not be used for normal data but that can be used to indicate the end of a list. The question is clear in this regard. – Eric Postpischil Feb 11 '19 at 23:59

1 Answers1

3

NaN is an appropriate value for the purpose you seek. C provides a symbol NAN which can be used to represent a NaN. However, a definition of NAN is provided in <math.h>, which is not in the headers you are allowed to use. Instead, you can obtain a NaN via double NaN = strtod("NAN", 0);, after including <stdlib.h>. (Credit to chux for the reminder.)

It seems odd an assignment would require you to use a sentinel value in a double without giving you access to <math.h>. You should likely use a null pointer in a pointer member of the structure rather than trying to use a double to mark the end of the list. Or, if not a null pointer, add another member to the structure to use to mark the end. Or identify the end by remember where you entered the list—when you have traversed a circular list back to your starting point, you have completely traversed the list.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312