C ÓïÑÔÖеÄÖ¸ÕëºÍÄÚ´æй©
http://www.ibm.com/developerworks/cn/aix/library/au-toughgame/index.html
Ó¢ÎÄÔÎÄ
ÒýÑÔ
¶ÔÓÚÈκÎʹÓà C ÓïÑÔµÄÈË£¬Èç¹ûÎÊËûÃÇ C ÓïÑÔµÄ×î´ó·³ÄÕÊÇʲô£¬ÆäÖÐÐí¶àÈË¿ÉÄÜ»á»Ø´ð˵ÊÇÖ¸ÕëºÍÄÚ´æй©
¡£ÕâЩµÄÈ·ÊÇÏûºÄÁË¿ª·¢ÈËÔ±´ó¶àÊýµ÷ÊÔʱ¼äµÄÊÂÏî¡£Ö¸ÕëºÍÄÚ´æ驶ÔijЩ¿ª·¢ÈËÔ±À´ËµËƺõÁîÈËη¾å£¬µ«ÊÇÒ»µ©ÄúÁ˽âÁËÖ¸Õë¼°Æä¹ØÁªÄÚ´æ²Ù×÷µÄ»ù´¡£¬ËüÃǾÍÊÇÄúÔÚ C ÓïÑÔÖÐÓµÓеÄ×îÇ¿´ó¹¤¾ß¡£
±¾ÎĽ«ÓëÄú·ÖÏí¿ª·¢ÈËÔ±ÔÚ¿ªÊ¼Ê¹ÓÃÖ¸ÕëÀ´±à³ÌÇ°Ó¦¸ÃÖªµÀµÄÃØÃÜ¡£±¾ÎÄÄÚÈÝ°üÀ¨£º
µ¼ÖÂÄÚ´æÆÆ»µµÄÖ¸Õë²Ù×÷ÀàÐÍ
ÔÚʹÓö¯Ì¬ÄÚ´æ·ÖÅäʱ±ØÐ뿼Âǵļì²éµã
µ¼ÖÂÄÚ´æй©µÄ³¡¾°
Èç¹ûÄúÔ¤ÏÈÖªµÀʲôµØ·½¿ÉÄܳö´í£¬ÄÇôÄú¾ÍÄܹ»Ð¡ÐıÜÃâÏÝÚ壬²¢Ïû³ý´ó¶àÊýÓëÖ¸ÕëºÍÄÚ´æÏà¹ØµÄÎÊÌâ¡£
ʲôµØ·½¿ÉÄܳö´í£¿
Óм¸ÖÖÎÊÌⳡ¾°¿ÉÄÜ»á³öÏÖ£¬´Ó¶ø¿ÉÄÜÔÚÍê³ÉÉú³Éºóµ¼ÖÂÎÊÌâ¡£ÔÚ´¦ÀíÖ¸Õëʱ£¬Äú¿ÉÒÔʹÓñ¾ÎÄÖеÄÐÅÏ¢À´±ÜÃâÐí¶àÎÊÌâ¡£
»ØÒ³Ê×
δ³õʼ»¯µÄÄÚ´æ
ÔÚ±¾ÀýÖУ¬p
Òѱ»·ÖÅäÁË 10 ¸ö×Ö½Ú¡£Õâ 10 ¸ö×Ö½Ú¿ÉÄÜ°üº¬À¬»øÊý¾Ý£¬Èçͼ 1
Ëùʾ¡£
char *p = malloc ( 10 );
ͼ 1. À¬»øÊý¾Ý
Èç¹ûÔÚ¶ÔÕâ¸ö p
¸³ÖµÇ°£¬Ä³¸ö´úÂë¶Î³¢ÊÔ·ÃÎÊËü£¬Ôò¿ÉÄÜ»á»ñµÃÀ¬»øÖµ£¬ÄúµÄ³ÌÐò¿ÉÄܾßÓв»¿ÉÔ¤²âµÄÐÐΪ¡£p
¿ÉÄܾßÓÐÄúµÄ³ÌÐò´ÓδÔøÔ¤Áϵ½µÄÖµ¡£
Á¼ºÃµÄʵ¼ùÊÇʼÖÕ½áºÏʹÓà memset
ºÍ malloc
£¬»òÕßʹÓà calloc
¡£
char *p = malloc (10);
memset(p,’\0’,10);
ÏÖÔÚ£¬¼´Ê¹Í¬Ò»¸ö´úÂë¶Î³¢ÊÔÔÚ¶Ô p
¸³ÖµÇ°·ÃÎÊËü£¬¸Ã´úÂë¶ÎÒ²ÄÜÕýÈ·´¦Àí Null
Öµ£¨ÔÚÀíÏëÇé¿öÏÂÓ¦¾ßÓеÄÖµ£©£¬È»ºó½«¾ßÓÐÕýÈ·µÄÐÐΪ¡£
»ØÒ³Ê×
Äڴ渲¸Ç
ÓÉÓÚ p
Òѱ»·ÖÅäÁË 10 ¸ö×Ö½Ú£¬Èç¹ûij¸ö´úÂëƬ¶Î³¢ÊÔÏò p
дÈëÒ»¸ö 11 ×Ö½ÚµÄÖµ£¬Ôò¸Ã²Ù×÷½«ÔÚ²»¸æËßÄúµÄÇé¿öÏÂ×Ô¶¯´ÓÆäËûij¸öλÖÓ³Ôµô”Ò»¸ö×Ö½Ú¡£ÈÃÎÒÃǼÙÉèÖ¸Õë q
±íʾ¸ÃÄÚ´æ¡£
ͼ 2. Ôʼ q ÄÚÈÝ
ͼ 3. ¸²¸ÇºóµÄ q ÄÚÈÝ
½á¹û£¬Ö¸Õë q
½«¾ßÓдÓδԤÁϵ½µÄÄÚÈÝ¡£¼´Ê¹ÄúµÄÄ£¿é±àÂëµÃ×ã¹»ºÃ£¬Ò²¿ÉÄÜÓÉÓÚij¸ö¹²´æÄ£¿éÖ´ÐÐijЩÄÚ´æ²Ù×÷¶ø¾ßÓв»ÕýÈ·µÄÐÐΪ¡£ÏÂÃæµÄʾÀý´úÂëƬ¶ÎÒ²¿ÉÒÔ˵Ã÷ÕâÖÖ³¡¾°¡£
char *name = (char *) malloc(11);
// Assign some value to name
memcpy ( p,name,11); // Problem begins here
ÔÚ±¾ÀýÖУ¬memcpy
²Ù×÷³¢ÊÔ½« 11 ¸ö×Ö½Úдµ½ p
£¬¶øºóÕß½ö±»·ÖÅäÁË 10 ¸ö×Ö½Ú¡£
×÷ΪÁ¼ºÃµÄʵ¼ù£¬Ã¿µ±
Ïà¹ØÎĵµ£º
Ò»Ö±ÏëдһЩ±È½ÏCºÍC++Éè¼ÆÓë±à³Ì·ç¸ñµÄÎÄ×Ö£¬ÕýºÃ½üÀ´¹¤×÷ÈÎÎñ²»¶à£¬Äܹ»ÓÐʱ¼äÖØÐÂÕûÀíÒ»ÏÂ×Ô¼ºËùѧµÄ¶«¶«£¬ÓÚÊDZã¾ö¶¨¿ªÊ¼Ð´Õâ¸öÌâÄ¿£¬¶øÄ¿µÄ½ö½öÖ»ÊÇ´òËãÄܾÍÕâ¸ö»°ÌâºÍ´ó¼ÒÌÖÂÛÌÖÂÛ£¬Å×שÒýÓñ¡¢Ôö³¤¸öÈ˵ļûʶ¡£
Ê×ÏÈÏë˵Ã÷һϣ¬¸öÈËÊÇC++Éè¼Æ¡¢±à³Ì·ç¸ñµÄ¼á¶¨Óµ»¤Õߣ¬ËùÒÔÎҵĹ۵ãÄÑÃâ´øÓиöÈ˵ÄÆ«ºÃ£¬Èç¹ûÄú²»ÔÞÍ ......
×Ö·û´®×ª»»Æª
atof
atoi
atol
gcvt
strtod
strtol
strtoul
toascii
tolower
toupper
atof£¨½«×Ö·û´®×ª»»³É¸¡µãÐÍÊý£©
Ïà¹Øº¯Êý
atoi£¬atol£¬strtod£¬strtol£¬strtoul
±íÍ·Îļþ
#include <stdlib.h>
¶¨Ò庯Êý
double atof(const char *nptr);
º¯Êý˵Ã÷
atof()»áɨÃè²ÎÊýnptr×Ö·û´®£¬Ìø¹ýÇ°Ãæµ ......
1¡¢´íÎó(err_return)µÄºê¶¨Òå
#define err_return(num,fmt,args) \
do
{
printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);
} while(0)
-1 ÊÇreturnµÄ·µ»ØÖµ£¬±íʾÓдíÎó£»
fmt Ê ......
ÎÒ֮ǰÊÇÒ»¸öC³ÌÐòÔ±£¬¶øÇÒÊǸöWindowsµÄ³ÌÐòÔ±£¬ÔÚwindowsÏÂʹÓÃVC6.0/VS2005µÈmicrosoftµÄɵ¹Ïʽ¹¤¾ß¹¤×÷£¬
¶ÔÓÚÄǸövc 6.0/vs2005µÄ¿ì½Ý²Ù×÷£¬debug²Ù×÷ÊÇÄÇôµÄÊìϤ£¬¿ÉÒÔ˵vc
6.0/vs2005ÊÇwindowÏ¿ª·¢c/c++×îºÃÓõŤ¾ßÁË£¬¿ÉÒԲ鿴µ÷ÓöÑÕ»£¬ÄÚ´æ±ä»¯Çé¿ö£¬±äÁ¿Öµ£¬ÁíÍâ°²×° visual assist
xºóÈÃVC¿´ÆðÀ´ÊÇÄÇô ......
C×Ö·û´®´¦Àíº¯ÊýµÄʵÏÖ£¨Linux£©
#include <stddef.h>
char * ___strtok = NULL;
char * strcpy(char * dest,const char *src)
{
char *tmp = dest;
while ((*dest++ = *src++) != '\0')
/* nothing */;
return tmp;
}
char * strncpy(char * des ......