How to find out if an int has no value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard

    How to find out if an int has no value

    I've really tried searching for this before I thought of bother you guys
    with this question. It's such a simple thing, but it's driving me nuts!
    Is it possible to check if an int (or any other type) has no value.
    something like:

    int i;
    ...
    ...
    ...
    if (i==NULL){
    ......
    }

    I know this doesn't work and I know NULL is a pointer. Is there any way
    this can be done?
    TIA.
  • Joona I Palaste

    #2
    Re: How to find out if an int has no value

    Richard <richardhengeve ld@hotmail.com> scribbled the following:[color=blue]
    > I've really tried searching for this before I thought of bother you guys
    > with this question. It's such a simple thing, but it's driving me nuts!
    > Is it possible to check if an int (or any other type) has no value.
    > something like:[/color]

    Scalar types, such as ints, always have values. Therefore your question
    does not make sense.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Parthenogeneti c procreation in humans will result in the founding of a new
    religion."
    - John Nordberg

    Comment

    • Richard

      #3
      Re: How to find out if an int has no value

      Joona I Palaste <palaste@cc.hel sinki.fi> wrote in
      news:cihhuo$jqa $1@oravannahka. helsinki.fi:
      [color=blue]
      > Richard <richardhengeve ld@hotmail.com> scribbled the following:[color=green]
      >> I've really tried searching for this before I thought of bother you
      >> guys with this question. It's such a simple thing, but it's driving
      >> me nuts! Is it possible to check if an int (or any other type) has no
      >> value. something like:[/color]
      >
      > Scalar types, such as ints, always have values. Therefore your
      > question does not make sense.
      >[/color]

      Okay, than I should be more spesific.
      I've got this:

      typdef struct
      {
      int x;
      int y;
      } Point;

      // I now want to make a function that checks if a Point is empty

      int pointEmpty(Poin t p)
      {
      return (p /*isEmpty*/ ? 1 : 0)
      }

      How should I do that?
      TIA




      Comment

      • Gordon Burditt

        #4
        Re: How to find out if an int has no value

        >I've really tried searching for this before I thought of bother you guys[color=blue]
        >with this question. It's such a simple thing, but it's driving me nuts!
        >Is it possible to check if an int (or any other type) has no value.[/color]

        An int never has "no value". C is not like SQL where integer
        variables can have null as well as integer values. A C variable
        may have an uninitialized or undefined value, in which case it might
        have any value, but this is not distinguishable from a variable
        that is assigned that value. It might have some kind of trapping
        value, which can't be tested because if you try the program terminates.

        Pointers may have a NULL value, which is not the same thing as "no
        value".

        Gordon L. Burditt

        Comment

        • Nicolas Pavlidis

          #5
          Re: How to find out if an int has no value

          Richard <richardhengeve ld@hotmail.com> writes:
          [color=blue]
          > Joona I Palaste <palaste@cc.hel sinki.fi> wrote in
          > news:cihhuo$jqa $1@oravannahka. helsinki.fi:
          >[color=green]
          > > Richard <richardhengeve ld@hotmail.com> scribbled the following:[color=darkred]
          > >> I've really tried searching for this before I thought of bother you
          > >> guys with this question. It's such a simple thing, but it's driving
          > >> me nuts! Is it possible to check if an int (or any other type) has no
          > >> value. something like:[/color]
          > >
          > > Scalar types, such as ints, always have values. Therefore your
          > > question does not make sense.
          > >[/color]
          >
          > Okay, than I should be more spesific.
          > I've got this:
          >
          > typdef struct
          > {
          > int x;
          > int y;
          > } Point;
          >
          > // I now want to make a function that checks if a Point is empty
          >
          > int pointEmpty(Poin t p)
          > {
          > return (p /*isEmpty*/ ? 1 : 0)
          > }[/color]

          How can a point be empty? As said by Joona scalar Types always have
          values, but you're responsible that they get initialized correctly.

          Or do you want to check if the point is (0,0)?

          Kind regards,
          Nicolas

          --
          | Nicolas Pavlidis | Elvis Presly: |\ |__ |
          | Student of SE & KM | "Into the goto" | \|__| |
          | pavnic@sbox.tug raz.at | ICQ #320057056 | |
          |-------------------University of Technology, Graz----------------|

          Comment

          • Gordon Burditt

            #6
            Re: How to find out if an int has no value

            >Okay, than I should be more spesific.[color=blue]
            >I've got this:
            >
            >typdef struct
            >{
            > int x;
            > int y;
            >} Point;
            >
            >// I now want to make a function that checks if a Point is empty[/color]

            A Point is never empty, unless you add an element to it that
            can be used to mark it empty.

            typedef struct
            {
            int x;
            int y;
            int z;
            int isempty;
            int isgreen;
            } Point;

            Now, is it acceptable for a Point to be empty and green at the same
            time?

            Gordon L. Burditt

            Comment

            • Richard

              #7
              Re: How to find out if an int has no value

              Nicolas Pavlidis <pavnic@sbox.tu graz.at> wrote in news:2r318iF13t vdiU2@uni-
              berlin.de:
              [color=blue][color=green]
              >> Okay, than I should be more spesific.
              >> I've got this:
              >>
              >> typdef struct
              >> {
              >> int x;
              >> int y;
              >> } Point;
              >>
              >> // I now want to make a function that checks if a Point is empty
              >>
              >> int pointEmpty(Poin t p)
              >> {
              >> return (p /*isEmpty*/ ? 1 : 0)
              >> }[/color]
              >
              > How can a point be empty? As said by Joona scalar Types always have
              > values, but you're responsible that they get initialized correctly.
              >
              > Or do you want to check if the point is (0,0)?
              >
              > Kind regards,
              > Nicolas
              >[/color]

              I want to make a fail save for myself. So, if I'd wrote

              Point p;

              somewhere without initializing it, I could check it whit my

              pointEmpty(p);

              function.
              I'm beginning to understand that I should be working with extra flags or
              something, if I insist in wanthing this. Am I correct?

              Kind regards,
              Richard

              Comment

              • Richard

                #8
                Re: How to find out if an int has no value

                gordonb.tdffa@b urditt.org (Gordon Burditt) wrote in news:cihj0j$6s5
                @library2.airne ws.net:
                [color=blue]
                >
                > A Point is never empty, unless you add an element to it that
                > can be used to mark it empty.
                >
                > typedef struct
                > {
                > int x;
                > int y;
                > int z;
                > int isempty;
                > int isgreen;
                > } Point;
                >
                > Now, is it acceptable for a Point to be empty and green at the same
                > time?
                >
                > Gordon L. Burditt
                >
                >[/color]

                I just kind of figured this out myself!
                Thanks for replaying.

                Regards,
                Richard

                Comment

                • Joona I Palaste

                  #9
                  Re: How to find out if an int has no value

                  Richard <richardhengeve ld@hotmail.com> scribbled the following:[color=blue]
                  > Nicolas Pavlidis <pavnic@sbox.tu graz.at> wrote in news:2r318iF13t vdiU2@uni-
                  > berlin.de:[color=green][color=darkred]
                  >>> Okay, than I should be more spesific.
                  >>> I've got this:
                  >>>
                  >>> typdef struct
                  >>> {
                  >>> int x;
                  >>> int y;
                  >>> } Point;
                  >>>
                  >>> // I now want to make a function that checks if a Point is empty
                  >>>
                  >>> int pointEmpty(Poin t p)
                  >>> {
                  >>> return (p /*isEmpty*/ ? 1 : 0)
                  >>> }[/color]
                  >>
                  >> How can a point be empty? As said by Joona scalar Types always have
                  >> values, but you're responsible that they get initialized correctly.
                  >>
                  >> Or do you want to check if the point is (0,0)?[/color][/color]
                  [color=blue]
                  > I want to make a fail save for myself. So, if I'd wrote[/color]
                  [color=blue]
                  > Point p;[/color]
                  [color=blue]
                  > somewhere without initializing it, I could check it whit my[/color]
                  [color=blue]
                  > pointEmpty(p);[/color]
                  [color=blue]
                  > function.
                  > I'm beginning to understand that I should be working with extra flags or
                  > something, if I insist in wanthing this. Am I correct?[/color]

                  Yes you are. C provides no way whatsoever of checking whether a
                  variable has been initialised. The value of an uninitialised value is
                  undefined, you can't distinguish it from a value you have initialised.
                  You must use a separate flag, or always work with initialised values.

                  --
                  /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                  \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                  "Immanuel Kant but Genghis Khan."
                  - The Official Graffitist's Handbook

                  Comment

                  • Gordon Burditt

                    #10
                    Re: How to find out if an int has no value

                    >I want to make a fail save for myself. So, if I'd wrote[color=blue]
                    >
                    >Point p;
                    >
                    >somewhere without initializing it, I could check it whit my
                    >
                    >pointEmpty(p );
                    >
                    >function.
                    >I'm beginning to understand that I should be working with extra flags or
                    >something, if I insist in wanthing this. Am I correct?[/color]

                    If you forget to initialize a Point, you'll forget to initialize
                    the flag that says whether it's empty.

                    Gordon L. Burditt

                    Comment

                    • Richard

                      #11
                      Re: How to find out if an int has no value

                      gordonb.i60tn@b urditt.org (Gordon Burditt) wrote in news:cihjre$6s5
                      @library2.airne ws.net:
                      [color=blue][color=green]
                      >>I want to make a fail save for myself. So, if I'd wrote
                      >>
                      >>Point p;
                      >>
                      >>somewhere without initializing it, I could check it whit my
                      >>
                      >>pointEmpty(p) ;
                      >>
                      >>function.
                      >>I'm beginning to understand that I should be working with extra flags[/color][/color]
                      or[color=blue][color=green]
                      >>something, if I insist in wanthing this. Am I correct?[/color]
                      >
                      > If you forget to initialize a Point, you'll forget to initialize
                      > the flag that says whether it's empty.
                      >
                      > Gordon L. Burditt
                      >[/color]


                      That's true, unless the isempty flag has an intial value.
                      But then I should make a

                      initPoint(Point p)

                      function that initiates the point (and sets the isempty flag) and never
                      write something like

                      p.x=1;

                      which is not very elegant.
                      Or is it?
                      Is there any other way or should I just drop it?

                      Regards,
                      Richard

                      Comment

                      • xarax

                        #12
                        Re: How to find out if an int has no value

                        "Gordon Burditt" <gordonb.y69xw@ burditt.org> wrote in message
                        news:cihike$6s5 @library2.airne ws.net...[color=blue][color=green]
                        > >I've really tried searching for this before I thought of bother you guys
                        > >with this question. It's such a simple thing, but it's driving me nuts!
                        > >Is it possible to check if an int (or any other type) has no value.[/color]
                        >
                        > An int never has "no value". C is not like SQL where integer
                        > variables can have null as well as integer values. A C variable
                        > may have an uninitialized or undefined value, in which case it might
                        > have any value, but this is not distinguishable from a variable
                        > that is assigned that value. It might have some kind of trapping
                        > value, which can't be tested because if you try the program terminates.
                        >
                        > Pointers may have a NULL value, which is not the same thing as "no
                        > value".[/color]

                        And remember that an uninitialized pointer may
                        have any bit pattern whatsoever. So don't expect
                        a pointer to have an initial NULL value, unless
                        you explicitly code the assignment.

                        --
                        ----------------------------
                        Jeffrey D. Smith
                        Farsight Systems Corporation
                        24 BURLINGTON DRIVE
                        LONGMONT, CO 80501-6906

                        z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!



                        Comment

                        • Gordon Burditt

                          #13
                          Re: How to find out if an int has no value

                          >> If you forget to initialize a Point, you'll forget to initialize[color=blue][color=green]
                          >> the flag that says whether it's empty.
                          >>
                          >> Gordon L. Burditt
                          >>[/color]
                          >
                          >
                          >That's true, unless the isempty flag has an intial value.[/color]

                          I repeat: if you forget to initialize a Point, you'll also forget
                          to initialize the flag that says whether it's empty. There's no
                          way to declare a structure that automatically has one or more
                          elements initialized. You'll need to (and forget to) do this at
                          every variable declaration for a Point.
                          [color=blue]
                          >But then I should make a
                          >
                          >initPoint(Poin t p)
                          >[/color]
                          If you forget to initialize a Point, you'll forget to call
                          initPoint(). Oh, yes, you'll forget to check if it's empty
                          before using it, too.
                          [color=blue]
                          >write something like
                          >
                          >p.x=1;
                          >
                          >which is not very elegant.[/color]

                          If you forget to initialize a Point, you'll forget NOT to do that.
                          [color=blue]
                          >Or is it?
                          >Is there any other way or should I just drop it?[/color]

                          If you forget to initialize a Point, you'll forget it, so you
                          won't have to drop it.

                          There are some uses for having an "unspecifie d point". However,
                          trying to cover yourself for forgetting to initialize something
                          isn't one of them. You'll forget to initialize the flag. You'll
                          forget to test the flag before using the values. You'll forget
                          what to do if you encounter an "empty" point where one shouldn't
                          be empty.

                          Gordon L. Burditt

                          Comment

                          • Michael Mair

                            #14
                            Re: How to find out if an int has no value

                            Cheerio Richard,
                            [color=blue][color=green][color=darkred]
                            >>>I've really tried searching for this before I thought of bother you
                            >>>guys with this question. It's such a simple thing, but it's driving
                            >>>me nuts! Is it possible to check if an int (or any other type) has no
                            >>>value. something like:[/color]
                            >>
                            >>Scalar types, such as ints, always have values. Therefore your
                            >>question does not make sense.[/color]
                            >
                            > Okay, than I should be more spesific.
                            > I've got this:
                            >
                            > typdef struct
                            > {
                            > int x;
                            > int y;
                            > } Point;
                            >
                            > // I now want to make a function that checks if a Point is empty
                            >
                            > int pointEmpty(Poin t p)
                            > {
                            > return (p /*isEmpty*/ ? 1 : 0)
                            > }
                            >
                            > How should I do that?[/color]

                            As the language and its semantics give you no easy way out,
                            do it yourself:

                            Either you add another member to your struct to indicate
                            emptiness, e.g int is_empty.

                            Or you use x and y themselves:
                            Your Point members x and y probably are restricted
                            to a certain range of values; if not, disallow one specific
                            value, e.g. INT_MIN (from <limits.h>), and set x and y to
                            this value ASAP after declaring your struct/allocating the memory
                            for it:

                            #include <limits.h>
                            #include <assert.h>

                            #define NO_GPC_VAL INT_MIN

                            typedef struct GaussPlaneCoord inate {
                            int x;
                            int y;
                            } GP_Coord;


                            void Init_GP_Coords (size_t n, GP_Coord *GPC)
                            {
                            size_t i;
                            for (i=0; i<n; i++) {
                            GPC[i].x = GPC[i].y = NO_GPC_VAL;
                            }
                            }

                            int IsEmpty_GP_Coor d (GP_Coord *GPC)
                            {
                            return ( GPC->x==NO_GPC_VA L && GPC->y==NO_GPC_VA L );
                            }


                            int main (void)
                            {
                            GP_Coord Point;

                            Init_GP_Coords( 1, &Point);
                            ....


                            if (IsEmpty_GP_Coo rd(&Point)) {
                            /* Do init */
                            }

                            assert (!IsEmpty_GP_Co ord(&Point));
                            ....

                            return 0;
                            }

                            However, the second method has the disadvantage that you have to
                            check the range after certain ops.
                            Why did I choose INT_MIN? If you are using a two's complement
                            representation, it is often defined as -INT_MAX-1 so you are only
                            sort of symmetrizing the range.

                            Disclaimer: I did not check the code, just typed it here to give
                            you an idea.
                            Of course, with C99, you might want to use _Bool return type
                            for IsEmpty_GP_Coor d().


                            HTH
                            Michael

                            Comment

                            • Michael Mair

                              #15
                              Re: How to find out if an int has no value

                              Sorry,

                              forgot to make another point:

                              If you want to be sure that points are empty when they start their
                              life, you should malloc() each and every point and access them
                              only by pointer; you can do the allocation in a function that
                              not only malloc()s the memory but also initializes your stuff as
                              "empty".

                              In order to get a subtle enough kind of safety, create a typedef'ed
                              type only for pointers to points. If you don't have the type, you
                              cannot inadvertently create one uninitialized ;-)


                              Cheers,
                              Michael

                              Comment

                              Working...