modify structure array pointer in the function

Discussion in 'C Programming' started by s88, Nov 21, 2005.

  1. s88

    s88 Guest

    Howdy:
    the follows is my program, I wanna change my structure array
    pointer in the function "testfunc", but I fail..., I also try to call
    the testfunc by reference, but the compiler says "test6.c:35: error:
    incompatible type for argument 1 of `testfunc'". Can I make my purpose
    in C?
    and how?


    typedef struct xxxx *xxxx_ptr;

    typedef struct xxxx{
    int just;
    long for_the;
    char *test;
    }xxxx;

    void testfunc(xxxx_ptr ptr){
    ptr++;
    }

    int main(void){
    xxxx XXXX[10];
    xxxx_ptr ptr = &XXXX[0];
    printf("%x\n",ptr);
    testfunc(ptr);
    printf("%x\n",ptr);
    return 0;
    }

    Thank you all.
    Dave.
    s88, Nov 21, 2005
    #1
    1. Advertising

  2. s88

    pete Guest

    s88 wrote:
    >
    > Howdy:
    > the follows is my program, I wanna change my structure array
    > pointer in the function "testfunc", but I fail..., I also try to call
    > the testfunc by reference, but the compiler says "test6.c:35: error:
    > incompatible type for argument 1 of `testfunc'". Can I make my purpose
    > in C?
    > and how?
    >
    > typedef struct xxxx *xxxx_ptr;
    >
    > typedef struct xxxx{
    > int just;
    > long for_the;
    > char *test;
    > }xxxx;
    >
    > void testfunc(xxxx_ptr ptr){
    > ptr++;
    > }
    >
    > int main(void){
    > xxxx XXXX[10];
    > xxxx_ptr ptr = &XXXX[0];
    > printf("%x\n",ptr);
    > testfunc(ptr);
    > printf("%x\n",ptr);
    > return 0;
    > }


    Every oportunity to use typedef,
    isn't necessarily a good one.

    /* BEGIN new.c */

    #include <stdio.h>

    struct my_struct {
    int just;
    long for_the;
    char *test;
    };

    void testfunc(struct my_struct **ptr)
    {
    ++*ptr;
    }

    int main(void)
    {
    struct my_struct array[10];
    struct my_struct *ptr = array;

    printf("%p\n", (void *)ptr);
    testfunc(&ptr);
    printf("%p\n", (void *)ptr);
    return 0;
    }

    /* END new.c */

    --
    pete
    pete, Nov 21, 2005
    #2
    1. Advertising

  3. s88

    Guest

    On the contrary when I copied your code into old Turbo C its compiled
    and run just fine.
    So I guess its a compiler issue
    , Nov 21, 2005
    #3
  4. On 20 Nov 2005 18:10:30 -0800, "s88" <> wrote:

    >Howdy:
    > the follows is my program, I wanna change my structure array
    >pointer in the function "testfunc", but I fail..., I also try to call
    >the testfunc by reference, but the compiler says "test6.c:35: error:
    >incompatible type for argument 1 of `testfunc'". Can I make my purpose
    >in C?
    >and how?
    >
    >
    >typedef struct xxxx *xxxx_ptr;
    >
    >typedef struct xxxx{
    > int just;
    > long for_the;
    > char *test;
    >}xxxx;
    >
    >void testfunc(xxxx_ptr ptr){


    C passes arguments by value. The ptr is this function is a copy of
    the argument value in the calling statement.

    > ptr++;


    This updates the copy.

    >}


    The copy is destroyed as part of the process of exiting the function.
    The updated value no longer exists.

    >
    >int main(void){
    > xxxx XXXX[10];
    > xxxx_ptr ptr = &XXXX[0];
    > printf("%x\n",ptr);


    %x expects an unsigned int. You are passing a pointer. This invokes
    undefined behavior.

    The only portable way to print a pointer value is to use %p and cast
    the value to a void *.

    > testfunc(ptr);


    The value of an argument cannot be changed by the called program.

    > printf("%x\n",ptr);
    > return 0;
    >}


    The two usual solutions are:

    Have the function return the updated value and call the function
    in an assignment statement that assigns the new value to a suitable
    variable.

    Pass the address of the variable to be updated and let the
    function update the variable by dereferencing the address.


    <<Remove the del for email>>
    Barry Schwarz, Nov 21, 2005
    #4
  5. "s88" <> writes:
    > the follows is my program, I wanna change my structure array
    > pointer in the function "testfunc", but I fail..., I also try to call
    > the testfunc by reference, but the compiler says "test6.c:35: error:
    > incompatible type for argument 1 of `testfunc'". Can I make my purpose
    > in C?
    > and how?
    >
    >
    > typedef struct xxxx *xxxx_ptr;
    >
    > typedef struct xxxx{
    > int just;
    > long for_the;
    > char *test;
    > }xxxx;
    >
    > void testfunc(xxxx_ptr ptr){
    > ptr++;
    > }
    >
    > int main(void){
    > xxxx XXXX[10];
    > xxxx_ptr ptr = &XXXX[0];
    > printf("%x\n",ptr);
    > testfunc(ptr);
    > printf("%x\n",ptr);
    > return 0;
    > }


    Are you sure that's the actual code you fed to the compiler? When I
    compiled it with gcc, I didn't get any diagnostics. When I
    upped the warning level (gcc -ansi -pedantic -Wall -W -c tmp.c), I got:

    tmp.c: In function `main':
    tmp.c:16: warning: implicit declaration of function `printf'
    tmp.c:16: warning: unsigned int format, xxxx_ptr arg (arg 2)
    tmp.c:18: warning: unsigned int format, xxxx_ptr arg (arg 2)

    You need a "#include <stdio.h>" to make the printf() calls legal.
    Also, the correct format for a pointer is "%p", not "%x":

    pirntf("%p\n", (void*)ptr);

    (Since you got an error message on line 35, what you posted apparently
    is shorter than the actual code.)

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Nov 21, 2005
    #5
  6. s88

    Blair Craft Guest

    Try this:
    /* start */
    typedef struct xxxx *xxxx_ptr;

    typedef struct xxxx{
    int just;
    long for_the;
    char *test;
    }xxxx;

    void testfunc(xxxx_ptr **ptr){
    (*ptr)++;
    }

    int main(void){
    xxxx XXXX[10];
    xxxx_ptr ptr = XXXX;
    printf("%x\n",ptr);
    testfunc(&ptr);
    printf("%x\n",ptr);
    return 0;
    }
    /* end */
    s88 wrote:
    > Howdy:
    > the follows is my program, I wanna change my structure array
    > pointer in the function "testfunc", but I fail..., I also try to call
    > the testfunc by reference, but the compiler says "test6.c:35: error:
    > incompatible type for argument 1 of `testfunc'". Can I make my purpose
    > in C?
    > and how?
    >
    >
    > typedef struct xxxx *xxxx_ptr;
    >
    > typedef struct xxxx{
    > int just;
    > long for_the;
    > char *test;
    > }xxxx;
    >
    > void testfunc(xxxx_ptr ptr){
    > ptr++;
    > }
    >
    > int main(void){
    > xxxx XXXX[10];
    > xxxx_ptr ptr = &XXXX[0];
    > printf("%x\n",ptr);
    > testfunc(ptr);
    > printf("%x\n",ptr);
    > return 0;
    > }
    >
    > Thank you all.
    > Dave.
    >
    Blair Craft, Nov 21, 2005
    #6
    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. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    347
    Tim Rentsch
    Oct 9, 2004
  2. Kiran
    Replies:
    6
    Views:
    408
    Christopher Benson-Manica
    Jan 18, 2007
  3. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    674
    Martin Ambuhl
    Jan 28, 2008
  4. A
    Replies:
    27
    Views:
    1,593
    Jorgen Grahn
    Apr 17, 2011
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page