Problem with pointers, require some help

Discussion in 'C Programming' started by david, Feb 5, 2008.

  1. david

    david Guest

    Hello, I am getting some problems with C and how it handles the
    pointers. I will tell more about the situation: I have created my own
    structure for making one way linked list. I have procedure int
    createList(child *root) [child is that structure] and before that I
    create pointer child *rootA, and then I invoke createList(rootA)
    [*rootA holds the value, and rootA is the pointer, holds the address
    as I remember]. And this function creates dynamic list, but the
    problem is that rootA does not point to it. After creating list
    function returns the length of it.

    The question is, how should I send to function a pointer, create a
    list in heap and then make that pointer to point to it?

    It would be something like this:
    int main {
    int *item;
    func(*item);
    printf("%d", *item);
    return 0;
    }

    func(int *num) {
    num = malloc(sizeof(int));
    *num = 8;
    printf("%d", *num);


    printf in func gives "8", but item still does not point to the same
    memory where that number is located. But I want it to point. How?
    }
    david, Feb 5, 2008
    #1
    1. Advertising

  2. david

    david Guest

    Small mistaking rewriting, should be "func(item);" in program. Any
    ideas how to make it point to what I want?
    david, Feb 5, 2008
    #2
    1. Advertising

  3. david

    fnegroni Guest

    Use a pointer to pointer.

    You can read about it in any C programming manual.
    fnegroni, Feb 5, 2008
    #3
  4. "david" <> wrote in message
    > int main {
    > int *item;
    > func(*item);
    > printf("%d", *item);
    > return 0;
    > }
    >
    > func(int *num) {
    > num = malloc(sizeof(int));
    > *num = 8;
    > printf("%d", *num);
    >
    >
    > printf in func gives "8", but item still does not point to the same
    > memory where that number is located. But I want it to point. How?
    > }
    >

    C always passes parameters by value. So when you set num to the return value
    of malloc(), you are setting a temporary copy.

    The way round this is to pass the address of a variable. This is one use of
    pointers.
    Let's give a slightly more realistic example. You want to clamp a pair of x,
    y coordinates to the width and height of an image.

    /*
    clamp x, y coordiantes to edges of image
    Params: width - image width
    height - image height
    x (in / out) x coordinate
    y (in / out) y coordinate
    Returns: 0 if point withing image, 1 if clamped
    */
    int clamp(int width, int height, int *x, int *y)
    {
    /* check if we are within the image */
    if(*x >= 0 && *x < width && *y >= 0 && *y < height)
    return 0;
    /* we're outside it, so adjust coordinates to nearest edge */
    if(*x < 0)
    *x = 0;
    if(*x >= width)
    *x = width-1;
    if(*y < 0)
    *y = 0;
    if(*y >= height)
    *y = height -1;
    return 1;
    }


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 5, 2008
    #4
  5. david

    david Guest

    Thanks for the help, I used pointer to pointer as a few people
    suggested.

    It is my first day with C programming language and I still collecting
    books, websites and etc. with the most detailed explanation how
    everything here works. Maybe you could recommend some good material?

    And for ASM looks a lot easier comparing to C, but it looks that in a
    week I will manage to write some good or at least better code than I
    do now.

    Thanks again.
    david, Feb 5, 2008
    #5
  6. david

    santosh Guest

    david wrote:

    > Thanks for the help, I used pointer to pointer as a few people
    > suggested.
    >
    > It is my first day with C programming language and I still collecting
    > books, websites and etc. with the most detailed explanation how
    > everything here works. Maybe you could recommend some good material?


    The C Programming Language (Second Edition) by Kernighan & Ritchie
    C: A Reference Manual (Fifth Edition) by Harbison & Steele
    C Programming: A Modern Approach by K.N. King

    Steve Summit's "notes" on C
    <http://www.eskimo.com/~scs/cclass/cclass.html>

    C tutorial by Tom Torf
    <http://cprog.tomsweb.net/>

    Latest draft of the evolving C Standard:
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>

    > And for ASM looks a lot easier comparing to C, [ ... ]


    The clinching advantage of C is that you needn't rewrite everything for
    every platform and chip that you target.
    santosh, Feb 5, 2008
    #6
  7. david

    Default User Guest

    david wrote:

    > Thanks for the help, I used pointer to pointer as a few people
    > suggested.
    >
    > It is my first day with C programming language and I still collecting
    > books, websites and etc. with the most detailed explanation how
    > everything here works. Maybe you could recommend some good material?


    Then this is not how you should approach learning. Get a basic tutorial
    book. Read through it, working the exercises in each chapter. Then
    start creating simple programs.





    Brian
    Default User, Feb 5, 2008
    #7
  8. david

    CBFalconer Guest

    david wrote:
    >
    > Small mistaking rewriting, should be "func(item);" in program. Any
    > ideas how to make it point to what I want?


    See sig below.

    --
    If you want to post a followup via groups.google.com, ensure
    you quote enough for the article to make sense. Google is only
    an interface to Usenet; it's not Usenet itself. Don't assume
    your readers can, or ever will, see any previous articles.
    More details at: <http://cfaj.freeshell.org/google/>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 5, 2008
    #8
  9. david

    Richard Bos Guest

    CBFalconer <> wrote:

    > david wrote:
    > >
    > > Small mistaking rewriting, should be "func(item);" in program. Any
    > > ideas how to make it point to what I want?

    >
    > See sig below.


    > --
    > Posted via a free Usenet account from http://www.teranews.com


    What's so interesting about that?

    Richard
    Richard Bos, Feb 6, 2008
    #9
  10. david

    Flash Gordon Guest

    santosh wrote, On 05/02/08 19:54:
    > david wrote:
    >
    >> Thanks for the help, I used pointer to pointer as a few people
    >> suggested.
    >>
    >> It is my first day with C programming language and I still collecting
    >> books, websites and etc. with the most detailed explanation how
    >> everything here works. Maybe you could recommend some good material?

    >
    > The C Programming Language (Second Edition) by Kernighan & Ritchie


    <snip good suggestions>

    Also the comp.lang.c FAQ which would have answered the OPs question. If
    can be found at http://c-faq.com/
    --
    Flash Gordon
    Flash Gordon, Feb 6, 2008
    #10
    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. clintonG
    Replies:
    4
    Views:
    287
    clintonG
    Jul 15, 2004
  2. Bennett Haselton
    Replies:
    4
    Views:
    7,268
    Frank Mamone
    Sep 10, 2004
  3. Justin Piper
    Replies:
    4
    Views:
    457
    Justin Piper
    Sep 22, 2006
  4. Christopher
    Replies:
    0
    Views:
    443
    Christopher
    Mar 22, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    674
Loading...

Share This Page