Thread: passing pointer to string between functions

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    passing pointer to string between functions

    Hi all
    I was wondering if you could help me figure something out.
    I have a pointer P to a string S. I have function A and function B.
    Function A calls function B with the pointer P.
    Function B moves pointer P along the string S.
    When function B finishes and I'm back in function A, pointer P (in function A) stays in the same position it was before calling function B.
    Function B cannot return the pointer P (since it returns another parameter).
    Do I need to make the pointer P a global parameter or is there another way of solving this???
    THANKS!!!
    Liat.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Since C passes parameters by value, to update a pointer, you must pass a pointer to pointer. For example:
    Code:
    int B(char **P)
    To make it simpler in B to move P, you can declare within B a local pointer initialized to *P. And to call B from A:
    Code:
    char *P = S;
    B(&P);
    EDIT: Changed int to char since the reference was to a string.
    Last edited by swoopy; 11-20-2007 at 09:24 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    11
    ha ha sounds logical...
    Thanks so much!

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    mmm... still not working :(

    Hi again,

    I've tried what swoopy has suggested, but I'm still getting the same problem (the pointer in the original function doesn't move).

    I've done something like this:
    Code:
    int funB(**char p)
    {
           char *c;
           c=*p;
           c++;
          fprintf(stderr,"after moving the pointer *c is: %c\n",*c);
          return 1;
    }
    int funA()
    {
           int number;
           char *c;
           fprintf(stderr,"*c is: %c\n",*c);
           number=funB(&c);
           fprintf(stderr,"after calling funB *c is: %c\n",*c);
    }
    I'm pretty sure this is what i've done...
    This doesn't work for me, is it suppose to? Or is something wrong with it?
    THANKS AGAIN!
    Liat.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    figured it out.

    Thanks anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM