English 中文(简体)
在一个职能中改变点名并不反映职能之外[重复]
原标题:Changing the pointer inside a function does not reflect outside the function [duplicate]
  • 时间:2015-04-30 11:01:39
  •  标签:
  • c
  • pointers
void alloco(int *ppa)
{
    int i;
    printf("inside alloco %d
",ppa);
    ppa = (int *)malloc(20);
    ppa[15] = 9;
    printf("size of a %d 
", sizeof(ppa));
    for(i=0;i<20;i++)
    printf("a[%d] = %d 
", i, ppa[i]);
}

int main()
{
    int *app = NULL;
    int i;
    printf("inside main
");
    alloco(app);
    for(i=0;i<20;i++)
    printf("app[%d] = %d 
", i, app[i]);
    return(0);
}

基本上,我想做的是,从我的<代码>main到职能(alloco<>/code>)中转出一个无效点,该功能分配到点人指点和回报的同一地点。 我正在正确阅读当地印刷材料,这些印刷材料属于内部功能(alloco,但并未载于main

我在这里做了什么错误?

最佳回答

你们需要:

void alloco(int **ppa)
{
    int i;
    printf("inside alloco %p
",ppa);
    *ppa = malloc(20 * sizeof(int));
    (*ppa)[15] = 9;     // rather pointless, in the loop below (*ppa)[15] will be
                        // overwritten anyway
    printf("size of a %d 
", sizeof(*ppa));  // rather pointless, not sure
                                              // what you want to print here

    for(i = 0; i < 20; i++)
      printf("a[%d] = %d 
", i, (*ppa)[i]);
}

int main()
{
    int *app = NULL;  // you can drop the  = NULL , it s useless, because
    int i;            // alloco(&app) will change the value of app anyway
    printf("inside main
");
    alloco(&app);

    for(i = 0; i < 20; i++)
      printf("app[%d] = %d 
", i, app[i]);

    return 0;
}

your节目中,请通过一个点到alloco,该点将载于ppa para amount。 <>ppa参数与alloco内的一个当地变量相同,经您修改后,您在<代码>main/code>(app)中转至该功能的原始价值即为t。

在经更正的版本中,我们通过了pointer to app。 在<代码>alloco中,我们参照该点,并写出其小值。

问题回答

<代码>C对功能参数的通过使用按价值分列的通行证。 可在<条码>上更改数值>。 换言之,ppa为地方职能alloco(。 更改<代码>ppa的数值,对(改为)<代码>app,载于main(

如果你想要改变<代码>应用的价值。 自alloco()起,您需要通过pointer to app(Pointer-to-Pointer)。

除此以外,在您的《法典》中

ppa = (int *)malloc(20);  

是错误的。 你们想要的东西实际上是什么。

ppa = malloc(20 * sizeof (int)); // argument is in bytes, not in `int` or any elements

或更好,

ppa = malloc(20 * sizeof * ppa);  // data type integrity

值得一提的是,请 不要投下>>malloc(和家庭。 C

另有一件事,同时印刷pointer<>em>,使用%p Format specifier,而印刷size_t,使用%zu

printf("inside alloco %p
",ppa);

以及

printf("size of a %zu
", sizeof(ppa));  // sizeof returns size_t




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签