IT story

"참조"및 "비정의"의 의미

hot-time 2020. 12. 29. 07:53
반응형

"참조"및 "비정의"의 의미


나는 인터넷에서 다른 것을 읽고 혼란스러워했습니다. 모든 웹 사이트가 다른 것을 말하고 있기 때문입니다.

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.

나는 항상 그것들이 반대의 의미로 사용되는 것을 들었습니다.

  • & 참조 연산자입니다-어떤 객체에 대한 참조 (포인터)를 제공합니다.

  • * 역 참조 연산자입니다. 참조 (포인터)를 받아 참조 된 객체를 돌려줍니다.


우선, 당신은 그것들을 거꾸로 가지고 있습니다 : &참조이고 *역 참조입니다.

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

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. * [star operator]는 역 참조에 사용됩니다.

참조

&참조 연산자입니다. 메모리 주소를 포인터 변수로 참조합니다.

예:

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.

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

  // 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'
  }

So, in the context of declaring a type such as int or char, we would use the dereferencer ' * ' to actually mean the reference (the address), which makes it confusing if you see an error message from the compiler saying: 'expecting char*' which is asking for an address.

In that case, when the * is after a type (int, char, etc.) the compiler is expecting a variable's address. We give it this by using a reference operator, alos called the address-of operator ' & ' before a variable. Even further, in the case I just made up above, the compiler is expecting the address to hold a character value, not a number. (type char * == address of a value that has a character)

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;

Think of each variable as having a position (or an index value if you are familiar with arrays) and a value. It might take some getting used-to to think of each variable having two values to it, one value being it's position, physically stored with electricity in your computer, and a value representing whatever quantity or letter(s) the programmer wants to store.

//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.

}

Inside each and every block, it copies the variables (if any) that are passed into (via parameters within "()"s). Within those blocks, the changes to a variable are made to a copy of that variable, the variable uses the same letters but is at a different address (from the original). By using the address "reference" of the original, we can change a variable using a block outside of main, or inside a child of main.

ReferenceURL : https://stackoverflow.com/questions/14224831/meaning-of-referencing-and-dereferencing

반응형