Share via

Parameter & * diff

Noah L 1 Reputation point
2021-09-11T08:52:43.827+00:00

131170-%E1%84%83%E1%85%AE%E1%86%AF%E1%84%8B%E1%85%B4-%E1%84%8E%E1%85%A1%E1%84%8B%E1%85%B5.png

void f1(int& a) {
a = 1;
}

void f2(int* a) {
*a = 2;
}

void main() {
int x = 0;
f1(x);
std::cout << x << std::endl;

f2(&x);  
std::cout << x << std::endl;  

}

f1, f2가 결국 main함수의 x 데이터를 바꾸는 같은 동작을 하는데,

실제 둘의 차이는 뭘까요?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


1 answer

Sort by: Most helpful
  1. David Lowndes 4,731 Reputation points
    2021-09-11T09:04:18.853+00:00

    I think you're looking to understand the difference between a reference and a pointer.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.