简体   繁体   中英

Setting an array to a pointer value

I'm not a C++ developer but found my self doing this. What does r in the following code example represents, and what is the c# code equivalent?

#include <iostream>

using namespace std;

int main()
{
    cout<<"Value:\n";

    uint32_t k[62];
    uint32_t* r = k;
    *r++;

    cout<<*r;
    
    return 0;
}

uint32_t* r = k; makes r point to the first element of the uninitialized array k .

*r++; increments r to point to the second element of k , and then dereferences the value of the second element (which is undefined). The deference is a no-op because the result is not used, so the statement does the same as just r++; . (*r)++; would increment the array element.

cout<<*r; then prints the value of the second element of k , which causes undefined behavior because the value is not defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM