¹ØÓÚC¡¢C++ÖеÄconstºÍÖ¸Õë
żȻµÄÒòËØ£¬¶ÔconstÀ´Á˵ãÐËȤ£¬×öÁËÒ»ÏÂʵÑ飺
1. Óï·¨£ºconst¡¢Ö¸Õë
ÔÚgccÖУ¬ ¶ÔÓÚCÓïÑÔ£¬ÏÂÃæµÄÓï¾äÊǺϷ¨µÄ£º
const int a=0;
int *p=&a;
¶øC++ÖУ¬ÐèÒªÊÖ¹¤Ç¿ÖÆ×ª»»²ÅÄܱàÒëͨ¹ý£º
const int a=0£»
int *p=£¨int *£©&a£»
2. ¼ÈÈ»Äܹ»½«const±äÁ¿µÄµØÖ·¸³¸øÖ¸Ïò·Çconst±äÁ¿µÄÖ¸Õ룬ÄÇôÄÜ·ñ¸Ä±äͨ¹ýÖ¸Õë¸Ä±äconst±äÁ¿µÄֵĨ£¿
1)ÔÚgccÖУ¬±àÒëÖ´ÐÐÒÔÏ´úÂ룺
const int c=2;
int *p=&c;
*p=1;
printf("c is :%d\n",c);
±àÒëʱ»áÓо¯¸æ£º¾¯¸æ£º³õʼ»¯¶ªÆúÁËÖ¸ÕëÄ¿±êÀàÐ͵ÄÏÞ¶¨
½á¹ûΪ£º
c is 1
˵Ã÷ËäÈ»Óо¯¸æ£¬µ«ÊÇ˳Àû±àÒëͨ¹ý£¬²¢Çҳɹ¦¸Ä±äÁ˳£Á¿cµÄÖµ£¨´Ó2±äΪ1£©¡£
2.½ÓÏÂÀ´¿´ÏÂÃæÒ»¶Î´úÂ룺
#include <stdio.h>
#include <unistd.h>
int main(void)
{
const int c=2;
int a=0;
const int *p;
p=&a;
int *p2=&a;
p2[1]=-1;
printf("p2[1] is :%d\n",p2[1]);
printf("a is :%d\n",a);
printf("c is :%d\n",c);
printf("c address is:%d\n",&c);
printf("p2[1] address is:%d\n",p2+1);
printf("*(&c) is :%d\n",*(&c));
}
Õâ¶Î´úÂ룬³ÌÐò»áÓÐÔõÑùµÄÊä³öÄØ£¿
1.±£´æÎªºó׺Ϊ.cµÄÎļþ£¬ÓÃgcc±àÒ룬ִÐнá¹ûÈçÏ£º
p2[1] is :-1
a is :0
c is :-1
c address is:-1076230280
p2[1] address is:-1076230280
*(&c) is :-1
˵Ã÷p2+1µÄµØÖ·ºÍcµÄµØÖ·Ò»Ñù£¬³ÌÐò³É¹¦µÄ¼ä½Ó¸Ä±äÁËcµÄÖµ£¨p2Ô½½ç·ÃÎ
Ïà¹ØÎĵµ£º
@echo --------------------- ÓÃÓÚ¡¶Ò»¸ö²Ù×÷ϵͳµÄʵÏÖ¡·µÄ cmd ---------------------
@echo off
mode con cols=80 lines=60 ::ǰһ¸öÊý×Ö»ò×ÖĸΪ±³¾°É«£¬ºóÒ»¸öÊý×Ö»òÕß×ÖĸΪǰ¾°É«£¬ÏÂÃæÊÇһЩÑÕÉ«¶ÔÓ¦µÄÊý×Ö»ò×Öĸ£º
::0£½ºÚ¡¢1£½À¶¡¢2£½ÂÌ¡¢3£½Ç³ÂÌ¡¢4£½ºì¡¢5£½×Ï¡¢6£½»Æ¡¢7=°×¡¢8=»Ò¡¢9=µÀ¶¡¢A£½µ ......
1¡¢×÷ÓÃ
ΪÁËÔÚʵÏÖ¶à̬µÄʱºò²»Ôì³ÉÄÚ´æÐ¹Â¶,
Èç¹û»ùÀàÎö¹¹º¯Êýǰ²»¼Óvitual£¬ÅÉÉúÀà¶ÔÏó±»Ïú»Ùºó£¬Ö»»áµ÷ÓûùÀàµÄÎö¹¹º¯Êý£¬¶ø²»»áÈ¥µ÷ÓÃÅÉÉúÀàµÄÎö¹¹º¯Êý¡£
2¡¢¶ÔÓÚÕý³£µÄº¯Êý£¬Èç¹û»ùÀàÖÐÉùÃ÷Ϊvirtual£¬ÔòÅÉÉúÀà¿ÉÒÔ²»ÓÃÔÙдvirtual
// CPPTest.cpp : Defines the entry point for the console application.
// ......
[This was posted to comp.lang.c by its author, David Anderson, on 1994-05-06.]
The ``Clockwise/Spiral Rule''
By David Anderson
There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration!
There are three simple steps to fo ......
ÏÂÃæÓõ½µÄ·½·¨½ÐշתÏà³ý·¨£¬¾ß×ö²½ÖèÈçÏÂ
ÏÈÓÃСµÄÒ»¸öÊý³ý´óµÄÒ»¸öÊý£¬µÃµÚÒ»¸öÓàÊý£»
ÔÙÓõÚÒ»¸öÓàÊý³ýСµÄÒ»¸öÊý£¬µÃµÚ¶þ¸öÓàÊý£»
ÓÖÓõڶþ¸öÓàÊý³ýµÚÒ»¸öÓàÊý£¬µÃµÚÈý¸öÓàÊý£» &nb ......