Thread: Passing char ** (is losing reference)

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    27

    Passing char ** (is losing reference)

    Hi, I'm working on my second C program ever and I'm lost...

    I have 2 source files. I'm passing a char ** from one function in one file to another function in a second file, it's losing the reference and pointing to garbage. Can some one tell me what I'm doing wrong ?

    Here's some code
    Code:
    FILE1.C
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	char ** input_word_list;
    
    	/* THIS WORKS WELL */
                    input_word_list = get_input_line();
    
    	/* THIS WORKS FINE */
    	int i=0;
    	while ( (strcmp(input_word_list[i], EOW)) ){
    		printf("----> %s\n",input_word_list[i++]);
    	}
    	
    	get_db_con(input_word_list); 
    return 0;
    }
    FILE2.c
    
    int get_db_con(char ** input_word_list)
    {
    
                    /* THIS FAILS WHY ???? */
                    int i=0;
    	while ( (strcmp(input_word_list[i], EOW)) ){
    		printf("FROM get_db_con ----> %s\n",input_word_list[i++]);
    	}
    
    return 0;
    }
    
    and here is the included header for each FILE1/2
    
    #define EOW     "~"
    
    
    int get_db_con(char **);
    char ** get_input_line(void);
    ***********************************************

    I used malloc in the first file in a local function, do I need to do somthing FAR etc.. ?? How do I ?
    Is that why it's losing the data, cause it's out of scope, how do I scope it right ?


    Thanks for the help,
    -Areal
    Last edited by Areal Person; 05-31-2010 at 02:39 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    27
    OK, I got it !

    I was calling msql stuff and it kills all my variables Da...

    So I'm going to restructure the flow and it will work.

    Thank you much,
    -Areal

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> I used malloc in the first file in a local function, do I need to do somthing FAR etc.. ?? How do I ?

    No call was made to malloc in what you posted.

    >> Is that why it's losing the data, cause it's out of scope, how do I scope it right ?

    What do you mean by "losing the data"? You haven't showed when input_word_list is allocated or freed -- we have no sense of how your memory is managed at all.

    Note that you are asking about a logical bug, and you posted a few lines of code as if there were merely a syntax error. You need to post a complete program that has the same bug if you want us to help you fix it.

    >> while ( (strcmp(input_word_list[i], EOW)) )

    Unrelated to your problem - but you may as well make it clear what kind of comparison you expect to end the loop.

    while ( strcmp(input_word_list[i], EOW) == 0 )

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >> while ( (strcmp(input_word_list[i], EOW)) )

    Unrelated to your problem - but you may as well make it clear what kind of comparison you expect to end the loop.

    while ( strcmp(input_word_list[i], EOW) == 0 )
    These two comparisons are not the same, in fact they make the loop behave completely the opposite from each other.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM