functions and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • askcq
    New Member
    • Mar 2007
    • 63

    functions and arrays

    void charfunc(char* );

    char* charfuncptr(cha r* );

    main()
    {
    char array[3]="ebc";

    char* narr;

    narr = array;

    charfunc(array) ;

    narr = charfuncptr(&ar ray[0]);

    printf("\nnew array is %s",narr);

    printf("\nnew array is %s",array);

    }


    void charfunc(char* ptr) //send an string
    {

    printf("\nstrin g is %s", ptr);

    }


    char* charfuncptr(cha r* ptr)
    {
    //ptr[0]='s';
    //ptr[1]='h';
    //ptr[2]='a';
    ptr="sha";
    printf("\nmdifi ed string is %s",ptr);
    return ptr;

    }

    when I modify the pointer (ptr) in function (charfuncptr) as ptr="sha"
    the array pointed by the pointer is not modified.
    can anyone help on this
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    First off with 62 posts you haven't learned how to use the CODE tags?
    Second what compiler are you using?

    char array[3]="ebc"; should throw an error. if you want to initialize the array = {'e','b','c'}; Second you are using "array" as a variable but it can be used as a keyword in c++ so i would look at using a different variable name.

    When you pass an array into a function you are only passing a pointer not the array itself. So if you pass (array) you just pass a pointer to the first element of the array.

    in your second function call you do (&array[0]) what are you hoping is the difference between the two function calls?

    If you remove the (&array[0]) and replace it with just (array) when you do the ptr = "sha" the ptr address will change then when you return the ptr the array variable will have the old string and the narr variable will have the one you initalize in the charfuncptr. I would recommend reading up on arrays and how they are passed into functions. Also i would play around with it in a simple test program (kind of like what you have constructed above).

    Comment

    • askcq
      New Member
      • Mar 2007
      • 63

      #3
      Since writing the above code would be very time consuming, C permits two alternate ways of achieving the same thing. First, one might write:

      char my_string[40] = {'T', 'e', 'd', '\0',};

      But this also takes more typing than is convenient. So, C permits:

      char my_string[40] = "Ted";

      array can be intialied by anyways above.....

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by askcq
        when I modify the pointer (ptr) in function (charfuncptr) as ptr="sha"
        the array pointed by the pointer is not modified.
        can anyone help on this
        It's because your function has a char* argument. Remember, this a copy of the pointer used to call the function. When the function changes the pointer, all it does is change the copy.

        If you need to chnage the pointer used to call the function, then you bneed to pass in the address of that pointer, that is, a char**.

        Comment

        • edwardrsmith
          New Member
          • Feb 2008
          • 62

          #5
          The code seems to have a few bizarre bits which could possibly be causing your problem. The first one and the one which I believe is most likely to be causing your problem is:
          Code:
          narr = charfuncptr(&array[0]);
          The '&' and '[0]' are unnecessary and could easily be causing a problem. With how your code is written at this point, both of the arrays 'char*' should equal the new array. I don't know if that is your intent but I thought I should mention it.

          Comment

          Working...