Ç - 传递指针给一个函数 [英] c - passing pointers to a function

查看:107
本文介绍了Ç - 传递指针给一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变,其中三分球最小值和最大值的指针,但似乎变是不是该函数的范围外贴(函数运行后)。运行函数之前,我设定*最小和最大*指向了双M = 0。我是怎样的一个小白所以任何意见将是AP preciated。

  INT min_max(双*分,双*最大,诠释大小,加倍[]){
INT I;的printf(\\ N%LG \\ T%LG \\ n,*分,*最大值);分=安培;一个[0];
最大=&放大器;一个[0];对于(i = 1; I<大小;我++){
 如果(一个[Ⅰ]≥*最大)
  最大=&放大器; A [];
 否则如果(一个[1] - *分钟)
  分=安培; A [];
 }
 的printf(\\ N%LG \\ T%LG \\ n,*分,*最大值);
 返回0;
}


解决方案

如果您发送一个指向双,你是允许的双重改变的内容。如果你希望指针改变,你必须发送一个指针的指针(双** ),它允许指针的内容改变。

  INT min_max(双**分,双**最大,诠释大小,加倍[]){
    INT I;    的printf(\\ N%LG \\ T%LG \\ n,**分,**最大);    *分钟=安培;一个[0];
    *最大=&放大器;一个[0];    对于(i = 1; I<大小;我++){
        如果(一个[Ⅰ]≥**最大)
            *最大=安培; A [];
        否则如果(一个[1] - ; **分钟)
            *分=安培; A [];
    }
    的printf(\\ N%LG \\ T%LG \\ n,**分,**最大);
    返回0;
}

通过所有间接引用会在这里,它可能是有意义的,以保持两个本地指针甚至只是指数,并在循环的最后一次设定的返回值。


  

为什么我不能在一个函数改变指针?如果我不是
  发送阵列(比如东西的指针)保留的最小值和最大值,
  比方说,MINMAX [0] =分钟,和极大极小[1] = max时,值将发生变化,无


嗯,有点,但你要求一个指向是最小和最大,而不是实际值的数组元素。所以,即使你传递一个数组来做到这一点,它必须是一个指针数组(双* MINMAX [2] )。现在,这其实只是一个双** ,它指向两个双* 值(你指数元素0和分别为1)。所以这是同样的事情。

现在,为什么你不能改变一个指针?您可以!但是你的变化是你的函数范围内的局限。你不能改变值的回呼叫者的,因为双* 指针是按值传递。您可能需要做一些阅读传递按值传递和按引用之前,我去混淆你,但总的思路是这样的:

您发送给函数的任何数据类型有效复制。所以,如果你传递一个双击,该值从呼叫者复制到新的存储位置(参数传递给函数)。该功能现在已经没有提及该值的原始位置。

 无效my_func,并将(双VAL){
    VAL = 42; //不会影响来电者的价值,因为它被复制
}双值= 1;
my_func,并将(值);
//值仍然是1

当你通过一个

这同样双* 。正在发送双击的地址值的功能,但在实际地址的(指针)是复制到<值code>双* 提供给你的函数。

 无效my_func,并将(双* VAL){
    * VAL = 42; //东西42到内存位置VAL指出。
}双值= 1;
my_func,并将(值);
//值现在为42

但主叫方似乎希望最大值和最小值的数组中的实际地址(除非这是一个错误,由于是新的指针)。在这种情况下,指针是不够的,因为你复制你的指针中的内容。在这里,你需要保存你的指针存储的地址,并通过对函数。这个地址被复制,而当你引用它,你能够指针写入该内存位置。

 无效my_func,并将(双** VAL){
    * VAL = * VAL + 1; //改变指针由VAL指出。
}双值[4] = {1,2,3,42};
双*值=安培;值[2]; //值指向3。
my_func,并将(安培;值);
//值现在指向42

每当你提供一个指向你想要的功能被改变的值,它是指通过引用传递。

I'm trying to change where the pointers min and max are pointer to, but it seems that the "change" is not sticking outside the function's scope (after the function has run). Before running the function I set *min and *max to point to a "double m = 0". I'm kind of a NOOB so any advice would be appreciated.

int min_max(double * min , double * max , int size , double a[]){                                                                       
int i;                                                                                                                                

printf("\n%lg\t%lg\n", *min, *max);                                                                                                   

min = &a[0];                                                                                                                          
max = &a[0];                                                                                                                          

for (i = 1 ; i < size ; i++) {                                                                                                        
 if (a[i] > *max)                                                                                                                    
  max = &a[i];                                                                                                                      
 else if (a[i] < *min)                                                                                                               
  min = &a[i];                                                                                                                      
 }                                                                                                                                     
 printf("\n%lg\t%lg\n", *min, *max);                                                                                                   
 return 0;                                                                                                                             
}

解决方案

If you send a pointer to a double, you are allowing the contents of that double to change. If you want the pointer to change, you must send a pointer to the pointer (double**), which allows the contents of the pointer to change.

int min_max(double ** min , double ** max , int size , double a[]){  
    int i;                                                                                                                                

    printf("\n%lg\t%lg\n", **min, **max);                                                                                                   

    *min = &a[0];                                                                                                                          
    *max = &a[0];                                                                                                                          

    for (i = 1 ; i < size ; i++) {                                                                                                        
        if (a[i] > **max)                                                                                                                    
            *max = &a[i];                                                                                                                      
        else if (a[i] < **min)                                                                                                               
            *min = &a[i];                                                                                                                      
    }                                                                                                                                     
    printf("\n%lg\t%lg\n", **min, **max);                                                                                                   
    return 0;                                                                                                                             
}

With all the dereferencing going on here, it may make sense to keep two local pointers or even just the indices, and set the return values once at the end of the loop.

[edit]

Why is it that I can't change a pointer in a function? If I instead send an array (a pointer like thing) to retain the min and max values, say, minmax[0]=min, and minmax[1]=max, the values will change, no?

Well, sort of, but you're asking for a pointer to the array element that is min and max, not the actual value. So even if you passed an array to achieve that, it would have to be an array of pointers (double *minmax[2]). Now, that's actually just a double** that points to two double* values (which you index as element 0 and 1 respectively). So it's the same thing.

Now, why can't you change a pointer? You can! But your changes are confined within the scope of your function. You can't change the value back at the caller because the double* pointer is passed by value. You might need to do some reading on pass-by-value and pass-by-reference before I go confusing you, but the general idea is this:

Any data type that you send to a function is effectively copied. So if you are passing a double, that value is copied from the caller into a new memory location (a parameter to the function). The function now has no reference to the original location of that value.

void my_func( double val ) {
    val = 42;  // Does not affect caller's value because it was copied
}

double value = 1;
my_func( value );
// value is still 1

The same goes when you pass a double*. You are sending the address of a double value to the function but the actual address (the pointer) is a value that is copied into the double* supplied to your function.

void my_func( double* val ) {
    *val = 42;  // Stuff 42 into the memory location pointed to by 'val'.
}

double value = 1;
my_func( value );
// value is now 42

But your caller appears to want the actual address within the array of the max and min values (unless this was a mistake due to being new to pointers). In that case, a pointer is not enough, because you are copying the contents of your pointers. Here you need to take the address of the memory that holds your pointer and pass that to the function. That address is copied, and when you reference it you are able to write a pointer into that memory location.

void my_func( double** val ) {
    *val = *val + 1;  // Change the pointer pointed to by 'val'.
}

double values[4] = { 1, 2, 3, 42 };
double *value = &values[2];         // value points to '3'.
my_func( &value );
// value now points to 42

Whenever you're supplying a pointer to a value that you want to be changed by the function, it's referred to as passing by reference.

这篇关于Ç - 传递指针给一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆