Pointer to an int. C ++

I need to go to a pointer to an int function. Now, if I want to pass 5, I do it like this:

int * i = NULL;
int b = 5;
i = &b;

Is there a better way to write it shorter?

I want to pass the bytes that are in i int for this function:

void Write2Asm(void* pxAddress, BYTE * MyBytes,  int size)
+3
source share
4 answers

You can simply pass & b functions; no intermediate pointer variable needed.

+7
source

Why create a pointer variable ?. Why can't you do it like this?

int b = 5;
func(&b)
+4
source
void f(int *i)
{
  //...
}

int b = 5;
f(&b);

!

+2

API- C, , .. - , - , :

#include <iostream>

struct X
{
    X(int n) : n_(n) { std::cout << "X()\n"; }
    ~X() { std::cout << "~X()\n"; }
    operator int&() { return n_; }
    operator const int() const { return n_; }
    int* operator&() { return &n_; }
    const int* operator&() const { return &n_; }
    int n_;
};

// for a function that modifies arguments like this you'd typically
// want to use the modified values afterwards, so wouldn't use
// temporaries in the caller, but just to prove this more difficult
// case is also possible and safe...
void f(int* p1, int* p2)
{
    std::cout << "> f(&" << *p1 << ", &" << *p2 << ")\n";
    *p1 += *p2;
    *p2 += *p1;
    std::cout << "< f() &" << *p1 << ", &" << *p2 << "\n";
}

int main()
{
    // usage...
    f(&X(5), &X(7));

    std::cout << "post\n";
}

, , f(...).

-1

All Articles