Pointers to structure and array of structure.

Discussion in 'C Programming' started by Excluded_Middle, Oct 24, 2004.

  1. how to convert these two notation in pointer form:

    1 - list[count] = value;
    (explaination)
    - list is an array of structure of type M.
    - count is integer index
    - value is a structure of type M.

    2 - tmp = list;
    - tmp is structure of type M
    - list is similar to above mentioned list
    - i is an integer index
     
    Excluded_Middle, Oct 24, 2004
    #1
    1. Advertising

  2. Excluded_Middle

    Artie Gold Guest

    Excluded_Middle wrote:
    > how to convert these two notation in pointer form:
    >
    > 1 - list[count] = value;
    > (explaination)
    > - list is an array of structure of type M.
    > - count is integer index
    > - value is a structure of type M.
    >
    > 2 - tmp = list;
    > - tmp is structure of type M
    > - list is similar to above mentioned list
    > - i is an integer index


    <rant>
    [sniff, sniff]Yup, smells like homework.

    DYOFHW!!!!!
    </rant>

    Find the FAQ. Read the FAQ. Show us what you have so far. *Then* we'll help.

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas

    "If you don't think it matters, you're not paying attention."
     
    Artie Gold, Oct 24, 2004
    #2
    1. Advertising

  3. (Excluded_Middle) wrote in message news:<>...
    > how to convert these two notation in pointer form:
    >
    > 1 - list[count] = value;
    > (explaination)
    > - list is an array of structure of type M.
    > - count is integer index
    > - value is a structure of type M.
    >
    > 2 - tmp = list;
    > - tmp is structure of type M
    > - list is similar to above mentioned list
    > - i is an integer index


    Use pointer notation. If you tell us what problems you have with
    your solution, we should be able to help.
     
    J. J. Farrell, Oct 25, 2004
    #3
  4. void main()
    {
    int i;
    int *x, *y;
    x = (int *) calloc(10, sizeof(int));
    for(i=0;i<10;i++)
    x = i;
    }

    in the above program I want to to access x by pointer notation instead
    of array notation i.e. instead of using

    x = i;

    I use

    *(x + i) = i;
    it doesn't work. secondly I use

    *x = i;
    x++;

    work fine but then I lost the pointer to first element i.e. x[0].

    I am giving this example using simple int but I really need to access
    a structure with pointer notation. which give me segmentation faults
    or other values.

    /********** Guys like "ARTIE GOLD" should read below
    *****************/
    This is part of an assignment question and I m not asking you guys to
    solve it all I ask is little help for calloc and structure array. Guys
    can't answer my question should not give me suggestion of searching
    for FAQ because I am doing my best to find the answer.
    /***************************/

    Thank you to all of you except "ARTIE GOLD" kinda guys.
     
    Excluded_Middle, Oct 26, 2004
    #4
  5. Excluded_Middle wrote:

    > void main()
    > {
    > int i;
    > int *x, *y;
    > x = (int *) calloc(10, sizeof(int));
    > for(i=0;i<10;i++)
    > x = i;
    > }
    >


    This program is badly broken. See below.

    > in the above program I want to to access x by pointer notation instead
    > of array notation i.e. instead of using
    >
    > x = i;
    >
    > I use
    >
    > *(x + i) = i;
    > it doesn't work. secondly I use


    What in the world do you mean by doesn't work? Of course it works:

    #include <stdlib.h> /* mha: added; needed for OP's code */
    #include <stdio.h> /* mha: not needed for the OP's code,
    used to display the results of the
    two approaches */


    int /* mha: was the Schildty 'void' */ main()
    {
    int i;
    int *x, *y;
    /* mha: replaced wasteful calloc call, which also sillily cast the
    return value from calloc */
    if (!(x = malloc(10 * sizeof *x))) {
    fprintf(stderr, " x allocation failed.\nQuiting");
    exit(EXIT_FAILURE);
    }
    for (i = 0; i < 10; i++)
    x = i;
    /* mha: the following is added to show the results of the assignments
    both from the OP's code and from pointer-based assignments */
    for (i = 0; i < 10; i++)
    printf("x[%d] = %d\n", i, x);
    printf("\n");
    /* mha: parallel to the above, but in using y, and running through
    the arrays backward */
    if (!(y = malloc(10 * sizeof *y))) {
    fprintf(stderr, "allocation failed.\nQuiting");
    free(x);
    exit(EXIT_FAILURE);
    }
    for (i = 9; i >= 0; i--)
    *(y + i) = i;
    for (i = 9; i >= 0; i--)
    printf("*(y+%d) = %d\n", i, y);
    printf("\n");
    free(y);

    free(x); /* mha: added, needed in OP's code */
    return 0; /* mha: added, needed in OP's code
    unless he has a C99 compiler */
    }

    [output]
    x[0] = 0
    x[1] = 1
    x[2] = 2
    x[3] = 3
    x[4] = 4
    x[5] = 5
    x[6] = 6
    x[7] = 7
    x[8] = 8
    x[9] = 9

    *(y+9) = 9
    *(y+8) = 8
    *(y+7) = 7
    *(y+6) = 6
    *(y+5) = 5
    *(y+4) = 4
    *(y+3) = 3
    *(y+2) = 2
    *(y+1) = 1
    *(y+0) = 0
     
    Martin Ambuhl, Oct 26, 2004
    #5
    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. Roger Leigh
    Replies:
    8
    Views:
    450
    Karl Heinz Buchegger
    Nov 17, 2003
  2. jof

    array of pointers to a structure

    jof, Aug 22, 2004, in forum: C Programming
    Replies:
    1
    Views:
    389
    -berlin.de
    Aug 22, 2004
  3. Sean
    Replies:
    2
    Views:
    641
    loufoque
    Sep 24, 2006
  4. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    335
    Chris Torek
    Apr 6, 2007
  5. cerr

    pointers, pointers, pointers...

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

Share This Page