Trying to understand pointers for function paramaters

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

    #16
    Re: Trying to understand pointers for function paramaters

    On Wed, 29 Sep 2004 23:09:10 +0200, "Richard Hengeveld"
    <richardhengeve ld@hotmail.com> wrote:
    [color=blue][color=green]
    >> Probably what is confusing you is the *formal* argument
    >>
    >> int* p[/color]
    >
    >Thanks for replying.
    >Yes, that is exactly what is confusing me.
    > I understand you set a pointer (if you're not passing to functions) by:
    >
    >int a, *p;
    >p = &a;
    >
    >and not:
    >
    >p* = &a
    >
    >Wouldn't it be more logical if it was something like:
    >
    > void f(int i) {
    > int *p
    > p = i;[/color]

    I assume you meant p=&i here.
    [color=blue]
    > p* = 0;[/color]

    I assume you meant *p=0 here. This will set i to 0 but I is local to
    f. When you return, i ceases to exist. Consequently, the argument in
    the calling function is never updated and defeats the purpose.
    [color=blue]
    > }
    >
    > int main(int argc, char* argv[]) {
    > int a;
    > f(&a);[/color]

    With f as defined above, this obviously causes a diagnostic since f is
    expecting an int but you are passing an int*.

    Going back to your original post which defined f as expecting an int*,
    when you call f with this argument, three significant things happen:

    i is created as an automatic variable local to f.
    i is initialized with the address of a (which is a local variable
    local to main.
    Control is transferred to f.

    Look back at your code at the top of this post and notice how similar
    the first two are to what you wrote when not calling a function.
    [color=blue]
    > return 0;
    > }[/color]

    The concepts to understand are

    In a definition, *i defines i as a pointer.

    After the definition, i refers to the pointer itself and not the
    object being pointed to, if any.

    Once the pointer has been initialized, coding *i dereferences the
    pointer and evaluates to the object being pointed to.

    A function call causes all the parameters of the function to be
    initialized with the corresponding arguments used in the calling
    statement. (So f(&a) has a very similar effect to p=&a.)


    <<Remove the del for email>>

    Comment

    • Flash Gordon

      #17
      Re: Trying to understand pointers for function paramaters

      On Thu, 30 Sep 2004 04:13:38 GMT
      CBFalconer <cbfalconer@yah oo.com> wrote:
      [color=blue]
      > Keith Thompson wrote:[color=green]
      > > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:[/color][/color]

      <snip>
      [color=blue][color=green]
      > > If E. Robert Tisdale claims that someone else has written something
      > > in a previous article, don't believe it unless you've verified it
      > > by reading the actual article that he claims to be quoting. Trust
      > > him at your own risk.[/color]
      >
      > And just when I thought Trollsdale was showing signs of
      > reformation. He seems to be a dedicated recidivist. Can we apply
      > the 'three strikes' law somehow?[/color]

      You mean if any of us meet him we get to strike him three times? :-)
      --
      Flash Gordon
      Sometimes I think shooting would be far too good for some people.
      Although my email address says spam, it is real and I read it.

      Comment

      • Mark A. Odell

        #18
        Re: Trying to understand pointers for function paramaters

        "Richard Hengeveld" <richardhengeve ld@hotmail.com> wrote in
        news:415b34db$0 $76531$b83b6cc0 @news.wanadoo.n l:
        [color=blue][color=green]
        >> How would it be more logical to assign an integer to a
        >> pointer-to-integer? What problem are you trying to solve?[/color]
        >
        > I'm not trying to solve a problem. I'm just trying to understand C.
        > In a function without pointer arguments, the argument(s) of that
        > function are set by value passing:[/color]

        Correct. In this case the called function gets a "copy" of the value to
        play with and the caller can be sure that the callee won't modify the
        original value in 'i'. This is nice when you want to preserve your value
        but have some function create a new value based on your value, e.g.

        #include <stdio.h>

        static int plusTwo(int copyOfVal)
        {
        /* Modify copy by adding two to demonstrate
        ** that the caller's 'val' is not modified.
        */
        copyOfVal += 2;

        return copyOfVal;
        }

        static int timesTenModify( int *pVal)
        {
        int failure = 0;

        if (pVal) *pVal *= 10;
        else failure = !0;

        return failure;
        }

        int main(void)
        {
        int failure;
        int val = 10;
        int valPlusTwo = plusTwo(val);

        /* As the caller, I retain my original value of 'val'
        ** whilst having a worker function calculate 'val' + 2
        ** for me which I store in 'valPlusTwo'. In this situation
        ** I do not wish my 'val' to be modified.
        */
        printf("My val is %d, and +2 is %d\n", val, valPlusTwo);

        /* In this case, I wish to modify 'val' and do not need
        ** to retain its original value of 10 so I call
        ** timesTenModify( ) with a pointer to my 'val' object
        ** so it can modify 'val' directly.
        */
        failure = timesTenModify( &val);
        if (failure) printf("Trouble with timesTenModify( ) call\n");
        else printf("My val is now %d\n", val);

        return 0;
        }
        [color=blue]
        > I just don't understand why this is (a little bit) different with
        > pointer arguments.[/color]

        Hopefully this simple example will help.

        --
        - Mark ->
        --

        Comment

        • Christopher Benson-Manica

          #19
          Re: Trying to understand pointers for function paramaters

          CBFalconer <cbfalconer@yah oo.com> spoke thus:
          [color=blue]
          > And just when I thought Trollsdale was showing signs of
          > reformation. He seems to be a dedicated recidivist. Can we apply
          > the 'three strikes' law somehow?[/color]

          Perhaps there could be a section of the FAQ dedicated to identifying
          posters to plonk?

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Christopher Benson-Manica

            #20
            Re: Trying to understand pointers for function paramaters

            Pedro Graca <hexkid@hotpop. com> spoke thus:
            [color=blue]
            > void g(int* p); // make 'int*' stand out[/color]

            That's actually misleading - it makes "int*" appear to be a type,
            which it is not.
            [color=blue]
            > typedef int *pointer_to_int ;
            > typedef int* pointer_to_int;[/color]
            [color=blue]
            > These two typedefs are absolutely equal. Which one do you prefer?[/color]

            Neither, for real code.

            --
            Christopher Benson-Manica | I *should* know what I'm talking about - if I
            ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

            Comment

            • John Bode

              #21
              Re: Trying to understand pointers for function paramaters

              "Richard Hengeveld" <richardhengeve ld@hotmail.com> wrote in message news:<415ae427$ 0$76520$b83b6cc 0@news.wanadoo. nl>...[color=blue]
              > Hi all,
              >
              > I'm trying to understand how pointers for function parameters work. As I
              > understand it, if you got a function like:
              >
              > void f(int *i)
              > {
              > *i = 0;
              > }
              >
              > int main()
              > {
              > int a;
              > f(&a);
              > return 0;
              > }
              >
              > It does what you want (namely altering the value of a).
              > I find this illogical. As far as I can understand, the address of "a" is
              > passed, and "*i" is set with this address not "i", as it should be in my
              > understanding.
              > What am I missing?[/color]

              I think you're getting hung up with declarator syntax as much as
              anything else, and that you're looking at the formal parameter
              declaration as an actual dereference operation, which is why you're
              confused.

              C declaration statements follow a "declaratio n mimics use" paradigm.
              When you dereference the pointer to get the value it's pointing to,
              you use the expression "*i". The idea is that the declaration of "i"
              (a pointer) should closely mirror how it's actually used in the code;
              therefore, the declaration looks like this:

              int *i

              The int-ness of i is specified in the type specifier "int"; the
              pointer-ness of i is specified in the declarator "*i". This statement
              introduces the symbol "i" as having type pointer-to-int (although the
              declaration reads as pointer-to-type-int).

              When you pass your value (&a, type pointer-to-int) to this function,
              it is written to the formal parameter "i" (type pointer-to-int), *not*
              to the expression "*i" (type int).

              Hope that helps.
              [color=blue]
              > TIA
              >
              > P.S.
              > I've really searched for this in the groups faq and elsewhere before I
              > posted.[/color]

              Comment

              • CBFalconer

                #22
                Re: Trying to understand pointers for function paramaters

                Christopher Benson-Manica wrote:[color=blue]
                > CBFalconer <cbfalconer@yah oo.com> spoke thus:
                >[color=green]
                >> And just when I thought Trollsdale was showing signs of
                >> reformation. He seems to be a dedicated recidivist. Can we
                >> apply the 'three strikes' law somehow?[/color]
                >
                > Perhaps there could be a section of the FAQ dedicated to
                > identifying posters to plonk?[/color]

                Too dynamic. The best I can do is publish my own c.l.c plonk
                list. Trollsdale isn't on it because he requires watching else
                his alteration tricks will go unnoticed. The FAQ plonks (Answers,
                FAQ list) are because I have a copy and don't need any more, and
                they are big.

                c:\netscape\use rs\cbf\news\hos t-netnews.att.net >grep condition=
                comp.lang.c.dat
                condition=" OR (subject,contai ns,comp.lang.c Answers)"
                condition=" OR (subject,contai ns,My china ex. girlfriend)"
                condition=" OR (from,contains, Kenny McCormack)"
                condition=" OR (subject,contai ns,comp.lang.c FAQ list)"
                condition=" OR (from,contains, RoSsIaCrIiLoIA) "
                condition=" OR (from,contains, lxrocks)"
                condition=" OR (from,contains, Generic Usenet Account)"
                condition=" OR (from,contains, SM Ryan)"
                condition=" OR (from,contains, Skybuck Flying)"
                condition=" OR (subject,contai ns,C++ wins over C)"
                condition=" OR (subject,contai ns,What is an object)"

                --
                A: Because it fouls the order in which people normally read text.
                Q: Why is top-posting such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?


                Comment

                • Stephen Sprunk

                  #23
                  Re: Trying to understand pointers for function paramaters

                  "Richard Hengeveld" <richardhengeve ld@hotmail.com> wrote in message
                  news:415b249d$0 $76511$b83b6cc0 @news.wanadoo.n l...[color=blue]
                  > Thanks for replying.
                  > Yes, that is exactly what is confusing me.
                  > I understand you set a pointer (if you're not passing to functions) by:
                  >
                  > int a, *p;
                  > p = &a;
                  >
                  > and not:
                  >
                  > p* = &a[/color]

                  This is a syntax error; I assume you meant:

                  *p = &a;

                  This is a type mismatch; you're trying to assign an address (a) to an
                  integer (*p) without a cast. Now, if you did:

                  p = &a;

                  This is correct. Then you can do:

                  *p = 1;

                  And then you will find that a==1. This is no different than doing:

                  *(&a) = 1;

                  S

                  --
                  Stephen Sprunk "God does not play dice." --Albert Einstein
                  CCIE #3723 "God is an inveterate gambler, and He throws the
                  K5SSS dice at every possible opportunity." --Stephen Hawking

                  Comment

                  • Richard Hengeveld

                    #24
                    Re: Trying to understand pointers for function paramaters

                    Thanks all for helping me out and being patiant with me.
                    To be clear, I was not trying to attack c logics or concepts. I was just
                    trying to learn c. Thanks again.
                    Richard


                    Comment

                    • Richard Hengeveld

                      #25
                      Re: Trying to understand pointers for function paramaters

                      Thanks all for helping me out and being patiant with me.
                      To be clear, I was not trying to attack C logics or concepts. Just trying to
                      learn C. Thanks again


                      Comment

                      • Mabden

                        #26
                        Re: Trying to understand pointers for function paramaters

                        "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                        news:415B8170.4 891BC87@yahoo.c om...[color=blue]
                        > Keith Thompson wrote:[color=green]
                        > > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:
                        > >[/color]
                        > ... snip mangled quote ...[color=green]
                        > >
                        > > Damn it, Tisdale, that's not what he wrote. Here's what Richard
                        > > Hengeveld *actually* wrote in the article to which you replied:
                        > >[/color]
                        > ... snip details ...[color=green]
                        > >
                        > > You've done this before, and you've been called on it. I have no
                        > > realistic expectation that you're going to change your ways this
                        > > time either. I'm posting this mostly as a warning to other readers.
                        > >
                        > > If E. Robert Tisdale claims that someone else has written something
                        > > in a previous article, don't believe it unless you've verified it
                        > > by reading the actual article that he claims to be quoting. Trust
                        > > him at your own risk.[/color]
                        >
                        > And just when I thought Trollsdale was showing signs of
                        > reformation. He seems to be a dedicated recidivist. Can we apply
                        > the 'three strikes' law somehow?
                        >[/color]
                        Shut up! It was the same function with a variable changed.

                        Where's your input? What is your content?

                        Nothing!

                        Fuck off!

                        --
                        Mabden


                        Comment

                        • Mabden

                          #27
                          Re: Trying to understand pointers for function paramaters

                          "Flash Gordon" <spam@flash-gordon.me.uk> wrote in message
                          news:7c3t22xsv9 .ln2@brenda.fla sh-gordon.me.uk...[color=blue]
                          > On Thu, 30 Sep 2004 04:13:38 GMT
                          > CBFalconer <cbfalconer@yah oo.com> wrote:
                          >[color=green]
                          > > Keith Thompson wrote:[color=darkred]
                          > > > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:[/color][/color]
                          >
                          > <snip>
                          >[color=green][color=darkred]
                          > > > If E. Robert Tisdale claims that someone else has written[/color][/color][/color]
                          something[color=blue][color=green][color=darkred]
                          > > > in a previous article, don't believe it unless you've verified it
                          > > > by reading the actual article that he claims to be quoting. Trust
                          > > > him at your own risk.[/color]
                          > >
                          > > And just when I thought Trollsdale was showing signs of
                          > > reformation. He seems to be a dedicated recidivist. Can we apply
                          > > the 'three strikes' law somehow?[/color]
                          >
                          > You mean if any of us meet him we get to strike him three times? :-)[/color]

                          Shut up!


                          Comment

                          • Keith Thompson

                            #28
                            Re: Trying to understand pointers for function paramaters

                            "Mabden" <mabden@sbc_glo bal.net> writes:[color=blue]
                            > "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                            > news:415B8170.4 891BC87@yahoo.c om...[color=green]
                            >> Keith Thompson wrote:[color=darkred]
                            >> > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:
                            >> >[/color]
                            >> ... snip mangled quote ...[color=darkred]
                            >> >
                            >> > Damn it, Tisdale, that's not what he wrote. Here's what Richard
                            >> > Hengeveld *actually* wrote in the article to which you replied:
                            >> >[/color]
                            >> ... snip details ...[color=darkred]
                            >> >
                            >> > You've done this before, and you've been called on it. I have no
                            >> > realistic expectation that you're going to change your ways this
                            >> > time either. I'm posting this mostly as a warning to other readers.
                            >> >
                            >> > If E. Robert Tisdale claims that someone else has written something
                            >> > in a previous article, don't believe it unless you've verified it
                            >> > by reading the actual article that he claims to be quoting. Trust
                            >> > him at your own risk.[/color]
                            >>
                            >> And just when I thought Trollsdale was showing signs of
                            >> reformation. He seems to be a dedicated recidivist. Can we apply
                            >> the 'three strikes' law somehow?
                            >>[/color]
                            > Shut up! It was the same function with a variable changed.
                            >
                            > Where's your input? What is your content?
                            >
                            > Nothing!
                            >
                            > Fuck off![/color]

                            Mabden, whoever you are, I am sick and tired of your insults. You've
                            sometimes shown signs of being worth talking to, but if I had a
                            killfile, you would have been in it several times over by now.
                            Tisdale deliberately forged a quotation, making it appear that the
                            previous poster had written something he didn't. He changed the
                            formatting of the OP's code (the original was better). He changed a
                            variable name; the new name was clearer, but he didn't bother to say
                            that. This is unacceptable behavior, and I don't choose to sit
                            quietly by and let other readers be lied to.

                            If you don't like our replies, feel free to ignore them. Killfile us
                            if that makes you happy.

                            As for your obscene language, it doesn't particularly bother me,
                            especially if it's used creatively (though I try to avoid it in public
                            forums); you're just not very good at it.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • CBFalconer

                              #29
                              Re: Trying to understand pointers for function paramaters

                              Mabden wrote:[color=blue]
                              >[/color]
                              .... snip ...[color=blue]
                              >
                              > Where's your input? What is your content?
                              >
                              > Nothing!
                              >
                              > Fuck off![/color]

                              PLONK

                              --
                              Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                              Available for consulting/temporary embedded and systems.
                              <http://cbfalconer.home .att.net> USE worldnet address!


                              Comment

                              • Mabden

                                #30
                                Re: Trying to understand pointers for function paramaters

                                "Keith Thompson" <kst-u@mib.org> wrote in message
                                news:ln1xggavx6 .fsf@nuthaus.mi b.org...[color=blue]
                                > "Mabden" <mabden@sbc_glo bal.net> writes:[color=green]
                                > > "CBFalconer " <cbfalconer@yah oo.com> wrote in message
                                > > news:415B8170.4 891BC87@yahoo.c om...[color=darkred]
                                > >> Keith Thompson wrote:
                                > >> > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:
                                > >> >
                                > >> ... snip mangled quote ...
                                > >> >
                                > >> > Damn it, Tisdale, that's not what he wrote. Here's what Richard
                                > >> > Hengeveld *actually* wrote in the article to which you replied:
                                > >> >
                                > >> ... snip details ...
                                > >> >
                                > >> > You've done this before, and you've been called on it. I have no
                                > >> > realistic expectation that you're going to change your ways this
                                > >> > time either. I'm posting this mostly as a warning to other[/color][/color][/color]
                                readers.[color=blue][color=green][color=darkred]
                                > >> >
                                > >> > If E. Robert Tisdale claims that someone else has written[/color][/color][/color]
                                something[color=blue][color=green][color=darkred]
                                > >> > in a previous article, don't believe it unless you've verified it
                                > >> > by reading the actual article that he claims to be quoting.[/color][/color][/color]
                                Trust[color=blue][color=green][color=darkred]
                                > >> > him at your own risk.
                                > >>
                                > >> And just when I thought Trollsdale was showing signs of
                                > >> reformation. He seems to be a dedicated recidivist. Can we apply
                                > >> the 'three strikes' law somehow?
                                > >>[/color]
                                > > Shut up! It was the same function with a variable changed.
                                > >
                                > > Where's your input? What is your content?
                                > >
                                > > Nothing!
                                > >
                                > > Fuck off![/color]
                                >
                                > Mabden, whoever you are, I am sick and tired of your insults. You've
                                > sometimes shown signs of being worth talking to, but if I had a
                                > killfile, you would have been in it several times over by now.[/color]

                                Well, stop trolling Tisdale unless you post actual content.
                                [color=blue]
                                > Tisdale ....[/color]

                                Get over it.
                                [color=blue]
                                > If you don't like our replies, feel free to ignore them. Killfile us
                                > if that makes you happy.[/color]

                                Ditto.
                                [color=blue]
                                > As for your obscene language, it doesn't particularly bother me,
                                > especially if it's used creatively (though I try to avoid it in public
                                > forums); you're just not very good at it.[/color]

                                Well, I use strong language with my friends (yes, I have some...) and
                                sometimes forget how some posters don't in their Real Life (tm).

                                I just have so many newsgroups to get through and a limited timeslice.
                                To have to read a long post about how this function was changed from "i"
                                to "p" and then compare them, and then find no difference, and then
                                realize, "Oh, it's just KT on his period again" gets really old.

                                Can't you just killfile Tisdale and be done with it, Nanny?

                                --
                                Mabden


                                Comment

                                Working...