Pointers and references practice (2019)

栏目: IT技术 · 发布时间: 6年前

内容简介:This page contains set of challenges that you can go through to test your understanding on the topic. It aditionally contains links to resources and or explanations on the topic.You can find its source on

pointers and references practice

This page contains set of challenges that you can go through to test your understanding on the topic. It aditionally contains links to resources and or explanations on the topic.

This page was last updated on 10.11.2019

You can find its source on github.

  1. Pointer is a variable that can contain a memory address.

    Recommended reading: The C Programming Language", 2nd edition, Kernighan and Ritchie; Chapter 5

    Useful links: cppreference.com , cplusplus.com , Stanford 106 , random pointer tutorial , and more pointers .

    [+] Show example code

    A reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C.

    Useful links: cppreference.com , isocpp.org .

    [+] Show example code

  2. 1. Create pointer 'pNum' that points to int 'num'. Set value of num to 10 by deferencing pNum pointer. Initialize new varibale 'num2' by directly acessing 'num' throught pNum pointer. Output num2.

    [+] Show solution

    2. How would you output memory address of num from previous example? How would you output memory address of num by using pointer? How would you ouput memory address of the pointer itself?

    [+] Show solution

    3. What does following code output?

    void coutA(int*a){
        cout << &a << " | " << a << " | " << *a << " | in pointer func" << endl;
    }
        
    void coutA(int&a){
        cout << &a << " | " << a << "\t\t | in reference func" << endl;
    }
                        
    int main() {
        int a = 42;
        int *pA = &a;
        int **pA2 = &pA;
    
        coutA(a);
        coutA(&a);
        coutA(pA);
        coutA(*pA);
        coutA(*pA2);
        coutA(**pA2);
    
        cout << endl;
        cout << &a << endl;
        cout << &pA << endl;
        cout << &pA2 << endl;

    [+] Show solution

    3. Initialize int and float variables 'foo' and 'bar' both to 0. Declare a new void pointer and use it to change values of foo and bar to 3 and 3.4 respectively. Output values of foo and bar.

    [+] Show solution

    4. Define an array of first 5 natural numbers. Output whole array by deferencing a pointer and change 3 to 4 before outputing it.

    [+] Show solution

    5. What does following code output?

    int a = 1;
    int b = 2;
    int c = 3;
    int arr[] = {a,b,c};
    
    *(arr+2) = 7;
    
    for(int i = 0; i<sizeof(arr)/sizeof(*arr); ++i){
        std::cout << &arr[i] << " | " << arr[i] << std::endl;
        std::cout << arr + i << " | " << *(arr+i) << std::endl;
    }

    [+] Show solution

    6. Define new pancake using struct bellow and assign it values 10 and 7.68 using pointers. Output pancake info using alternative notation.

    struct Pancake {
        int i;
        float f;
    };

    [+] Show solution

© 2019 Tomislav (frainfreeze) Kucar.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

HTML5与CSS3基础教程(第8版)

HTML5与CSS3基础教程(第8版)

[美] Elizabeth Castro、[美] Bruce Hyslop / 望以文 / 人民邮电出版社 / 2014-5 / 69.00元

本书是风靡全球的HTML和CSS入门教程的最新版,至第6版累积销量已超过100万册,被翻译为十多种语言,长期雄踞亚马逊书店计算机图书排行榜榜首。 第8版秉承作者直观透彻、循序渐进、基础知识与案例实践紧密结合的讲授特色,采用独特的双栏图文并排方式,手把手指导读者从零开始轻松入门。相较第7版,全书2/3以上的内容进行了更新,全面反映了HTML5和CSS3的最新特色,细致阐述了响应式Web设计与移......一起来看看 《HTML5与CSS3基础教程(第8版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试