Thread: Passing string variables

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Passing string variables

    Hi,
    Could someone pls tell me what I am doing wrong in the code below.
    I'm trying to read two strings into two variables.
    When I printf in the function ProcessString, I get what I am looking for. However when I printf first and second in

    main, I don't get those strings.
    I'm I calling ProcessString the wrong way from main?
    Thanks.

    Code:
    #include <stdio.h>
    
    void ProcessString(FILE *infile, char *fval, char *sval)
    {
    `int i = 0;
     int ch;
     char localfval[1024];
     char localsval[1024];
    
    if ((ch = fgetc(infile)) != EOF)
      {
       while (ch != 'X')
        {
          localfval[i++] = ch;
        }
        localfval[--i] = '\0';
        i = 0;
    
       while (ch != 'Y')
        {
          localsval[i++] = ch;
        }
        localsval[--i] = '\0';
    
      }
    fval = localfval;
    sval = localsval;
    printf("%s, %s\n", localfval, localsval);
    }
    
    
    int main()
    {
    FILE *infile;
    char *first;
    char *second;
    
    if (infile = fopen("filename.txt", "r") == NULL)
    exit(0);
    
    ProcessString(infile, first, second);
    
    printf("The first string is %s\n", first);
    printf("The second string is %s\n", second);
    
    fclose(infile);
    
    return (1)
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're pointing at local variables, which are destroyed as soon as the function call ends. You need to allocate space with malloc, or some such, or pass it arrays, and strcpy the strings to whatever you pass it. Oh, and if you want to make a pointer point to some space you allocate inside the function, you'll have to use a pointer to a pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Thanks Quzah for your help. Unfortunately, I'm not that versed in C programming so could you please elaborate further? Maybe with a couple of lines of code.
    Thanks.

    Koolsafe

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    User arrays:
    Code:
    int main( void )
    {
        char buf1[ BUFSIZ ], buf2[ BUFSIZ ];
    
        yourfunction( buf1, buf2 ); /* pass two arrays to use */
    
        ...do stuff...
    
        return 0;
    }
    The problem with this is your calling function doesn't know the size of your buffers, so you'll just assume you have enough. This may or may not be a problem. Or, you can pass another argument or so, telling them the size of them.

    Use dynamic allocation:
    Code:
    int main( void )
    {
        char *buf1, *buf2;
    
        yourfunction( &buf1, &buf2 ); /* pass pointers to the pointers, allocate and fill */
    
        ...do stuff...
    
         return 0;
    }
    Here you allocate memory inside your function, and update the pointers so that outside the function you're still pointing at the right spot. The downside to this is that you have to make sure you free your allocated stuff correctly. You'll also have to check outside to make sure you've been given some memory (make sure your pointers aren't NULL after the function's been called) when the function ends.

    The first version:
    Code:
    void yourfunction( char buf1[], char buf2[] )
    {
        ...work with them directly...
    }
    Here you're assuming you're being passed an array and you're allowed to fill it. You can't be passing string literals, or anything else that's read only.

    The second version:
    Code:
    void yourfunction( char **buf1, char **buf2 )
    {
        *buf1 = malloc( somesize );
        *buf2 = malloc( somesize );
    
        ...now work with them, assuming they're not null...
    }
    You'll need to test the return value (ie: your pointer) to make sure malloc didn't return NULL. If it did, it means you're out of memory, so don't try to use what it returned. When you're all done, you have to free whatever you've allocated. So call free on the pointers (outside the function) when you're done with them, once each.

    You have to use a pointer to a pointer, because everything in C is 'pass by value'. Thus, if you make a pointer like you had it origionally, you are in effect making a copy of the pointer, so any changes are lost, because you're not really working with the origional one.

    There's a FAQ or two on dynamic memory allocation I believe, in the FAQ section.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or use the same interface which fgets() uses, which amounts to
    function ( char *storeTheAnswerHere, int maxLengthOfAnswer );

    This makes the entire problem of how much space to allocate (whether an array or via malloc) the responsibility of the caller. The called function just does it's job with the resources provided.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM