18. 9. 2013

Programování C/C++. Parameter funkce odkazem

Vsadím se, že jste to zaisté zkoušeli, předát parameter funkce pomocí její reference v prostředí pro ANSI C případně C99 a hodilo Vám to hlášku (třeba v překladači Mingw):

... error: expected ';', ',' or ')' before '&' token ...

,protože jste se snažili zkompilovat následující ukázku programu:

void test(u8 &a) {
 a++;
}

Důvod, proč to nejde zkompilovat, je ten že předávání parametru funkce pomocí reference, jde pouze u jazyku C++ a ne u jazyka C a ani u jazyka objective-C.

A když už jsme u reference, tak se podívejte na následující ukázku:

int main(void) {
 u8 b=1;
 test(b);
 cout << "b=" << b << endl;
}

Po spuštění programu bude hodnota b=2. A to je z toho důvodu že se vezme reference lokální proměnné b. Ta se předá jako parameter do funkce test, tam se inkrementuje hodnota na kterou reference ukazuje a tím se změní i hodnota proměnné b protože reprezentuje to samé pamětové místo.

Zdroj: Passing arguments by reference
S pozdravem
ViaExplore - Tomáš Kamenický

24 komentářů:

SEO CANADA řekl(a)...

There are several dissertation web sites on the net once you locate unsurprisingly explained in the website 먹튀

SeoMaster789 řekl(a)...

Keep up the good work; I read few posts on this website, including I consider that your blog is fascinating and has sets of the fantastic piece of information. Thanks for your valuable efforts 안전놀이터

Unknown řekl(a)...

Hmm… I interpret blogs on a analogous issue, however i never visited your blog. I added it to populars also i’ll be your faithful primer 메이저놀이터

office laptop002 řekl(a)...

This is really intriguing, You’re an especially efficient writer. I have signed up with your feed additionally look ahead to finding your personal interesting write-ups. Furthermore, I’ve got shared the blog inside our social networks. 먹튀검증

Robert řekl(a)...

Because of this it will be significantly better which you could suitable studies just before manufacturing. You'll be able to create significantly greater guide that way. 먹튀폴리스

office laptop řekl(a)...

I am really very happy to visit your blog. Now I am found which I actually want. Thanks a lot for sharing a piece of wonderful information which I am looking for a longer period of time 안전놀이터

Linda řekl(a)...

Congratulations for posting such a useful blog. Your blog isn’t only informative but also extremely artistic to 토토사이트

bmb řekl(a)...

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it 먹튀검증커뮤니티

Robert řekl(a)...

I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. The sims : Discover university is now available worldwide on PC! Your dorm room is ready for you, dom't be late for your first day of class 토토사이트

Unknown řekl(a)...

When do you think this Real Estate market will go back up? Or is it still too early to tell? We are seeing a lot of housing foreclosures in Longwood Florida. What about you? We would love to get your feedback on this. 먹튀검증

SeoMaster789 řekl(a)...

This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post. 먹튀검증

dgdfgerg řekl(a)...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good wor 토토사이트

슈어맨 řekl(a)...

 I got so interested in this material that I couldn’t stop reading. Your blog is really impressive 슈어맨

Robert řekl(a)...

I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up 먹튀검증사이트

Muna řekl(a)...

Thanks for this wonderful post! It has long been very helpful. I wish that you’ll carry on posting your wisdom with us. courtier immobilier Candiac

Henry Jacob řekl(a)...

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts. 먹튀검증사이트

xohefa řekl(a)...

토토사이트 TEN BET
토토사이트 TEN BET은 검증 된 토토사이트로 다양한 이벤트를 통해 현재 신규회원에게 첫 충전 30%와 매 충전 10%를 지급하고 있습니다 메이저사이트
.

Hibbah řekl(a)...

Dankwoods comes in part from the popular Backwoods Smokes brand of cigarillos. These cigarillos are rolled with a Connecticut Broadleaf tobacco leaf, which must be of remarkable quality. dankwoods

Hibbah řekl(a)...

led grow light produced by Spider Farmer integrated with Samsung Diodes that offers full spectrum and wavelength for optimum growth of plants and vegetables in your grow tent or indoor garden. led grow light

Hibbah řekl(a)...

Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. iPhone screen not responding to touch

Hibbah řekl(a)...

very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! Readabiography

Hibbah řekl(a)...

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! Raja Ampat Holidays

Hibbah řekl(a)...

I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article. Online Casino in Singapore

I will fix HTML, CSS, js, PHP, bootstrap bugs on your website within 12 hours řekl(a)...

Good day very cool website!! Man .. Excellent .. Superb .. I’ll bookmark your website and take the feeds additionally. I am glad to find a lot of useful information right here in the put up, we want to develop more strategies in this regard, thanks for sharing.