http://blog.csdn.net/cogbee/article/details/37559205
2014-07-08
很久很久沒有寫C程序了,C++已經(jīng)深入骨髓。寫起C來居然還是非常吃力。
比如在C中,數(shù)組和字符串是不能互相賦值的。這可難為了。輸出一個(gè)數(shù)組還要一個(gè)一個(gè)輸出,
下面是一個(gè)下例子,就是將一個(gè)字符串反轉(zhuǎn)。
- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <stdlib.h>
-
- #define MAXLINE 1024
- static char revbuf[MAXLINE];
-
- void *reverse(char * buf,int len)
- {
- int i,n;
-
- if(len > MAXLINE || len < 0)
- {
- printf("length is error\n");
-
- }
- i = 0;
- n = len;
- while(i < len)
- {
- revbuf[i++] = *(buf+(--n));
- }
- revbuf[i] = '\n';
-
- }
-
- int main()
- {
- char *buf = "OK,I am test";
- reverse(buf,strlen(buf));
- fwrite(revbuf,1,strlen(buf)+1,stdout);
- }
在不能數(shù)組跟字符串相互賦值,字符串不能當(dāng)成數(shù)組用的時(shí)候,只能指針一個(gè)一個(gè)指定了。
為什么C中字符串不能當(dāng)數(shù)組使用呢?
在另外一個(gè)地方似乎找到了答案:
In C arrays are non-modifiable lvalues, you can't change what they point to since they don't point anywhere in the first place.
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。