代码之家  ›  专栏  ›  技术社区  ›  T.T.T.

找不到静态字符数组并且无法使用strncpy?

  •  2
  • T.T.T.  · 技术社区  · 16 年前
    char *  function decode time()
    { 
    
       tm *ptm; //time structure
        static char timeString[STRLEN]; //hold string from asctime()
    
        ptm = gmtime( (const time_t *)&ltime ); //fill in time structure with ltime
    
        if(ptm) 
        {
    
           strncpy(timeString, asctime( ptm ), sizeof(timeString) ); 
    //EDIT  
    sprintf(test, "Sting is: %s", timeString);
    
    
           return timeString;
    .
    .
    } //end function
    


    timeString CXX0017:错误:未找到符号“timeString”

    但是,当我从timeString中删除work“static”时,它确实正确地填充了字符串,但现在是一个本地副本,将被销毁。

    Visual Studio 6.0-MFC

    谢谢。

    编辑 “test”字符串不包含timeString的值。

    2 回复  |  直到 16 年前
        1
  •  2
  •   user411313    16 年前

    function_decode_time() function decode time()

    对于本地静态时间字符串,将使用“\0”初始化整个时间字符串,而不保证使用静态时间字符串 如果没有static,则调用上下文中的返回值是未定义的。

    strncpy不会在timeString中添加“\0”以使用“sizeof(timeString)”,请参见定义;

    char * functionDecodeTime()
    {
      tm *ptm; /* time structure */
      static char timeString[STRLEN]; /* hold string from asctime() */
    
      memset( timeString, 0 , sizeof timeString ); /* entire content always is defined ! */
    
      ptm = gmtime( (const time_t *)&ltime ); //fill in time structure with ltime
    
      if( ptm )
      {
        strncpy(timeString, asctime( ptm ), sizeof(timeString)-1 );
      }
    
      return timeString;
    }
    

        2
  •  2
  •   Clifford    16 年前

    你能改用VC++2010 Express吗?它是免费的,除非你使用的是“视觉”设计器或MFC,它可能会更好。

    我已经很长时间没有使用VC++6.0了,但是我使用过的许多其他调试器似乎都在与静态变量作斗争,一个简单的解决方案是:

    static char timeString[STRLEN]; //hold string from asctime()
    #if _DEBUG
    char* timeStringDebugRef = timeString;
    #endif
    

    timeStringDebugRef 而不是 timeString .


    [编辑]

    VC++6.0支持多种调试格式,并为链接器和编译器提供选项( described here ). 请确保您对其进行了适当的配置,也许?


    推荐文章