C program array pointer 的問題

2009-05-16 8:10 am
如果我想將2個array(char 的array,用來存string)的pointer對掉,只對掉pointer,不對掉array中的值,可不可以?如可以,怎樣做?

回答 (1)

2009-05-16 2:08 pm
✔ 最佳答案
Yes, you can do that. Try the following code:
int main(int argc, char* argv[])
{
char *word1="First";
char *word2="Second";
char *p;
printf("%s, %s\n", word1, word2);
// Exchange
p=word1; word1=word2;word2=p;
printf("%s, %s\n", word1, word2);
system("pause");
return 0;
}
The output will look like this:
First, Second
Second, First


收錄日期: 2021-04-13 16:36:57
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090516000051KK00031

檢視 Wayback Machine 備份