Thread: Warning message - What to do

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Warning message - What to do

    Hi!

    I'm writing a function which returns some string.
    Here's the code:
    Code:
    char *Text (char File[])
    {
        FILE *fin = NULL;
        short Lines = 0, j=0;
        char Character, temp1[100], temp2[100], *temp1kaz, *temp2kaz, temp3[100], *temp3kaz;
    
        strncpy (File, "./DATA/LANGUAGE/sc.lng", sizeof ("./DATA/LANGUAGE/sc.lng"));
        if ((fin = fopen (File, "rt")) != NULL)
    	{
    		temp1kaz = temp1;
    		temp2kaz = temp2;
    		temp3kaz = temp3;
    		while (fscanf (fin, "%c", &Character) != EOF)
    		{
    			if (Character == '#') Lines++;
    			if (Lines == 5)
    			{
    			    j++;
    			    if (j >= 7)
                                {
                                    *temp3kaz = Character;
                                     temp3kaz++;
                                }
    			}
    			*temp3kaz = '\0'; 
    		}
    		fclose (fin);
                    return temp3;
    	}
    	else
    	{
    	     return NULL;
    	}
    }
    But I get a warning message which I can't solve it. This is the warning message "warning C4172: returning address of local variable or temporary". What to do??
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Make temp3 into a static variable:
    Code:
    static char temp3[100];
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    It's working now. THANKS!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Be careful returning static variables from a function. There can be difficult side effects if you don't walk softly.

    -Prelude
    My best code is written with the delete key.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do you understand the consequences of using a static variable in this manner (ie your need to either copy out the string in the external function, or at least be aware that there is only one temp3 variable in existence)? If not, post again and someone will advise.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    What should I do now? Use the static variable or not?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    If you declare the variable as static in your function, this means that the value of the variable will be stored in memory, also when the function ends. When the function is called again, the last variable's value is the same as when it ended.

    Assume you have this function:

    Code:
    void function_a (void)
    {
        static int a = 0;
        
        a++;
        
        printf ("%d\n", a);
    }
    The variable a is initialised to 0. Then it is increased, so at the value 1 will be printed. When the function finishes, the variable will be kept in memory with value 1. When the function is called again, the value is still 1 and so the value 2 will be printed.

    The problem is that you pass back the address of the start of temp3, which is a local variable. This means, that temp3 doesn't exist when the function has finished.

    What you could do is, allocating some memory within the function and pass the address to that allocated memory back.

    Code:
    char *Text (char File[])
    {
        char *temp3;
    
        temp3 = malloc (sizeof (char) * 100);
    
        if (temp3 == NULL)
            return temp3;
    
        ...
        
        return temp3;
    
       ...
    }
    
    void calling_function ()
    {
        char *str;
        ...
        
        str = Text (FileName);
    
        ...
    
        free (str);
    }
    Note that the allocated memory is not local. Only the pointer temp3 is local. But that doesn't matter since the address is passed back and stored in str.

    In my opinion using a local static variable in this situation is unnecessary. Local static variables are only requried if the value in the function must be remembered when the function is called again.
    Last edited by Shiro; 05-11-2002 at 07:59 AM.

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Ok, now I understand this static declaration.

    In the Text function I wrote this and it is not working. I get no text. The string temp3 is empty.
    Code:
    char *Text (char File[])
    {
        FILE *fin = NULL;
        short Lines = 0;
        char Character, *temp3 = NULL;
    
        if ((fin = fopen (File, "rt")) != NULL)
    	{
    		temp3 = malloc (sizeof (char) * 100);
    		while (fscanf (fin, "%c", &Character) != EOF)
    		{
    			if (Character == '#') Lines++;
    			*(temp3++) = Character;
    		}
                    *temp3 = '\0';
    		fclose (fin);
    		return temp3;
    	}
    	else
    	{
    		return NULL;
    	}
    }
    What is wrong?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    34
    temp3 is pointing at the end of the array. You need to use two pointers lad.

    Code:
    char *Text (char File[])
    {
      FILE *fin = NULL;
      short Lines = 0;
      char Character;
      char *temp3;
      char *pToArray;
    
      temp3 = pToArray = NULL: //initialize pointers
    
      if ((fin = fopen (File, "rt")) != NULL)
        {
          temp3 = malloc (sizeof (char) * 100);
          if ( temp3 == NULL) exit(1); //allocation failed
          pToArray = temp3;
                                    
           while (fscanf (fin, "%c", &Character) != EOF)
           {
              if (Character == '#') Lines++;
              *(pToArray++) = Character;
           }
              *pToArray = '\0';
              fclose (fin);
              return temp3;
             }
          else
          {
             return NULL;
          }
    }
    I did not test the code, but this is the idea.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    That is caused by the increment of the pointer. You increase the pointer, so at the end the pointer temp3 points at '\0'.

    Assume temp3 points at address 0 in the beginning. The first character is read and stored at that address. Then you increment the pointer so it points to 1. And this goes on until you've read all. Then you increment the pointer again and write '\0' to it. If you have read 5 characters then '\0' is placed on address 5. The pointer temp3 also points to address 5. But what you want is it to point to the start of the string, which is at address 0.

    A possible solution: define another pointer temp4. Let this point to temp3. And at the end return temp4. Such like this:

    Code:
    char *Text (char File[])
    {
        FILE *fin = NULL;
        short Lines = 0;
        char Character, *temp3 = NULL, *temp4 = NULL;
    
        if ((fin = fopen (File, "rt")) != NULL)
    	{
    		temp3 = malloc (sizeof (char) * 100);
                                    temp4 = temp3;
    		while (fscanf (fin, "%c", &Character) != EOF)
    		{
    			if (Character == '#') Lines++;
    			*(temp3++) = Character;
    		}
                    *temp3 = '\0';
    		fclose (fin);
    		return temp4;
    	}
    	else
    	{
    		return NULL;
    	}
    }

  11. #11
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    It is working now. Thanks guys!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. warning message
    By potbelle in forum C Programming
    Replies: 11
    Last Post: 04-03-2002, 10:20 PM