改变C指针的值正确方法 [英] correct way to change values of c pointers

查看:150
本文介绍了改变C指针的值正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,另外C指针的问题..我有一个函数func()进行排序的数组,然后获取最大和最小的整数。我试图把它们指针变量里面在main(),但该值仅是FUNC()函数中正确的。我不明白为什么:(

 的#include<&stdio.h中GT;无效FUNC(INT ARR [],INT * S,为int * L,INT N){
    INT I = 1;
    对于(; I< N;我++){
        INT TEMP =改编[I]
        INT N =;
        而(正大于0&放大器;&放大器;常用3 [N-1]≥温度){
            改编[η] = ARR [N-1];
            N--;
        }
        ARR [N] =温度;
    }
    L =&放大器;常用3 [N-1];
    S =&放大器;改编[0]; \\
    的printf(%D \\ n,* L,* S);
}诠释主要(无效){
    INT ARR [] = {} 1,2,9,3,58,21,4;
    为int * S,* L;
    INT大小= 7;
    FUNC(ARR,S,L,大小);
    的printf(%D \\ n,* L,* S);
}


解决方案

当你传递一个指针在C,一个的拷贝参数传递给函数的的指针进行。因此,改变所述指针的值具有函数以外没有影响。然而,在记忆更改值的的引用指针将生效无处不在,只要你想。在你的情况,你需要做的是:

 无效FUNC(INT ARR [],INT * S,为int * L,INT N){
    //排序code ..
    // L =&放大器;常用3 [N-1];错 - 这些改变指针的值,
    // S =&放大器;改编[0]; \\不改变存储器的值它们引用    * L =改编[N-1]; //正确 - 在引用的内存更改数值
    * S =改编[0];
    的printf(%D \\ n,* L,* S);
}

当然,您使用的指针在的方式主要也是不正确的;他们未初始化的并且容易造成分段错误。既然有似乎没​​有理由使用实际为int * 在普通 INT 变量变量存在,我们可以采取另一种方法路过他们的引用

  INT主要(无效){
    INT ARR [] = {} 1,2,9,3,58,21,4;
    //为int * S,* L;错了 - 我们不需要指针,我们需要通过定期的整数
    INT S,L;
    INT大小= 7;
    //获取我们的地址变量的地址(安培;)运算符
    //这有效地创造出我们INT变量INT *变量
    FUNC(ARR,&安培; S,放大器; L,大小);
    的printf(%D \\ n,* L,* S);
}

请注意,术语参照这里是不是在这句话的真正意义上正确的,因为你仍然与变量相关的地址接收的复制的。大多数语言通过消除这种区别,并只允许您访问变量及其值,该拷贝有点淡出人们的视线程序员提供了一个真正的通过引用教员。你可以认为这是通过相对于参照取值,在这个意义上,它们的值可以由于被调用的函数发生变化。

Sorry, another C pointer question.. I have a function func() that sorts an array, then get the largest and smallest integers. I'm trying to put them inside pointer variables in main() but the values are only correct inside the func() function. I don't understand why :(

#include <stdio.h>

void func(int arr[], int *s, int *l, int n){
    int i = 1;
    for(; i < n; i++){
        int temp = arr[i];
        int n = i;
        while( n > 0 && arr[n-1] > temp){
            arr[n] = arr[n-1];
            n--;
        }
        arr[n] = temp;
    }
    l = &arr[n-1];
    s = &arr[0];\
    printf("%d %d\n",*l,*s);
}

int main(void){
    int arr[] = {1,2,9,3,58,21,4};
    int *s, *l;
    int size = 7;
    func(arr,s,l,size);
    printf("%d %d\n",*l,*s);
} 

解决方案

When you pass a pointer as an argument to a function in C, a copy of the pointer is made. Thus, changing the value of the pointer has no effect outside of that function. However, changing the value at the memory referenced by the pointer will take effect everywhere, as you want. In your case, you would need to do this:

void func(int arr[], int *s, int *l, int n){
    // sorting code..
    // l = &arr[n-1]; WRONG - these change the value of the pointer,
    //s = &arr[0];\   without changing the value of the memory they reference

    *l = arr[n-1]; // CORRECT - changes the value at the referenced memory
    *s = arr[0];
    printf("%d %d\n",*l,*s);
}

Of course, the way you're using the pointers in main is also incorrect; they're uninitialized and likely to cause a segmentation fault. Since there appears to be no reason to use actual int* variables over ordinary int variables there, we can take another approach to passing them "by reference":

int main(void){
    int arr[] = {1,2,9,3,58,21,4};
    // int *s, *l; WRONG - we don't need pointers, we need to pass regular ints
    int s, l;
    int size = 7;
    // Get the address of our variables with the address-of (&) operator
    // This effectively creates int* variables out of our int variables
    func(arr, &s, &l,size);
    printf("%d %d\n",*l,*s);
} 

Note that the term "by reference" here is not correct in the true sense of the phrase, since you are still receiving a copy of the address associated with the variable. Most languages provide a true by-reference faculty by removing this distinction and only allowing you access to the variable and its value, with the copying somewhat out of sight of the programmer. You can think of this as being "by reference with respect to l and s inside main", in the sense that their values can change due to the called function.

这篇关于改变C指针的值正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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