C??????const?????ü?????
?????Linux???? ???????[ 2017/3/22 10:52:09 ] ??????????????????? .NET
???????????????????????????????????????????????????????????ж????????????磬????????????????????????????????????????С?????????????????????? const ??????????????????
????const int MaxNum = 100 ; //?????????
???????? MaxNum ???????????????κζ? MaxNum ????????????????????
????MaxNum = 90 ; //????????? const ????д??????
????????????? const ?????????????Constant ??????????????????????
????const type name = value;
????const ?? type ???????????α???????????λ?????????????? type ???? const ??棺
????type const name = value;
????????????????????????????????????????????齫???????????????д????????????????????????
????????????????????????????????????????????????????????????????????????κθ????????????????????????????????????????????????????????????????
#include <stdio.h>
int getNum () {
return 100 ;
}
int main () {
int n = 90 ;
const int MaxNum1 = getNum (); //??????????
const int MaxNum2 = n ; //??????????
const int MaxNum3 = 80 ; //??????????
printf ( "%d?? %d?? %d
" ?? MaxNum1 ?? MaxNum2 ?? MaxNum3 );
return 0 ;
}
???????н????
????100?? 90?? 80
????const ?????
????const ???????????????????????????????????????????????????????????????const ?????????????м???????????????????
????const int *p1 ;
????int const *p2 ;
????int * const p3 ;
??????????????£??????????????? p3 ????????????????????????????£????????????????????????? p1??p2 ?????????????????????????????????????????????????
???????????????????????????п????????????????????д??????????????
????const int * const p4 ;
????int const * const p5 ;
????const ????????д???????е??ó??????????????????????????????const ???????????????????????????????????????????????????????????????????????У??????????????????????????????
????const ??????β?
??????C?????У????????? const ??????????????????????????? #define ??????檔const ???????????β??У?????β??????????????????????????????????????????? const ???????
??????C?????????У??к???????βζ??? const ?????????????????????????
size_t strlen ( const char * str );
int strcmp ( const char * str1 ?? const char * str2 );
char * strcat ( char * destination ?? const char * source );
char * strcpy ( char * destination ?? const char * source );
int system ( const char * command );
int puts ( const char * str );
int printf ( const char * format ?? ... );
???????????????庯??????????? const ???βμ??????????????????????????????????????
#include <stdio.h>
size_t strnchr ( const char *str ?? char ch ) {
int i ?? n = 0 ?? len = strlen (str );
for (i = 0 ; i <len ; i ++) {
if (str [i ] == ch ) {
n ++;
}
}
return n ;
}
int main () {
char *str = "http://c.biancheng.net" ;
char ch = 't' ;
int n = strnchr (str ?? ch );
printf ( "%d
" ?? n );
return 0 ;
}
???????н????
????3
???????? strnchr() ??????????????????????????? str ???б??????????????????????? const ????????????????????????????????????????????????????????????????????????????????????????????????
????const ??? const ???????
??????????????? str1 ?? const ??????????????? const char *str1 ???????????????????????????????????? str1 ????????????δ?? const ???ε??????? str2???п??????Σ????????? str1 ?????????????????????? str2 ????????????????巢????????????????????????????????????????檔
?????????? const char * ?? char * ???????????????? const char * ????????????? char * ???????????????????????????????????? char * ????????????? const char * ??????????
???????????????????? char * ?????????ж????д???????? const char * ??????????ж?????????????????????????κ????????????????????п??????Σ???
????C?????????к????????????? const ???????????????????????????в?????????????????????? const ????????????? const ??????βΣ?????????δ?????κθ??????????????潲??????? const ?????????? const ????????????
??????????????? const ?????????? const ??????????
#include <stdio.h>
void func ( char *str ) { }
int main () {
const char *str1 = "c.biancheng.net" ;
char *str2 = str1 ;
func (str1 );
return 0 ;
}
??????7??8?д????????????????Σ????ε????????????? const ??????????????? const ?????????????????????????????????????????棬???????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11