passing pointer to function - modify in different function not working

Discussion in 'C Programming' started by Kiran, Jan 18, 2007.

  1. Kiran

    Kiran Guest

    Hi all,
    What I am trying to do is to pass a pointer to the first element of
    an array to a function, modify it in that function, and then print out
    the values of the array (which has been modified by the function) in
    main. However, I am getting a segmentation fault. Here is the code:
    (Please note, the size is fixed in this code, but in my code where I am
    actually going to use this, the size of the array is not known until
    you get to the modify function which is why I am using pointer
    notation).

    #include <stdio.h>

    void modifiy( int* nums );

    int main() {
    int* nums;
    int i;

    for (i=0; i < 5; i++) {
    printf("%d", nums);
    }

    }

    void modify( int* nums ) {
    int i;
    nums = (int*)malloc( 5*sizeof(int));
    for (i=0; i < 5; i++) {
    nums = i;
    }
    }

    Once I can get this to work, I want to do the same thing w/ double
    arrays, so if there are any pitfalls associated with that, any help
    would be greatly appreciated.

    thanks!
    Kiran, Jan 18, 2007
    #1
    1. Advertising

  2. Kiran

    Ben Pfaff Guest

    "Kiran" <> writes:

    > int main() {
    > int* nums;
    > int i;
    >
    > for (i=0; i < 5; i++) {
    > printf("%d", nums);
    > }
    >


    You should return a value from main(), probably 0.

    > }
    >
    > void modify( int* nums ) {
    > int i;
    > nums = (int*)malloc( 5*sizeof(int));


    I don't recommend casting the return value of malloc():

    * The cast is not required in ANSI C.

    * Casting its return value can mask a failure to #include
    <stdlib.h>, which leads to undefined behavior.

    * If you cast to the wrong type by accident, odd failures can
    result.

    In unusual circumstances it may make sense to cast the return value of
    malloc(). P. J. Plauger, for example, has good reasons to want his
    code to compile as both C and C++, and C++ requires the cast, as he
    explained in article <9sFIb.9066$>.
    However, Plauger's case is rare indeed. Most programmers should write
    their code as either C or C++, not in the intersection of the two.

    When calling malloc(), I recommend using the sizeof operator on
    the object you are allocating, not on the type. For instance,
    *don't* write this:

    int *x = malloc (128 * sizeof (int)); /* Don't do this! */

    Instead, write it this way:

    int *x = malloc (128 * sizeof *x);

    There's a few reasons to do it this way:

    * If you ever change the type that `x' points to, it's not
    necessary to change the malloc() call as well.

    This is more of a problem in a large program, but it's still
    convenient in a small one.

    * Taking the size of an object makes writing the statement
    less error-prone. You can verify that the sizeof syntax is
    correct without having to look at the declaration.


    > for (i=0; i < 5; i++) {
    > nums = i;
    > }
    > }


    Your real problem is in the FAQ:

    4.8: I have a function which accepts, and is supposed to initialize,
    a pointer:

    void f(int *ip)
    {
    static int dummy = 5;
    ip = &dummy;
    }

    But when I call it like this:

    int *ip;
    f(ip);

    the pointer in the caller remains unchanged.

    A: Are you sure the function initialized what you thought it did?
    Remember that arguments in C are passed by value. The called
    function altered only the passed copy of the pointer. You'll
    either want to pass the address of the pointer (the function
    will end up accepting a pointer-to-a-pointer), or have the
    function return the pointer.

    See also questions 4.9 and 4.11.

    --
    "Large amounts of money tend to quench any scruples I might be having."
    -- Stephan Wilms
    Ben Pfaff, Jan 18, 2007
    #2
    1. Advertising

  3. Re: passing pointer to function - modify in different function notworking

    Ben Pfaff writes:
    > Your real problem is in the FAQ:


    He'd also be a step closer if main() actually called modify() :)

    --
    Hallvard
    Hallvard B Furuseth, Jan 18, 2007
    #3
  4. Kiran

    Kiran Guest

    Hi all,
    first of all thanks for your responses.
    yes, i forgot to put that in (and also the function prototype is
    misspelled), but the result is the same, and after reading the faq, i
    think i understand why its going wrong, but not how to fix it.

    This is my understanding.
    The pointer itself is a variable, in this case containing the address
    of the first element in the array called nums, so when i pass modify
    nums, the function modify gets its own copy of where nums in main
    points to? (but if thats the case, shouldnt they both still point to
    the same thing?) I think I am confusing myself here.

    anyway, thanks again for all of yoru help.

    Hallvard B Furuseth wrote:
    > Ben Pfaff writes:
    > > Your real problem is in the FAQ:

    >
    > He'd also be a step closer if main() actually called modify() :)
    >
    > --
    > Hallvard
    Kiran, Jan 18, 2007
    #4
  5. Re: passing pointer to function - modify in different function notworking

    Kiran writes:
    > This is my understanding.
    > The pointer itself is a variable, in this case containing the address
    > of the first element in the array called nums, so when i pass modify
    > nums, the function modify gets its own copy of where nums in main
    > points to? (but if thats the case, shouldnt they both still point to
    > the same thing?) I think I am confusing myself here.


    If you allocate the memory in main(), yes. Or if you just declare
    int nums[5];
    or whatever in main().

    If you allocate 'nums' in modify(), then when you modify the pointer
    in that function, the change doesn't affect 'nums' in main(). Unless
    you return 'nums' from modify() and do 'nums = modify()' in main,
    or if you do
    void modify(int **nums_ptr);
    and call it as
    main(&nums);

    --
    Hallvard
    Hallvard B Furuseth, Jan 18, 2007
    #5
  6. Kiran

    Default User Guest

    Re: passing pointer to function - modify in different function not working - TPA

    Kiran wrote:

    > Hi all,
    > first of all thanks for your responses.


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
    Default User, Jan 18, 2007
    #6
  7. Ben Pfaff <> wrote:

    > I don't recommend casting the return value of malloc():


    Neither does the FAQ: http://c-faq.com/malloc/cast.html (Not to say
    that your explanation wasn't excellent, just possibly unnecessary.)

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jan 18, 2007
    #7
    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. James Vanns
    Replies:
    7
    Views:
    7,035
    Evan Carew
    Jan 21, 2004
  2. Replies:
    8
    Views:
    553
    PJP of NYC
    May 24, 2005
  3. beetle
    Replies:
    2
    Views:
    907
    beetle
    Jan 25, 2005
  4. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  5. Vijai Kalyan
    Replies:
    4
    Views:
    704
    Vijai Kalyan
    Nov 8, 2005
Loading...

Share This Page