½âÎöCÓëC++ÖеĹؼü×Öconst
ÔÚCÓëC++ÓïÑÔÖж¼´æÔڹؼü×Öconst,ºÜ¶àÈ˶¼¶Ô´Ë¹Ø¼ü×Ö´æÔÚÒ»¸ö´íÎóµÄÈÏʶ£¬ÈÏΪÔÚCÓïÑÔÖйؼü×ÖconstÊÇʹ±äÁ¿×÷Ϊһ¸ö³£Á¿£¬¼´½«±äÁ¿³£Á¿»¯£¬¾ÍÏñºê¶¨ÒåÒ»Ñù¡£¶øÔÚCÓïÑÔÖеĹؼü×ÖconstËùÆðµÄ×÷Óò¢²»ÊÇʹ±äÁ¿³£Á¿»°£¬¶øÊÇÏÞÖÆ±äÁ¿£¬Ê¹±äÁ¿³ýÁ˱»¸³³õÖµÍ⣬ÎÞ·¨±»ÖØÐ¸³Öµ¡£
¶øÔÚC++Öйؼü×Öconst²»½öʹ¸Ã±äÁ¿ÎÞ·¨Ð޸ģ¬Ò²ÊÇʹ±äÁ¿³£Á¿»¯£¬¼´½«±äÁ¿¸³³õÖµºó¿ÉÒÔµ±×÷³£Á¿Ê¹Óã¬Ï൱ÓÚ½øÐÐÁ˺궨Òå¡£
ÔÚ±àÒëÆ÷ÖÐÊäÈëÒÔÏ´úÂ룬Äã»áÓиüÖ±¹ÛµÄÌå»á¡£
ÔÚCÓïÑÔ±àÒëÆ÷ÖУº
/* constÏÞÖÆµÄ±äÁ¿µÄÖµÎÞ·¨ÐÞ¸Ä*/
#include <stdio.h>
int main(void)
{
const int a=1;
int const b=1;
a=1;
b=1;
system("pause");
return 0;
}
µ±Ä㽫Õâ¶Î´úÂë±àÒëʱ£¬±àÒëÆ÷»á±¨´í£ºÔÚmianº¯ÊýÖÐÎÞ·¨ÐÞ¸ÄconstÏÞÖÆµÄ¶ÔÏó¡£
×¢£ºÔÚCÓïÑÔÖУ¬¹Ø¼ü×Öconst±»·ÅÔÚ±êʶ·û֮ǰ»òÖ®ºóÊÇÒ»ÑùµÄЧ¹û¡£
/* constÏÞÖÆµÄ±äÁ¿ÎÞ·¨ÐÞ¸Ä*/
#include <stdio.h>
int main(void)
{
const int a=1;
switch(1)
{
case a:prinf("variable a can be used as constant");break;
default :printf("variable a cannot be used as constant");
}
system("pause");
return 0;
}
µ±Äã±àÒëÕâ¶Î´úÂëÊÇ£¬±àÒëÆ÷»á±¨´í£ºÔÚmainº¯ÊýÖÐÒªÇóʹÓó£Á¿¡£
¹Ø¼ü×Öconst¶ÔÖ¸ÕëµÄÏÞÖÆ£º
/*cosntÔÚ*ǰ£¬Ö¸ÕëÖ¸Ïò¶ÔÏóµÄÖµÎÞ·¨ÐÞ¸Ä*/
#include <stdio.h>
int main(void)
{
int a=1,b=2;
const int * p1=&a;
int const * p2=&b;
*p1=1;
*p2=1;
system("pause");
return 0;
}
constÔÚÐǺÅǰ¶Ô¶ÔÖ¸Õë±äÁ¿½øÐÐÏÞÖÆÊ±ÊǶÔÖ¸Õë±äÁ¿ËùÖ¸ÏòµÄ¶ÔÏó½øÐÐÏÞÖÆ£¬Õë¶ÔÉÏÁдúÂë¾ÍÊǶÔa£¬b½øÐÐÏÞÖÆ£¬Ê¹a£¬b²»Äܱ»ÖØÐ¸³Öµ£¬Òò´Ë±àÒëʱ»á±¨´í£ºÔÚmianº¯ÊýÖÐÎÞ·¨ÐÞ¸ÄconstÏÞÖÆµÄ¶ÔÏó¡£µ«Á½¸öÖ¸Õë±äÁ¿µÄÖµÊÇ¿ÉÒÔÐ޸ĵġ£±àÒëÏÂÁдúÂ룺
/* cosntÔÚ*ǰ£¬Ö¸Õë±äÁ¿µÄÖµ¿ÉÒÔÐÞ¸Ä*/
#include <stdio.h>
int main(void)
{
int a=1,b=2;
const int * p1=&a;
int const * p2=&b;
printf ("a=%d\nb=%d\n",*p1,*p2);
p1=&b;
if(p1==p2)
printf("the address of p1 is equal to the address of p2\n");
system("pause");
re
Ïà¹ØÎĵµ£º
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
main()
{
FILE *fp;
if ( (fp = fopen( "c:\\a.txt", "r" )) == NULL ) printf("ERROR!\n");
int tmp[MAXSIZE];
int i;
for ( i=0; i<MAXSIZE; i++ )
{
tmp[i] = 0; ......
Ò»¡¢ÓÐÈçÏ´úÂ룺
int age = 25;
const int *pAge = &age;
ÉÏÃæµÄ´úÂë±íʾ£º
1¡¢Ö¸Õë±äÁ¿pAge´æ·Å±äÁ¿ageµÄµØÖ·£¬ÇÒ²»ÄÜͨ¹ý *pAge = 30£¬À´¸Ä±äÖ¸Õë±äÁ¿pAgeËùÖ¸ÏòµÄ´æ´¢¿Õ¼äµÄÖµ£¬µ«ÊÇ ¶ÔÓÚ age = 30£¬ÔòÊÇûÓÐÎÊÌâµÄ¡£
2¡¢pAge ±¾Éí¿ÉÒÔÔÙ´æ·ÅÆäËü±äÁ¿µÄµØÖ·£¬Ò²¿ÉÒÔÖ¸ÏòNULL£¬Èç pAge = NULL;ÊÇÕýÈ·µÄ¡£
¶ ......
<!--
/* Font Definitions */
@font-face
{font-family:ËÎÌå;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@ËÎÌå" ......
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#include<math.h>
typedef struct worker
{
int num; //񅧏
char name[15]; //ÐÕÃû
char zhicheng[15];& ......
C²âÊÔС³ÌÐò
1¡¢ ×Ö·û´®Àà
1.1 strstr
¹¦ÄÜ£º²éÕҺͻñÈ¡×Ó´®
void test_strstr()
{
char *str="Borland Inte ......