顯示具有 pointer 標籤的文章。 顯示所有文章
顯示具有 pointer 標籤的文章。 顯示所有文章

2011年10月3日 星期一

Constant Pointer


[18.5] What's the difference between "Fred const* p", "Fred* const p" and "Fred const* const p"?

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.5

You have to read pointer declarations right-to-left.

  • Fred const* p means "p points to a constant Fred": the Fred object can't be changed via p.
  • Fred* const p means "p is a const pointer to a Fred": you can't change the pointer p, but you can change the Fred object via p.
  • Fred const* const p means "p is a constant pointer to a constant Fred": you can't change the pointer p itself, nor can you change the Fred object via p.

const X& x and X const& x are equivalent.
const X* x and X const* x are equivalent.

TypeX const ... 可寫成 const TypeX ....

2010年11月9日 星期二

陣列不等於指標


 作者  loveflames (咕啾咕啾魔法陣)                            看板  C_and_CPP
 標題  Re: [語法] 有關陣列與指標的問題
 時間  Tue Nov  9 17:39:06 2010
───────────────────────────────────────

※ 引述《pioneerLike (~小耿~)》之銘言:
: 不知道怎麼下標題好
: #include <stdio.h>
: #include <stdlib.h>
: #include <string.h>
: int main() {
:   char *a = new char[100];
:   char *b = (char *)malloc(100*sizeof(char));
:   char c[100] ;
:   printf("[%p] [%p]\n",a,&a) ;  // not same
  stack         heap
&a| a |       a|*a |
:   printf("[%p] [%p]\n",b,&b) ;  // not same
  stack         heap
&b| b |       b|*b |
:   printf("[%p] [%p]\n",c,&c) ;  // the same
&c是指標
指到第一個元素
c是陣列
只是這裡被compiler換成指標
:   system("PAUSE");
: }
: 小弟百思不得其解,查了很多資料依然不明白其中原理的差異,請板友解惑,謝謝=]
來,跟我說一次"陣列不等於指標"

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.252.74.195
→ meconin:其實有趣的應該是 (c = &c = *&c) != (*c = **&c)          11/09 18:05
→ meconin:推錯篇 ...                                              11/09 18:09
→ loveme00835:樓上... http://ppt.cc/Sa(,                          11/09 18:30
→ meconin:把 include 換回 c 的。http://codepad.org/McSEbBKk       11/09 18:42
→ loveme00835:傻眼...                                             11/09 19:06
→ loveflames:那我要推一下這個 http://codepad.org/QZw62kPG         11/09 19:07

2010年11月5日 星期五

strcpy除錯

作者: jasonkey123 (jasonkey123) 看板: C_and_CPP標題: [問題] strcpy除錯
時間: Thu Nov 27 22:01:34 2008

這是面試問到的問題,我覺得沒有任何錯誤.但卻寫說要更正錯誤@@
以下是code.
但我怎麼看都沒問題.
==========================
void test(char *p,int n)
{
p=(char*)malloc((char)sizeof(char)*n);
}
void main(void)

{
char *ptr;
test(ptr,100);
strcpy(ptr,"hello");
}

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.168.24.209
推 ianfang:malloc那邊 (char)sizeof 前面的(char)要去掉 11/27 22:04
→ jasonkey123:但我試過有沒有拿掉都ok 11/27 22:05
推 gozha:test(&ptr, 100); void test(char **p,int n) *p=.... 11/27 22:07
推 Biboy:我剛剛也看很久...樓上一說就懂了...看來我功力還不夠ˊˋ 11/27 22:12
→ gozha:我習慣是char* test(int n){return (char*)malloc(......);} 11/27 22:14
推 tsaiminghan:我連答案出來了,都要想半天才搞懂... 11/27 22:33
推 Clain66:3樓是正解 11/27 22:34
推 Biboy:可以問個問題嗎? sizeof前面的(char)是什麼作用? 11/27 22:35
推 gozha:那個無意義,但是當n也是char型態的時候會有問題 11/27 22:37
→ gozha:char就當作只是1byte的整數就好了 11/27 22:38
→ jasonkey123:終於知道了,3F,5F方法都可以 11/27 23:35
推 ledia:memory leak 11/28 01:09
→ ledia:access violation 11/28 01:10
推 OpenBigJue:其實沒有call by address,只有call by value/reference 11/28 03:09