pointer initialization

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

    #1

    pointer initialization

    hello,
    Need to know how the pointer can be initailzed to a particular
    location so that the value can be checked.

    void FlashWrite1(vol atile unsigned short *addr1,volatile unsigned short
    *addr11,unsigne d short ucVal )
    {

    unsigned short volatile *addr2;
    (*addr2)=(0x7F1 2);
    /* i get a warning that addr2 may not be initialized. I have tried
    different ways but it is not happening. here addr2 needs to be initailzed
    to 0x7f12 and the value passed by ucVal should be written into that
    location
    //&addr2=0x7F1 2;
    addr2=&ucVal;
    *addr2=ucVal;

    *addr1=*addr2;/* writing into required address*/
    *addr11=*addr2;/*writing into int flash*/

    }
  • sachin

    #2
    Re: pointer initialization

    Hi Risha,
    First I would like to point that volatile unsigned short is not the
    same as unsigned short volatile.

    Second, if you have to fill the value of the pointer addr2 to 0x&F12,
    then it should be done like this
    addr2 = 0x7F12;
    /* This is wrong, *addr2 = 0x7F12; */

    *addr2 = ucval;

    The rest of the statements are not specific to this function I think
    and could have been kept outside this function, may be in the calling
    function itself.

    Regards
    Sachin

    risha wrote:[color=blue]
    > hello,
    > Need to know how the pointer can be initailzed to a particular
    > location so that the value can be checked.
    >
    > void FlashWrite1(vol atile unsigned short *addr1,volatile unsigned[/color]
    short[color=blue]
    > *addr11,unsigne d short ucVal )
    > {
    >
    > unsigned short volatile *addr2;
    > (*addr2)=(0x7F1 2);
    > /* i get a warning that addr2 may not be initialized. I have tried
    > different ways but it is not happening. here addr2 needs to be[/color]
    initailzed[color=blue]
    > to 0x7f12 and the value passed by ucVal should be written into that
    > location
    > //&addr2=0x7F1 2;
    > addr2=&ucVal;
    > *addr2=ucVal;
    >
    > *addr1=*addr2;/* writing into required address*/
    > *addr11=*addr2;/*writing into int flash*/
    >
    > }[/color]

    Comment

    • Krishanu Debnath

      #3
      Re: pointer initialization

      [top posting fixed]
      [color=blue]
      >risha wrote:
      >
      >[color=green]
      >>hello,
      >> Need to know how the pointer can be initailzed to a particular
      >>location so that the value can be checked.
      >>
      >>void FlashWrite1(vol atile unsigned short *addr1,volatile unsigned
      >>
      >>[/color]
      >short
      >
      >[color=green]
      >>*addr11,unsig ned short ucVal )
      >>{
      >>
      >>unsigned short volatile *addr2;
      >>(*addr2)=(0x7 F12);
      >>/* i get a warning that addr2 may not be initialized. I have tried
      >>different ways but it is not happening. here addr2 needs to be
      >>
      >>[/color]
      >initailzed
      >
      >[color=green]
      >>to 0x7f12 and the value passed by ucVal should be written into that
      >>location
      >>//&addr2=0x7F1 2;
      >>addr2=&ucVa l;
      >>*addr2=ucVa l;
      >>
      >>*addr1=*addr2 ;/* writing into required address*/
      >>*addr11=*addr 2;/*writing into int flash*/
      >>
      >>}
      >>
      >>[/color]
      >
      >Hi Risha,
      >First I would like to point that volatile unsigned short is not the
      >same as unsigned short volatile.
      >
      >[/color]
      Really? How?

      Krishanu

      Comment

      • Christian Kandeler

        #4
        Re: pointer initialization

        sachin wrote:
        [color=blue]
        > First I would like to point that volatile unsigned short is not the
        > same as unsigned short volatile.[/color]

        Yes it is.


        Christian

        Comment

        • manoj1978@gmail.com

          #5
          Re: pointer initialization

          volatile int * p and int * volatile p are different. but volatile int i
          and int volatile i are same.

          Comment

          • Richard Bos

            #6
            Re: pointer initialization

            "sachin" <reachsachin@gm ail.com> wrote:

            [ Do not top-post. Corrected. ]
            [color=blue]
            > risha wrote:[color=green]
            > > Need to know how the pointer can be initailzed to a particular
            > > location so that the value can be checked.[/color][/color]
            [color=blue][color=green]
            > > unsigned short volatile *addr2;
            > > (*addr2)=(0x7F1 2);
            > > /* i get a warning that addr2 may not be initialized. I have tried
            > > different ways but it is not happening. here addr2 needs to be
            > > initailzed to 0x7f12 and the value passed by ucVal should be written
            > > into that location[/color][/color]

            Then you need to assign a value _to_ addr2, not to the object it points
            at. The warning is correct: at this moment, addr2 probably does not
            point at any object, and your dereference causes undefined behaviour.
            [color=blue]
            > First I would like to point that volatile unsigned short is not the
            > same as unsigned short volatile.[/color]

            Yes, it is.
            [color=blue]
            > Second, if you have to fill the value of the pointer addr2 to 0x&F12,
            > then it should be done like this
            > addr2 = 0x7F12;[/color]

            Nope. This is one of the few occasions in C where you _should_ use a
            cast. In fact, it's the one occasion where the cast actually makes
            sense. You're assigning something that is not a pointer value to a
            pointer object. This is seriously weird. In this case, it's _correctly_
            seriously weird, but you need to tell the compiler that yes, you do know
            what you're doing, and yes, you do want the type system to be
            circumvented just this once. For that, you need the cast:

            addr2 = (unsigned short volatile *)0x7F12;

            AFAICT it's OK to leave out the volatile in the cast, which saves some
            typing, but it's probably less head-scratch-provoking to leave it in.

            Richard

            Comment

            • Chris Torek

              #7
              Re: pointer initialization

              >> risha wrote:[color=blue][color=green][color=darkred]
              >>> Need to know how the pointer can be initailzed to a particular
              >>> location so that the value can be checked.
              >>> unsigned short volatile *addr2;[/color][/color][/color]
              [snippage]

              In article <422f244e.14786 694@news.indivi dual.net>
              Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:[color=blue]
              >Then you need to assign a value _to_ addr2 ...[/color]

              [right]
              [color=blue]
              >"sachin" <reachsachin@gm ail.com> wrote:[color=green]
              >> First I would like to point that volatile unsigned short is not the
              >> same as unsigned short volatile.[/color][/color]
              [color=blue]
              >Yes, it is.[/color]

              Indeed, one can write this as:

              short volatile unsigned *addr2;

              or:

              volatile short unsigned *addr2;

              or:

              unsigned volatile short *addr2;

              or any other number of combinations. I happen to prefer the order
              "volatile unsigned short" myself, but this is a matter of taste rather
              than correctness.
              [color=blue]
              >... This is one of the few occasions in C where you _should_ use a
              >cast. ...
              > addr2 = (unsigned short volatile *)0x7F12;
              >AFAICT it's OK to leave out the volatile in the cast, which saves some
              >typing, but it's probably less head-scratch-provoking to leave it in.[/color]

              It is OK but, as you did, I would include it. Morever, I would
              probably do at least this, if not something even fancier (depending
              on the ultimate application of the program):

              #define FLASH_DEVICE_AD DR ((volatile unsigned short *)0x7f12)
              ...
              volatile unsigned short *hw = FLASH_DEVICE_AD DR;
              ...
              *hw = SOME_MACRO; /* tell it to do X */
              error = flash_wait(hw); /* wait for response */
              if (error) ...
              *hw = SOME_OTHER_MACR O; /* tell it to do Y */
              error = flash_wait(hw); /* wait for response */
              if (error) ...

              Given the #define, the local variable is not actually required:

              *FLASH_DEVICE_A DDR = SOME_MACRO;

              will have the same effect. (But I would use the local variable
              here anyway. I would probably not name it just "hw" though --
              depending on what kind of hardware register it is, it would get
              an appropriate name reflecting that.)
              --
              In-Real-Life: Chris Torek, Wind River Systems
              Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
              email: forget about it http://web.torek.net/torek/index.html
              Reading email is like searching for food in the garbage, thanks to spammers.

              Comment

              • risha

                #8
                Re: pointer initialization

                addr2 = (unsigned short volatile *)0x7F12;
                Thank u very much for the help extended to one and all, specially
                richard sir i am grateful the statement above worked the way i
                wanted.I am a student and help like this makes us get back to our
                toes.

                thanks again,
                Risha

                Comment

                • CBFalconer

                  #9
                  Re: pointer initialization

                  risha wrote:[color=blue]
                  >
                  > addr2 = (unsigned short volatile *)0x7F12;
                  > Thank u very much for the help extended to one and all, specially
                  > richard sir i am grateful the statement above worked the way i
                  > wanted.I am a student and help like this makes us get back to our
                  > toes.[/color]

                  What statement above? You failed to quote anything, and I suspect
                  you changed the subject line again. Also the use of abbreviations
                  such as 'u' serve only to annoy and make things hard to read. A
                  sentence terminating '.' should be followed by at least one blank.
                  See my sig below if you insist on using the broken google groups
                  posting mechanism.

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson


                  Comment

                  • CBFalconer

                    #10
                    Re: pointer initialization

                    risha wrote:[color=blue]
                    >
                    > addr2 = (unsigned short volatile *)0x7F12;
                    > Thank u very much for the help extended to one and all, specially
                    > richard sir i am grateful the statement above worked the way i
                    > wanted.I am a student and help like this makes us get back to our
                    > toes.[/color]

                    What statement above? You failed to quote anything, and I suspect
                    you changed the subject line again. Also the use of abbreviations
                    such as 'u' serve only to annoy and make things hard to read. A
                    sentence terminating '.' should be followed by at least one blank.
                    See my sig below if you insist on using the broken google groups
                    posting mechanism.

                    --
                    "If you want to post a followup via groups.google.c om, don't use
                    the broken "Reply" link at the bottom of the article. Click on
                    "show options" at the top of the article, then click on the
                    "Reply" at the bottom of the article headers." - Keith Thompson


                    Comment

                    • Richard Harter

                      #11
                      Re: pointer initialization

                      On Thu, 07 Apr 2005 10:24:11 GMT, CBFalconer <cbfalconer@yah oo.com>
                      wrote:
                      [color=blue]
                      >risha wrote:[color=green]
                      >>
                      >> addr2 = (unsigned short volatile *)0x7F12;
                      >> Thank u very much for the help extended to one and all, specially
                      >> richard sir i am grateful the statement above worked the way i
                      >> wanted.I am a student and help like this makes us get back to our
                      >> toes.[/color]
                      >
                      >What statement above? You failed to quote anything, and I suspect
                      >you changed the subject line again. Also the use of abbreviations
                      >such as 'u' serve only to annoy and make things hard to read. A
                      >sentence terminating '.' should be followed by at least one blank.
                      >See my sig below if you insist on using the broken google groups
                      >posting mechanism.[/color]

                      This is the statement he was referring to:
                      addr2 = (unsigned short volatile *)0x7F12;



                      Richard Harter, cri@tiac.net
                      http://home.tiac.net/~cri, http://www.varinoma.com
                      Save the Earth now!!
                      It's the only planet with chocolate.

                      Comment

                      • CBFalconer

                        #12
                        Re: pointer initialization

                        Richard Harter wrote:[color=blue]
                        > CBFalconer <cbfalconer@yah oo.com> wrote:[color=green]
                        >> risha wrote:[color=darkred]
                        >>>
                        >>> addr2 = (unsigned short volatile *)0x7F12;
                        >>> Thank u very much for the help extended to one and all, specially
                        >>> richard sir i am grateful the statement above worked the way i
                        >>> wanted.I am a student and help like this makes us get back to our
                        >>> toes.[/color]
                        >>
                        >> What statement above? You failed to quote anything, and I suspect
                        >> you changed the subject line again. Also the use of abbreviations
                        >> such as 'u' serve only to annoy and make things hard to read. A
                        >> sentence terminating '.' should be followed by at least one blank.
                        >> See my sig below if you insist on using the broken google groups
                        >> posting mechanism.[/color]
                        >
                        > This is the statement he was referring to:
                        > addr2 = (unsigned short volatile *)0x7F12;[/color]

                        The point is not so much to get the reference, but to make the OP
                        realize that his posts are useless without proper quoting, and that
                        a means to that end is available.

                        --
                        "If you want to post a followup via groups.google.c om, don't use
                        the broken "Reply" link at the bottom of the article. Click on
                        "show options" at the top of the article, then click on the
                        "Reply" at the bottom of the article headers." - Keith Thompson

                        Comment

                        Working...