简体   繁体   English

如何打印指针指向的值?

[英]How are the pointed-to values of pointers being printed?

I found the following code in my textbook: 我在教科书中找到以下代码:

#include<stdio.h>

void disp( int *k)
{
printf("%d",*k);
}

int main( )
{
 int i ;
 int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
 for ( i = 0 ; i <= 6 ; i++ )
 disp ( &marks[i] ) ;
 return 0;
}

 }

The code works just fine, but I have doubts regarding the logic: 该代码可以正常工作,但是我对逻辑有疑问:

  1. I am sending the address of variables of the array. 我正在发送数组变量的地址。 But in the disp function I am using a pointer variable as the argument and printing the value of the pointer. 但是在disp函数中,我使用了一个指针变量作为参数,并打印了指针的值。 So the type of argument sent from the main function should mismatch with the argument of disp . 因此,从main函数发送的参数类型应与disp的参数不匹配。 So how does it work? 那么它是怎样工作的?

  2. I tried to do the same by changing the disp function as 我试图通过将disp函数更改为

     void disp( int (&k)) { printf("%d",*k); } 

    but I am getting an error. 但我遇到一个错误。 What should I do to make it work by taking an address as an argument, ie void disp(int &k) ? 我应该怎么做才能使其通过以地址作为参数来工作,即void disp(int &k)

1) I am sending the address of variables of the array. 1)我正在发送数组变量的地址。 But in the disp function I am using a pointer variable as argument and printing the value of the pointer. 但是在disp函数中,我使用指针变量作为参数,并打印指针的值。

Understand that a pointer is an address. 了解指针是地址。 So &marks[i] is an int* . 因此&marks[i] 一个int* And you're not printing the value of the pointer, but the value it points to. 而且您不是在打印指针的值,而是它指向的值。

printf("%d",*k);

the *k dereferences the pointer and gives the pointed-to value. *k取消引用指针并给出指向的值。

void disp( int (&k))

is invalid syntax in C, &k is not a valid identifier. 是C中的无效语法, &k不是有效的标识符。

When you do: - 当您这样做:-

int *k = &marks[i];

The above statement is broken into: - 上面的语句分为:-

int *k;   -> Integer Pointer
k = &marks[i]; --> `k` points to the address of marks[i]

So, basically, k is the an integer pointer, that points to the address of the current element in your array . 因此,基本上, k是一个integer指针,它指向array当前元素的地址。

So, when you print *k , it is equivalent to: - *(&marks[i]) , which dereferences the value and prints the element marks[i] . 因此,当您打印*k ,它等效于:- *(&marks[i]) ,它取消引用值并打印元素marks[i]

So, in the below code, you can understand, how the whole process of pointer assignment and de-referencing takes place: - 因此,在下面的代码中,您可以了解pointer分配和de-referencing的整个过程是如何发生的:-

int *k;  // Declaring integer pointer.

// Actually 'k' is equal to the `&marks[i]`. 
// '*k' is just the indication of it being a pointer
k = &marks[i];      

// Dereference pointer
*k = *(&marks[i]); -->  = marks[i]

printf("%d",*k);  --> printf("%d",marks[i]);

Also, since you cannot declare variable like: - 另外,由于您不能像这样声明变量:-

int &k = &marks[i];

You cannot have them as parameter in your function: - 您不能将它们作为函数中的参数:-

void disp(int &k);

Because, eventually the address of array element passed is stored in this variable. 因为,最终传递的array element的地址存储在此变量中。 So, it has to be int *k . 因此,它必须是int *k

1) You understand correct that in the for loop the address is sent to disp . 1)您正确理解在for循环中地址已发送到disp But disp receives pointer as an argument... which is just an address. 但是disp接收指针作为参数...这只是一个地址。 Inside that function you don't print the "value" of the pointer. 在该函数内部,您不会打印指针的“值”。 You print the value pointed by that pointer (the value that is on that address). 您打印该指针指向的值(该地址上的值)。 In the printf call "*k" you dereference the pointer - get the value pointed by that pointer. 在printf调用“ * k”中,您取消引用指针-获取该指针指向的值。

2) You didn't change the function so that it receives address. 2)您没有更改功能以使其接收地址。 You changed it so that it receives reference. 您对其进行了更改,以便获得参考。 You can also say it receives a hidden pointer - it does the same as the pointer but you don't need to "*k" to dereference it - you use it as a normal variable 您也可以说它接收到一个隐藏的指针-它的作用与指针相同,但是您不需要“ * k”来取消引用它-您可以将其用作普通变量

Read up on pointers and address-of variable. 阅读指针和地址变量。 And how they're used. 以及如何使用它们。 When a function expects a pointer, you HAVE to send either a pointer or an address of a variable. 当函数需要指针时,您必须发送指针或变量的地址。 Since pointers hold addresses. 由于指针保存地址。

The second problem you're facing is that of syntax error. 您面临的第二个问题是语法错误。 Because there is no & operator that can be applied to "lvalues". 因为没有&运算符可应用于“左值”。

Well, simply i can say that you are passing an address in the function and recieving it in a pointer..Its correct as pointer can point to or hold the address. 好吧,我可以简单地说,您正在函数中传递地址并在指针中接收它。它的正确性在于指针可以指向或保留该地址。 Or other way simply if you send an address but store it in a reference variable but the reference of whom, is the question to be asked. 或者以其他方式简单地发送一个地址,但将其存储在一个引用变量中,但是要引用谁的引用才是要问的问题。 Try vice versa you'll understand the structure more better... 反之亦然,您会更好地了解其结构...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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