Basic Pointer Question...

Discussion in 'C Programming' started by shan_rish@yahoo.com, Aug 11, 2005.

  1. Guest

    Hi CLCers,
    I coded a function to allocate memory and i am passing a pointer to the
    function. The code is compiling but throws error and closes while
    executing. The program is as below:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int *p;
    void mem_fun(int *i);
    mem_fun(p);
    printf("%d\n",*p);
    getch();
    return 0;
    }

    void mem_fun(int *i)
    {

    printf("%u\n",i);
    i=malloc(sizeof(int));
    *i=10;
    printf("%d\n",*i);
    }

    Please help in understanding the problem. Advanced thanks.
    Cheers
    Shan
    , Aug 11, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi CLCers,
    > I coded a function to allocate memory and i am passing a pointer to the
    > function. The code is compiling but throws error and closes while
    > executing. The program is as below:
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main()
    > {
    > int *p;
    > void mem_fun(int *i);
    > mem_fun(p);
    > printf("%d\n",*p);
    > getch();
    > return 0;
    > }
    >
    > void mem_fun(int *i)
    > {
    >
    > printf("%u\n",i);
    > i=malloc(sizeof(int));
    > *i=10;
    > printf("%d\n",*i);
    > }
    >
    > Please help in understanding the problem. Advanced thanks.
    > Cheers
    > Shan


    You are not using pointers correctly. Here's a good website with an
    easy to understand explanation of pointers:

    http://cslibrary.stanford.edu/

    I removed your first printf from the mem_fun() since I had no idea what
    you were trying to do.

    Here's your code corrected:

    #include <stdio.h>
    #include <stdlib.h>

    void mem_fun(int **);

    int
    main() {

    int *p;
    mem_fun(&p);
    (void)printf("%d\n",*p);
    exit(0);
    }

    void
    mem_fun(int **i)
    {

    *i = malloc(sizeof(int));
    if(*i == NULL)
    return;
    **i = 10;
    (void)printf("%d\n",**i);
    return;
    }
    , Aug 11, 2005
    #2
    1. Advertising

  3. Michael Mair Guest

    wrote:
    > Hi CLCers,
    > I coded a function to allocate memory and i am passing a pointer to the
    > function. The code is compiling but throws error and closes while
    > executing. The program is as below:
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main()
    > {
    > int *p;
    > void mem_fun(int *i);


    Do not hide your function declarations within other functions.

    > mem_fun(p);


    Let us recapitulate: C passes arguments by value.
    You have given no value to p, so making a copy of p for mem_fun
    invokes undefined behaviour. From now on, anything can happen,
    including your harddisk being formatted.
    If you want to change something using a function, you have
    to pass its _address_, not the object itself (which will only
    copied but not changed).
    Note: The address is also passed by value, but this value is
    sufficient to do something.

    > printf("%d\n",*p);
    > getch();


    This function is not defined in standard C.

    > return 0;
    > }
    >
    > void mem_fun(int *i)
    > {
    >
    > printf("%u\n",i);


    The format specifier for pointers is %p.
    If pointers and integers have different sizes, the above may
    go wrong.

    > i=malloc(sizeof(int));


    Better:
    i = malloc(sizeof *i);
    This works even if you change the type to i.

    You forgot to check whether malloc() succeeded.

    > *i=10;
    > printf("%d\n",*i);


    As you work on a copy of a pointer value which expires at the
    end of the function, you have produced a memory leak.

    > }
    >
    > Please help in understanding the problem. Advanced thanks.


    Better:

    #include<stdio.h>
    #include<stdlib.h>

    void mem_fun (int **i);

    int main (void)
    {
    int *p = 0;
    mem_fun(&p);
    if (p)
    printf("%p : %d\n", p, *p);
    free(p);
    getch();
    return 0;
    }

    void mem_fun (int **i)
    {
    *i = malloc(sizeof **i);
    if (*i)
    **i = 10;
    else {
    fprintf(stderr, "mem alloc trouble\n");
    exit(EXIT_FAILURE); /* Replace by _real_ error
    ** handling whenever possible */
    }
    }

    As you see, I check _twice_ whether malloc() succeeded to be
    on the safe side. This is because mem_fun() does not directly
    tell me whether it was successful. You may consider returning
    *i/int * to signal success or failure.
    In addition, as malloc() is hidden within a function, free()
    can be easily forgotten.
    It is usually better to malloc() on the same "level" as you free()
    or to provide companion functions to be called from the same
    level performing malloc() and free().


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Aug 11, 2005
    #3
  4. Guest

    Hi Guys,
    Thanks for your reply. Now i understand what the problem is. When
    coding with pointers, i stumble a lot, even though i know that values
    are passed to C functions. Thanks again for your time for clearing my
    doubt.
    Cheers
    Shan
    , Aug 11, 2005
    #4
  5. Michael Mair wrote:

    <snip>

    > Better:
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > void mem_fun (int **i);
    >
    > int main (void)
    > {
    > int *p = 0;
    > mem_fun(&p);
    > if (p)
    > printf("%p : %d\n", p, *p);


    typo? printf("%p : %d\n",(void *) p, *p);

    Krishanu
    Krishanu Debnath, Aug 11, 2005
    #5
  6. Guest

    >Better:

    >#include<stdio.h>
    >#include<stdlib.h>



    >void mem_fun (int **i);



    >int main (void)
    >{
    > int *p = 0;

    hi,Michael
    maybe "int *p = (void *)0; " is better than "int *p = 0; ".
    and, I always use NULL to initilize a pointer. do you think it's a good
    style?
    Is the macro "NULL" a standard C's definition?
    any word would be appreciated.
    > mem_fun(&p);
    > if (p)
    > printf("%p : %d\n", p, *p);
    > free(p);
    > getch();
    > return 0;




    }
    , Aug 11, 2005
    #6
  7. wrote:
    > >Better:

    >
    > >#include<stdio.h>
    > >#include<stdlib.h>

    >
    >
    > >void mem_fun (int **i);

    >
    >
    > >int main (void)
    > >{
    > > int *p = 0;

    > hi,Michael
    > maybe "int *p = (void *)0; " is better than "int *p = 0; ".


    No, both are same.

    > and, I always use NULL to initilize a pointer. do you think it's a good
    > style?
    > Is the macro "NULL" a standard C's definition?
    > any word would be appreciated.


    Did you read FAQs?

    http://www.eskimo.com/~scs/C-faq/q5.4.html

    Krishanu

    --
    "Be nice to nerds. Chances are you'll end up working for one."
    --Bill Gates
    Krishanu Debnath, Aug 11, 2005
    #7
  8. wrote:

    > maybe "int *p = (void *)0; " is better than "int *p = 0; ".
    > and, I always use NULL to initilize a pointer. do you think it's a good
    > style?
    > Is the macro "NULL" a standard C's definition?
    > any word would be appreciated.


    Look up Q 5.4 in the FAQ. I would also recommend to read the whole
    section 5, which deals with NULL pointers. The FAQ can be found at
    http://www.eskimo.com/~scs/C-faq/top.html. It used to be anyway; I
    couldn't connect to that site today :(

    Peter
    Peter Pichler, Aug 11, 2005
    #8
  9. Michael Mair Guest

    Krishanu Debnath wrote:
    > Michael Mair wrote:
    >
    > <snip>
    >
    >>Better:
    >>
    >>#include<stdio.h>
    >>#include<stdlib.h>
    >>
    >>void mem_fun (int **i);
    >>
    >>int main (void)
    >>{
    >> int *p = 0;
    >> mem_fun(&p);
    >> if (p)
    >> printf("%p : %d\n", p, *p);

    >
    >
    > typo? printf("%p : %d\n",(void *) p, *p);


    True. Thanks.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Aug 11, 2005
    #9
  10. Guest


    > Here's your code corrected:


    I dont think so !!!
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > void mem_fun(int **);
    >
    > int
    > main() {
    >
    > int *p;
    > mem_fun(&p);
    > (void)printf("%d\n",*p);
    > exit(0);
    > }
    >
    > void
    > mem_fun(int **i)
    > {
    >
    > *i = malloc(sizeof(int));
    > if(*i == NULL)
    > return;
    > **i = 10;
    > (void)printf("%d\n",**i);
    > return;

    what you are returning and why ? Check your declaration.
    > }
    , Aug 12, 2005
    #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. Replies:
    10
    Views:
    697
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,250
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    634
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page