warning: assignment makes pointer from integer without a cast

這個錯誤自己經常碰到的,不過這次自己一直在看自己的程序,而且還是從書上抄的,應改沒錯啊。誰知道是因爲自己的頭文件順序影響了。

因爲自己用的是strptime函數。所以需要明確請求使用X/Open的標準功能。所以應該在頭文件前加上#define  _XOPEN_SOURCE ,而且是必須在所有的頭文件前面。

至於#define _XOPEN_SOURCE的作用,自己也不太明瞭。搜索如下:

Glibc 所實現全部或部分規範下的功能有:
1.ISO C: C語言國際標準. 
2.POSIX: 操作系統的 ISO/IEC 9945 (aka IEEE 1003) 標準. 
3.Berkeley Unix: BSD 和 SunOS. 
4.SVID: V 系統接口描述. 
5.XPG: The X/Open Portability Guide.

程序中,爲了實現上述功能需要定義對應的宏。如第二個功能POSIX,應定義 _POSIX_SOURCE。類似的有 _BSD_SOURCE、_SVID_SOURCE、_XOPEN_SOURCE。
也就是說:
#define _XOPEN_SOURCE
是爲了可以使用 5. The X/Open Portability Guide 的功能。

[實用方法]
使用上述五種功能的方法有兩種:
    1)在cc命令中指定,如:cc -D _POSIX_SOURCE file.c
    2)將源程序的第一行設置爲:#define _POSIX_SOURCE 1

[_XOPEN_SOURCE是什麼?]
翻譯成漢語表達很蹩腳,英文原版解釋如下:
Macro: _XOPEN_SOURCE 
If you define this macro, functionality described in the X/Open Portability Guide is included. This is a superset of the POSIX.1 and POSIX.2 functionality and in fact _POSIX_SOURCE and _POSIX_C_SOURCE are automatically defined. 
As the unification of all Unices, functionality only available in BSD and SVID is also included. 
If the macro _XOPEN_SOURCE_EXTENDED is also defined, even more functionality is available. The exa functions will make all functions available which are necessary for the X/Open Unix brand.
書上代碼也貼下了:

#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
	struct tm	 *tm_ptr;
	struct tm    timestruct;  //申請了空間~~~
	time_t	 the_time;
	char		 buf[256];
	char		 *result;      //不必有內存空間~~~

	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	strftime( buf, 256, "%A %d %B, %I:%M %p", tm_ptr );
      
	printf( "strftime gives: %s\n", buf );

	strcpy( buf, "Thu 26 July 2007, 17:53 will do fine" );

	printf( "calling strptime with: %s\n", buf );
	tm_ptr = ×truct;

    result = strptime( buf, "%a %d %b %Y, %R", tm_ptr );     //strptime怎麼實現的?
	printf( "strptime consumed up to: %s\n", result );

	printf( "strptime gives: \n" );
	printf( "date: %02d/%02d/%02d\n",
			tm_ptr->tm_year % 100, tm_ptr->tm_mon+1, tm_ptr->tm_mday );
	printf("time: %02d:%02d\n",
			tm_ptr->tm_hour, tm_ptr->tm_min );
	exit(0);

}


發佈了47 篇原創文章 · 獲贊 7 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章