pointer question

Discussion in 'C Programming' started by Bill Cunningham, Mar 16, 2013.

  1. I want to take a value stored in an int use a pointer to move it to
    another int variable using a int **. Would this be the right or a usable
    technique? I know how of course to use a pointer to an int to move a value
    to an empty int and have sharing memory with pointers done but I want to use
    a int **. And move the value /from/ one int to another.

    int main(){
    int a,b;
    a=32;
    int *pi;
    pi=&a;
    b=*pi;
    }

    The above is simple but I've never used pointers to pointers.

    Bill
    Bill Cunningham, Mar 16, 2013
    #1
    1. Advertising

  2. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > I want to take a value stored in an int use a pointer to move it to
    > another int variable using a int **. Would this be the right or a usable
    > technique? I know how of course to use a pointer to an int to move a value
    > to an empty int and have sharing memory with pointers done but I want to use
    > a int **. And move the value /from/ one int to another.
    >
    > int main(){
    > int a,b;
    > a=32;
    > int *pi;
    > pi=&a;
    > b=*pi;
    > }
    >
    > The above is simple but I've never used pointers to pointers.


    Well you still haven't....

    --
    Ian Collins
    Ian Collins, Mar 16, 2013
    #2
    1. Advertising

  3. Ian Collins wrote:
    > Well you still haven't....


    Are you implying that there is error in my code above or that I'm not
    using pointers to pointers. I know that that's what I'm inquiring about.

    Bill
    Bill Cunningham, Mar 17, 2013
    #3
  4. "Bill Cunningham" <> writes:
    > Ian Collins wrote:
    >> Well you still haven't....

    >
    > Are you implying that there is error in my code above or that I'm not
    > using pointers to pointers. I know that that's what I'm inquiring about.


    You're not using pointers to pointers. You mentioned int** in your
    question, but you don't use it in your code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 17, 2013
    #4
  5. On Sat, 16 Mar 2013 19:41:20 -0400, "Bill Cunningham"
    <> wrote:

    > I want to take a value stored in an int use a pointer to move it to
    >another int variable using a int **. Would this be the right or a usable


    Given your abysmal record of confusion about the C language, why would
    you want to perform such a simple operation is such an obfuscated
    manner?

    >technique? I know how of course to use a pointer to an int to move a value
    >to an empty int and have sharing memory with pointers done but I want to use


    What does the phrase "have sharing memory with pointers done" above
    mean?

    >a int **. And move the value /from/ one int to another.


    Do you want to move the value or copy it? If you really meant move,
    what fills the vacuum in the source int after the value is moved to
    the target int?

    >
    >int main(){
    > int a,b;
    > a=32;
    > int *pi;
    > pi=&a;
    > b=*pi;
    >}
    >
    >The above is simple but I've never used pointers to pointers.


    Since you stated in the previous paragraph you want to use int**,
    where in this code did you define an object or value of that type?

    --
    Remove del for email
    Barry Schwarz, Mar 17, 2013
    #5
  6. pete wrote:
    > Bill Cunningham wrote:
    >>
    >> Ian Collins wrote:
    >>> Well you still haven't....

    >
    > [snip]
    >
    >> I'm not using pointers to pointers.

    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    >
    > int
    > main(void)
    > {
    > int *p[2];
    > int **ppi = p;
    > int a = 32;
    > int b = 0;
    >
    > printf("a is %d\n", a);
    > printf("b is %d\n", b);
    > putchar('\n');
    > p[0] = &a;
    > p[1] = &b;
    > **(ppi + 1) = **ppi;
    > printf("a is %d\n", a);
    > printf("b is %d\n", b);
    > return 0;
    > }
    >
    > /* END new.c */


    Thanks Pete for your example. I will study it and use it. I have been
    reading about linked lists lately and I want to get down pointers to
    pointers before trying something so complicated. For me anyway.

    Bill
    Bill Cunningham, Mar 17, 2013
    #6
  7. Bill Cunningham

    Shao Miller Guest

    On 3/16/2013 19:41, Bill Cunningham wrote:
    > I want to take a value stored in an int use a pointer to move it to
    > another int variable using a int **. Would this be the right or a usable
    > technique? I know how of course to use a pointer to an int to move a value
    > to an empty int and have sharing memory with pointers done but I want to use
    > a int **. And move the value /from/ one int to another.
    >
    > int main(){
    > int a,b;
    > a=32;
    > int *pi;
    > pi=&a;

    int **ppi;
    ppi=&pi;
    b=**ppi;
    return 0;
    > }
    >
    > The above is simple but I've never used pointers to pointers.
    >


    Enjoy.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Mar 18, 2013
    #7
  8. pete wrote:
    > Bill Cunningham wrote:
    >>

    >
    >> Thanks Pete for your example.

    >
    > You're welcome.
    >
    >> I will study it and use it. I have been
    >> reading about linked lists lately and I want to get down pointers to
    >> pointers before trying something so complicated. For me anyway.

    >
    > If from your posts
    > I've gotten the correct impression of what klonopin is,
    > then I think that (pointers to pointers)
    > is going to be very difficult for you.


    Yes it is. I've been working on it for awhile now.

    Bill
    Bill Cunningham, Mar 18, 2013
    #8
  9. On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"
    <> wrote:

    snip

    > Thanks Pete for your example. I will study it and use it. I have been
    >reading about linked lists lately and I want to get down pointers to
    >pointers before trying something so complicated. For me anyway.


    Why do you think you need pointers to pointers to use linked lists?

    A linked list consists of a string of nodes which are usually
    implemented as structures. Each structure will contain at least one
    pointer which points to the next structure in the list.

    Where would an int** fit in this design?

    --
    Remove del for email
    Barry Schwarz, Mar 19, 2013
    #9
  10. Bill Cunningham

    Shao Miller Guest

    On 3/19/2013 01:38, Barry Schwarz wrote:
    > On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"
    > <> wrote:
    >
    > snip
    >
    >> Thanks Pete for your example. I will study it and use it. I have been
    >> reading about linked lists lately and I want to get down pointers to
    >> pointers before trying something so complicated. For me anyway.

    >
    > Why do you think you need pointers to pointers to use linked lists?
    >
    > A linked list consists of a string of nodes which are usually
    > implemented as structures. Each structure will contain at least one
    > pointer which points to the next structure in the list.
    >
    > Where would an int** fit in this design?
    >


    Another chap and I were discussing tracking list heads and a null
    sentinel value versus list tails and having a circular list.

    My code for circular: http://ideone.com/aNGBA
    His code for null sentinel: http://ideone.com/nVSdt

    (His code has a typo, somewhere, but I can't remember quite where.)

    Anyway, he made extensive use of multiple levels of indirection, as is
    evident.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Mar 19, 2013
    #10
  11. Barry Schwarz wrote:
    > On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"
    > <> wrote:
    >
    > snip
    >
    >> Thanks Pete for your example. I will study it and use it. I have
    >> been reading about linked lists lately and I want to get down
    >> pointers to pointers before trying something so complicated. For me
    >> anyway.

    >
    > Why do you think you need pointers to pointers to use linked lists?
    >
    > A linked list consists of a string of nodes which are usually
    > implemented as structures. Each structure will contain at least one
    > pointer which points to the next structure in the list.
    >
    > Where would an int** fit in this design?


    the int ** was just being used in an example that was not a linked list
    example. After a struct describing a node has been written some kind of
    "push" function has to push nodes into the chain of nodes. That's where my
    tutorial mentions pointer to pointers.

    Bill
    Bill Cunningham, Mar 19, 2013
    #11
  12. Barry Schwarz wrote:
    > On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"
    > <> wrote:
    >
    > snip
    >
    >> Thanks Pete for your example. I will study it and use it. I have
    >> been reading about linked lists lately and I want to get down
    >> pointers to pointers before trying something so complicated. For me
    >> anyway.

    >
    > Why do you think you need pointers to pointers to use linked lists?
    >
    > A linked list consists of a string of nodes which are usually
    > implemented as structures. Each structure will contain at least one
    > pointer which points to the next structure in the list.
    >
    > Where would an int** fit in this design?


    The .pdf on this page is what I'm going by. It seems to be the simplest.
    http://cslibrary.stanford.edu/103/
    Bill Cunningham, Mar 19, 2013
    #12
  13. On Tue, 19 Mar 2013 13:32:41 -0400, "Bill Cunningham"
    <> wrote:

    >Barry Schwarz wrote:
    >> On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"
    >> <> wrote:
    >>
    >> snip
    >>
    >>> Thanks Pete for your example. I will study it and use it. I have
    >>> been reading about linked lists lately and I want to get down
    >>> pointers to pointers before trying something so complicated. For me
    >>> anyway.

    >>
    >> Why do you think you need pointers to pointers to use linked lists?
    >>
    >> A linked list consists of a string of nodes which are usually
    >> implemented as structures. Each structure will contain at least one
    >> pointer which points to the next structure in the list.
    >>
    >> Where would an int** fit in this design?

    >
    > The .pdf on this page is what I'm going by. It seems to be the simplest.
    >http://cslibrary.stanford.edu/103/


    The tutorial loses credibility as soon as it states that a pointer
    stores a reference, especially since the term has a specific meaning
    in C++ which the tutorial claims to be relevant for.

    While that tutorial does describe how a function can alter a variable
    in the calling function by receiving a pointer to that variable, the
    tutorial's claim that this is the traditional way to write a push
    function seems suspect. Instead of having the push function return
    void, the tradition I'm familiar with would have it return a pointer
    to the head of the list and eliminate the need for a pointer to
    pointer. This eliminates both the need for a pointer to pointer and
    the unnecessary indirection within the push function at the cost of
    using the push function in an assignment statement. In other words,
    instead of calling push with
    push(&head, data);
    I would call it with
    head = push(head,data);
    This provides the additional flexibility of storing the return value
    in a pointer different than the one passed as an argument.

    --
    Remove del for email
    Barry Schwarz, Mar 20, 2013
    #13
  14. Barry Schwarz <> writes:
    > On Tue, 19 Mar 2013 13:32:41 -0400, "Bill Cunningham"
    > <> wrote:

    [...]
    >> The .pdf on this page is what I'm going by. It seems to be the simplest.
    >>http://cslibrary.stanford.edu/103/

    >
    > The tutorial loses credibility as soon as it states that a pointer
    > stores a reference, especially since the term has a specific meaning
    > in C++ which the tutorial claims to be relevant for.

    [...]

    I haven't looked at the tutorial, but N1570 6.2.5p20 says:

    A pointer type describes an object whose value provides a
    reference to an entity of the referenced type.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 20, 2013
    #14
  15. pete wrote:
    > Barry Schwarz wrote:
    >>
    >> On Tue, 19 Mar 2013 13:32:41 -0400, "Bill Cunningham"
    >> <> wrote:
    >>
    >>> Barry Schwarz wrote:
    >>>> On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"
    >>>> <> wrote:
    >>>>
    >>>> snip
    >>>>
    >>>>> Thanks Pete for your example. I will study it and use it. I
    >>>>> have been reading about linked lists lately and I want to get down
    >>>>> pointers to pointers before trying something so complicated. For
    >>>>> me anyway.
    >>>>
    >>>> Why do you think you need pointers to pointers to use linked lists?
    >>>>
    >>>> A linked list consists of a string of nodes which are usually
    >>>> implemented as structures. Each structure will contain at least
    >>>> one pointer which points to the next structure in the list.
    >>>>
    >>>> Where would an int** fit in this design?
    >>>
    >>> The .pdf on this page is what I'm going by. It seems to be the
    >>> simplest. http://cslibrary.stanford.edu/103/

    >>
    >> The tutorial loses credibility as soon as it states that a pointer
    >> stores a reference, especially since the term has a specific meaning
    >> in C++ which the tutorial claims to be relevant for.
    >>
    >> While that tutorial does describe how a function can alter a variable
    >> in the calling function by receiving a pointer to that variable, the
    >> tutorial's claim that this is the traditional way to write a push
    >> function seems suspect. Instead of having the push function return
    >> void, the tradition I'm familiar with would have it return a pointer
    >> to the head of the list and eliminate the need for a pointer to
    >> pointer. This eliminates both the need for a pointer to pointer and
    >> the unnecessary indirection within the push function at the cost of
    >> using the push function in an assignment statement. In other words,
    >> instead of calling push with
    >> push(&head, data);
    >> I would call it with
    >> head = push(head,data);
    >> This provides the additional flexibility of storing the return value
    >> in a pointer different than the one passed as an argument.

    >
    > Using a return value makes the most sense to me too.
    >
    > The only time that I use a pointer parameter
    > to achieve a side effect in the calling function,
    > is when the return value is already being used for something else.
    >
    > The code in the tutorial
    > never checks the return value of an allocation,
    > for a null pointer.


    Does anyone have any other suggestions for a good C tutorial on this. I
    have invested so much time in C if I can't eventually grasp it the thing to
    look at would be C++. But this isn't a C issue I don't believe so much as a
    algorithm issue in C.

    Bill
    Bill Cunningham, Mar 20, 2013
    #15
  16. Shao Miller wrote:

    > A tutorial on linked lists or on multiple levels of indirection with
    > pointers? I agree with you if you are saying that linked lists can be
    > implemented with C, but are not intrinsic to C.


    That's pretty much what I'm saying.

    Bill
    Bill Cunningham, Mar 21, 2013
    #16
  17. On Wed, 20 Mar 2013 16:01:11 -0400, "Bill Cunningham"
    <> wrote:

    snip

    > Does anyone have any other suggestions for a good C tutorial on this. I
    >have invested so much time in C if I can't eventually grasp it the thing to
    >look at would be C++. But this isn't a C issue I don't believe so much as a
    >algorithm issue in C.


    If you are having trouble with the C concepts, C++ will not make
    things any easier.

    --
    Remove del for email
    Barry Schwarz, Mar 21, 2013
    #17
  18. Bill Cunningham

    Öö Tiib Guest

    On Wednesday, 20 March 2013 22:01:11 UTC+2, Bill Cunningham wrote:
    > Does anyone have any other suggestions for a good C tutorial on this. I
    > have invested so much time in C if I can't eventually grasp it the thing to
    > look at would be C++. But this isn't a C issue I don't believe so much as a
    > algorithm issue in C.


    C++ is often considered "monstrously complex" by fans of other languages
    because it adds a lot of features to C. With "a lot" I mean *A LOT*.
    Classes, virtual functions, operator overloading, multiple
    inheritance, exceptions, templates, lambda functions etc. C++ standard
    library is also quite large so it takes some time to learn to use it.

    Most C programs actually compile on C++ compilers with very little changes
    (if any) so most of the complexity of C is legal in C++.

    You may want to consider some scripting language like Python, that lot
    of people say is easy to use.
    Öö Tiib, Mar 21, 2013
    #18
  19. Bill Cunningham

    Guest

    In article <514a1582$0$10404$>,
    Bill Cunningham <> wrote:

    [ snip ]

    > Does anyone have any other suggestions for a good C tutorial on this. I
    > have invested so much time in C if I can't eventually grasp it the thing to
    > look at would be C++. But this isn't a C issue I don't believe so much as a
    > algorithm issue in C.
    >


    I agree with the others who have said that if you're having trouble
    with C it's unlikely that moving to C++ will help -- it's a much
    bigger and more complicated language, with pretty much all the warts
    of C, so -- how do you think it would be better?

    I'm inclined to agree, though, that the problems you're having are
    not necessarily language-specific -- C is probably one of the more
    difficult languages to start with, but based on your history of
    posts here I'm inclined to think that the problems are not so much
    with the specifics of C as with, oh, "algorithmic thinking" maybe.
    Possibly it's a result of the medication you're taking. Whether
    switching to a different language would help -- I guess I'm not
    optimistic, but I'd be glad to be proved wrong.

    One thing I'm curious about, though:

    A while back you posted to comp.lang.fortran asking about
    introductory and/or tutorial material. I'd be interested in hearing
    about how/whether you followed up on that. ?

    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
    , Mar 22, 2013
    #19
  20. wrote:

    [snip]

    > A while back you posted to comp.lang.fortran asking about
    > introductory and/or tutorial material. I'd be interested in hearing
    > about how/whether you followed up on that. ?


    I figured I've come this far so move on. And I think you are right. If I
    knew fortran I still wouldn't know much to *do* with it or how to do
    anything with it. I know what I'd like to do but lack the knowledge. I
    mentioned C++ because it is much higher level mainly.

    Bill
    Bill Cunningham, Mar 22, 2013
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    673
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    824
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,233
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    626
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    445
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page