#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    pointer to an array in a function


    Hi,
    I wanted to find out how i would point to the array arr in function get(char *p) so that i ould print the contents of this array.
    Your help will be appreciated.



    int main(){
    char * ptr = NULL:

    get(ptr);
    printf("contents: %s:, ptr);
    }

    void get(char *p){
    char [5]mm = NULL;
    int i;

    for(ii = o;i <5;i++){
    mm[i] = i;
    }

    p = mm; /*I'm not sure about this part */
    }
    Last edited by js_badboy; September 11th, 2003 at 10:35 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    I'm no expert but i got a ? on here too and maybe this will help you and karma will come around and get my ? answered.

    First of all you have a bug in your for loop where as you don't have i declared but instead have ii in this line:for(ii = o;i <5;i++){
    needs to be: for(int i = 0;i <5;i++){ (0 is a number not a letter)
    then if all you want to do is output it to the screen, #include <fstream.h> and just after you set all the values in the array you can put a cout << mm[i] (That is in the same loop just after you set it) or the better way to do it is to loop back through the array and cout << mm[i]; for every cycle.

    try that.
    Thanks,

    Jonathan Cobb
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: pointer to an array in a function


    js_badboy, please use code tags when you post code.

    Your function get() does not accept the variable p by reference, so it is passed by value. Therefore changing p within the function does not change the ptr variable passed in. If you want to change the ptr variable that exists outside of the function, you must either pass it to the function by reference, or make the function return a value (instead of void), and assign this value to ptr:

    ptr = get();

    Also, the array within the function is only valid within the function itself. Once the function terminates, it no longer exists. So, if you have a pointer to this array, then it is fine to use it within the function, but as soon as you attempt to use it outside of the function, you will get junk values as the array no longer exists. Of course, if nothing has used this memory since the function terminated, the array's values may still remain in that memory location, therefore it may appear to be working... do not be fooled. If the array was declared globally, then the entire program as well as the function has access to the array (it exists for the duration of the program), and therefore, you can access it anywhere.

    Also, if you wish to print an array of characters to the screen using printf with the %s format specifier, then you must ensure that the array of chars is terminated by a NULL character, as this is what the %s specifier is expecting.

    You should also check over the syntax of your program. You do have numerous errors.

    I hope this helps.
    Last edited by Jason Doucette; September 12th, 2003 at 06:26 AM.

IMN logo majestic logo threadwatch logo seochat tools logo