C程序,指针参数都不会保存值 [英] C program, pointer argument won't hold values

查看:91
本文介绍了C程序,指针参数都不会保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我很抱歉把这个打扰您,但我已经开始在这里失去它..
我最近用C重新开始编程,我遇到了一些错误,只是我想不通..
我的C程序是(应该是)一个简单的人,所以它需要做到以下几点:
自然元素的未定义数量读取形成键盘,直到0被读取。之后,它必须计算所有元素的乘积,并计算该结果的末尾的0的个数..

Hi guys I'm sorry to bother you with this but I'm starting to loose it here.. I have recently started programming in C again and I have run into some kind bug that just I can't figure out.. My C program is(should be) an easy one, so it needs to do the following: An undefined number of natural elements is read form the keyboard until a 0 is read. After that it has to compute the product of all elements and compute the number of 0-s on the end of that result..

int input(int* v)                      {

    int n = 0;   
    do                                                            
    {
        n = n + 1;
        v = (int*) realloc(v,n*sizeof(int));        printf("Enter number %d: ",n);          scanf("%d",&v[n-1]);
    }
    while(v[n-1] != 0);           
    n--;
    return n;  }

int tZeros(int* v, int eNb)     {    
    int i;
    int  val = 1;
    for(i = 0; i < eNb; i++)
    {
        val = val * v[i];        
    }

    i=0;
    while(val % 10 == 0)
    {
        i++;
    }  
    return i; }

int main (int argc, char** argv)     {

    int* v = NULL;                                                  
    int eNb = input(&(*v));                                          
    if(eNb>0)                                                       
    {
        int zeros = tZeros(v, eNb);
        printf("The number of ending zeros in the product of the given numbers is: %d",zeros);

    }
    else
    {
        printf("No elements to do computations with!\n");
    }
    free(v);
    return 0; 
    }

输入函数应该返回两件事情:动态数组中的元素和实际的元素..数量,但在输入函数运行后的值中输入有全部丢失..
我通过参数转换为输入作为指针,所以应该在正确的地址更改值吧?

The input function should return two things: The number of elements and the actual elements in the dynamic array.. But after the input function runs the values entered there are all lost.. I pass the argument into input as a pointer so it should change the values at the correct addresses right?

所有建议都欢迎!
日Thnx!

All suggestions are welcome! Thnx!

P.S。我认为这个问题是我的阵列诉。退出它失去所有的值输入功能后..

P.s. I think the problem is with my array v.. after exiting the input function it looses all values..

推荐答案

用C一切都按值传递的,所以当你执行任务:

Everything in C is passed by value, so when you perform the assignment:

v = (int*) realloc(v,n*sizeof(int));

您要分配一个新的值的变量的本地副本的v 。如果你想改变指针本身的价值(而不是修改的什么的指针已指向),您将需要一个指针传递给一个指针和反引用一次执行任务,即

You are assigning a new value to the local copy of the variable v. If you want to change the value of the pointer itself (as opposed to modifying what the pointer already points to) you will need to pass a pointer to a pointer and dereference it once to perform the assignment, i.e.,

int ModifyPointer( int **v )
{
    // error checking and whatnot
    *v = realloc(*v,n*sizeof(int));
}

它认为这种方式;你必须驻留在内存地址为0xA和指向的内存地址0xFF的一个指针 P 。你通过 P 来的函数,将会创建一个副本 v 。复制 v 驻留在内存地址0x2A,但仍指向的内存地址0xFF的。

Think of it this way; you have a pointer p that resides at memory address 0xA and points to memory address 0xFF. You pass p to a function and a copy v is created. The copy v resides at memory address 0x2A, but still points to memory address 0xFF.

                     p(0xA)      v(0x2A)
                       |            |
                        \          /
                         \        /
                          \      /
                            0xFF

所以,当你解引用指针要么你最终指相同的内存块,的但指针本身住在不同的地方在内存的。因此,当你修改副本的值时,它不会反映在原始(即realloc的是创造一个新的存储位置的新指针,并将其分配给变量 v ,但 p 不变。)

So, when you dereference either pointer you end up referring to the same chunk of memory, but the pointers themselves live in different places in memory. Thus, when you modify the value of the copy it is not reflected in the original (i.e., realloc is creating a new pointer in a new memory location and assigning it to the variable v, but p remains unchanged.)

这篇关于C程序,指针参数都不会保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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