programing

C에서 "참조" 및 "회의"의 의미

prostudy 2022. 5. 13. 23:49
반응형

C에서 "참조" 및 "회의"의 의미

나는 인터넷에서 다른 것들을 읽었고, 혼란스러웠다. 왜냐하면 모든 웹사이트는 다른 것들을 말하기 때문이다.

나는 에 대해 읽었다.*참조 연산자 및&참조란 변수를 가리키는 포인터를 만들고 포인터가 가리키는 변수의 값에 액세스하는 것을 의미한다.그래서 나는 혼란스러웠다.

"참조 및 비참조"에 대한 간단하지만 철저한 설명을 들을 수 있는가?

참조란 포인터 변수를 설정하기 위해 기존 변수의 주소를 가져오는 것을 의미한다.유효성을 위해 포인터는 별표가 없는 포인터와 같은 유형의 변수의 주소로 설정되어야 한다.

int  c1;
int* p1;
c1 = 5;
p1 = &c1;
//p1 references c1

포인터 비참조는 * 연산자(아스터스크 문자)를 사용하여 포인터가 가리키는 메모리 주소에서 값을 검색하는 것을 의미한다. 참고:포인터의 주소에 저장된 값은 포인터가 "포인트"할 변수의 유형과 같은 값이어야 하지만 포인터가 올바르게 설정되지 않은 한 이 경우라는 보장은 없다.포인터가 가리키는 변수의 유형은 가장 바깥쪽 별표를 뺀 유형이다.

int n1;
n1 = *p1;

잘못된 비참조 때문에 충돌이 발생하거나 발생하지 않을 수 있음:

  • 초기화되지 않은 포인터를 취소하면 충돌이 발생할 수 있음
  • 잘못된 유형의 캐스팅을 사용한 비참조는 충돌을 유발할 수 있다.
  • 동적으로 할당되고 이후에 할당 해제된 변수에 대한 포인터를 폐기하면 충돌이 발생할 수 있음
  • 이후 범위를 벗어난 변수에 대한 포인터를 폐기하는 것도 충돌을 일으킬 수 있다.

잘못된 참조는 충돌보다 컴파일러 오류를 일으킬 가능성이 더 높지만, 이를 위해 컴파일러에 의존하는 것은 좋은 생각이 아니다.

참조:

http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators

& is the reference operator and can be read as “address of”.
* is the dereference operator and can be read as “value pointed by”.

http://www.cplusplus.com/doc/tutorial/pointers/

& is the reference operator    
* is the dereference operator

http://en.wikipedia.org/wiki/Dereference_operator

The dereference operator * is also called the indirection operator.

나는 항상 그들이 반대 의미로 사용되었다고 들었다.

  • &참조 연산자 - 어떤 객체에 대한 참조(초기)를 제공한다.

  • *참조(reference) 연산자 - 참조(reference)를 사용하고 참조된 객체를 다시 제공함.

*가 있는 문맥은 때때로 의미를 혼동한다.

  // when declaring a function
int function(int*); // This function is being declared as a function that takes in an 'address' that holds a number (so int*), it's asking for a 'reference', interchangeably called 'address'. When I 'call'(use) this function later, I better give it a variable-address! So instead of var, or q, or w, or p, I give it the address of var so &var, or &q, or &w, or &p.   

//even though the symbol ' * ' is typically used to mean 'dereferenced variable'(meaning: to use the value at the address of a variable)--despite it's common use, in this case, the symbol means a 'reference', again, in THIS context. (context here being the declaration of a 'prototype'.) 


    //when calling a function
int main(){ 
    function(&var);  // we are giving the function a 'reference', we are giving it an 'address'
  }

그래서, int나 char와 같은 유형을 선언하는 맥락에서, 우리는 참조(주소)를 실제로 의미하기 위해 참조자 ' *'를 사용하게 되는데, 만약 당신이 주소를 요구하는 컴파일러로부터 '예상 char*'라는 오류 메시지를 보게 되면 혼란스러워진다.

이 경우 *가 유형(int, char 등)을 쫓을 때 컴파일러는 변수의 주소를 예상한다.우리는 이것을 변수 앞에 '&' 연산자 주소라고 불리는 참조 연산자를 사용하여 제공한다.더욱이 방금 위와 같이 꾸며낸 경우, 컴파일러는 주소가 숫자가 아닌 문자 값을 가질 것으로 기대하고 있다.(타입 문자 * == 문자가 있는 값의 주소)

int* p;
int *a;   // both are 'pointer' declarations. We are telling the compiler that we will soon give these variables an address (with &).

int c = 10;  //declare and initialize a random variable
//assign the variable to a pointer, we do this so that we can modify the value of c from a different function regardless of the scope of that function (elaboration in a second)

p = c; //ERROR, we assigned a 'value' to this 'pointer'. We need to assign an 'address', a 'reference'.
p = &c; // instead of a value such as: 'q',5,'t', or 2.1 we gave the pointer an 'address', which we could actually print with printf(), and would be something like
//so
p = 0xab33d111; //the address of c, (not specifically this value for the address, it'll look like this though, with the 0x in the beggining, the computer treats these different from regular numbers)
*p = 10; // the value of c

a = &c; // I can still give c another pointer, even though it already has the pointer variable "p"

*a = 10;
 a = 0xab33d111;

각 변수를 위치(또는 어레이에 익숙한 경우 인덱스 값)와 값을 갖는다고 생각하십시오.컴퓨터 안에 전기를 저장해 두 개의 값, 즉 한 개의 값, 그리고 프로그래머가 저장하고자 하는 수량이나 문자를 나타내는 값을 각 변수에 대해 생각하려면 어느 정도 익숙해져야 할지도 모른다.

//Why it's used
int function(b){
    b = b + 1; // we just want to add one to any variable that this function operates on.
} 

int main(){

    int c = 1;  // I want this variable to be 3.

    function(c); 
    function(c);// I call the function I made above twice, because I want c to be 3.

     // this will return c as 1. Even though I called it twice.
     // when you call a function it makes a copy of the variable.
     // so the function that I call "function", made a copy of c, and that function is only changing the "copy" of c, so it doesn't affect the original
}
  //let's redo this whole thing, and use pointers

int function(int* b){ // this time, the function is 'asking' (won't run without) for a variable that 'points' to a number-value (int). So it wants an integer pointer--an address that holds a number.
*b = *b + 1; //grab the value of the address, and add one to the value stored at that address
}

int main(){
    int c = 1; //again, I want this to be three at the end of the program
    int *p = &c; // on the left, I'm declaring a pointer, I'm telling the compiler that I'm about to have this letter point to an certain spot in my computer. Immediately after I used the assignment operator (the ' = ') to assign the address of c to this variable (pointer in this case) p. I do this using the address-of operator (referencer)' & '.
    function(p); // not *p, because that will dereference. which would give an integer, not an integer pointer ( function wants a reference to an int called int*, we aren't going to use *p because that will give the function an int instead of an address that stores an int.

    function(&c); // this is giving the same thing as above, p = the address of c, so we can pass the 'pointer' or we can pass the 'address' that the pointer(variable) is 'pointing','referencing' to. Which is &c. 0xaabbcc1122...


      //now, the function is making a copy of c's address, but it doesn't matter if it's a copy or not, because it's going to point the computer to the exact same spot (hence, The Address), and it will be changed for main's version of c as well.

}

각 블록 안에서는 ("s"s"s 내의 파라미터를 통해) 전달되는 변수(있는 경우)를 복사한다.이러한 블록 내에서 변수에 대한 변경은 해당 변수의 복사본에 적용되며, 변수는 동일한 문자를 사용하지만 (원본과는) 다른 주소에 있다.원본의 "참조" 주소를 사용함으로써, 우리는 메인 바깥의 블록이나 메인 아이 내부의 블록을 사용하여 변수를 변경할 수 있다.

우선, 당신은 그것들을 거꾸로 가지고 있다.&참조 및*부결이다.

변수를 참조하는 것은 변수의 메모리 주소에 액세스하는 것을 의미한다.

int i = 5;
int * p;
p = &i; //&i returns the memory address of the variable i.

변수 비참조는 메모리 주소에 저장된 변수에 액세스하는 것을 의미한다.

int i = 5;
int * p;
p = &i;
*p = 7; //*p returns the variable stored at the memory address stored in p, which is i.
//i is now 7

아래의 설명을 찾아내다.

int main()
{
    int a = 10;// say address of 'a' is 2000;
    int *p = &a; //it means 'p' is pointing[referencing] to 'a'. i.e p->2000
    int c = *p; //*p means dereferncing. it will give the content of the address pointed by 'p'. in this case 'p' is pointing to 2000[address of 'a' variable], content of 2000 is 10. so *p will give 10. 
}

결론 :

  1. &참조에는 [주소 연산자]가 사용된다.
  2. *[별표 연산자]는 분리를 위해 사용된다.

참조

&참조 연산자.그것은 메모리 주소를 포인터 변수와 연관시킬 것이다.

예:

int *p;
int a=5;
p=&a; // Here Pointer variable p refers to the address of integer variable a.

비참조

참조 해제 연산자*포인터 변수가 메모리 주소 대신 변수의 값에 직접 액세스하는 데 사용된다.

예:

int *p;
int a=5;
p=&a;
int value=*p; // Value variable will get the value of variable a that pointer variable p pointing to.

참조 해제 포인터 참조는 또한 점 변수의 주소와 동일하다.

설명 :-

int var = 3; int *p;

p = &var;

그래서, var의 주소는 ABCDE라고 생각해보자.

그때

p = ABCDE 및 &*p = ABCDE;

즉, 참조와 정보 제거를 중립화한다는 뜻이다.


또한 함수를 선언할 때,

함수의 주장은 포인터가 되어야 한다.

그리고 주요 방법으로 호출할 때 이 함수의 주장에는 & 연산자와 함께 있어야 한다.

좀 헷갈리네그러나 int *p = &var;도 위의 포인터 선언과 같이 정확하다는 것을 기억하십시오.

참조URL: https://stackoverflow.com/questions/14224831/meaning-of-referencing-and-dereferencing-in-c

반응형