C指针,指向帮助:磁盘阵列/指针等价 [英] C Pointer help: Array/pointer equivalence

查看:213
本文介绍了C指针,指向帮助:磁盘阵列/指针等价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个玩具code例如:

  INT MAX = 5;无效fillArray(INT **的someArray,为int *等等){
  INT I;
  对于(i = 0; I< MAX;我++)
    (*的someArray)[我] =等等[I] //段错误发生在这里
}诠释主(){
  INT的someArray [MAX];
  INT嗒嗒[] = {1,2,3,4,5};  fillArray(安培;的someArray,等等);  返回0;
}

...我要填充数组的someArray,并有持续的变化之外的功能。

这是一个非常大的家庭作业的一部分,这个问题解决了问题,而让我的解决方案复制。我因为接受一个int **作为参数的函数签名,我应该code逻辑,以填补该数组。我是IM pression的非关联化和放大器下;在fillArray内的someArray()函数会给我所需要的数组(指向第一个元素),并使用该阵列上括号内的数组元素访问会给我必要需要的位置进行分配。不过,我想不通,为什么我得到段错误。

非常感谢!


解决方案

  

我要填充的数组的someArray,并有持续的变化之外的功能。


只要数组传递给函数,因为它衰减到的指针的第一个元素

 无效fillArray为(int *的someArray,为int *等等){
    INT I;
    对于(i = 0; I< MAX;我++)
        的someArray [I] =等等[I]
}

和调用:

  fillArray(的someArray,等等);

该元素的变化将是函数的外部可见。

如果实际的code是中分配一个数组 fillArray()那么 INT ** 将需要:

 无效fillArray(INT **的someArray,为int *等等){
    INT I;
    * =的someArray的malloc(sizeof的(INT)* MAX);
    如果(*的someArray)
    {
        对于(i = 0; I< MAX;我++)/ *或的memcpy()代替循环* /
            (*的someArray)[我] =等等[I]
    }
}

和调用:

 为int *的someArray = NULL;
fillArray(安培;的someArray,等等);
免费(的someArray);

In this toy code example:

int MAX = 5;

void fillArray(int** someArray, int* blah) {
  int i;
  for (i=0; i<MAX; i++)
    (*someArray)[i] = blah[i];  // segfault happens here
}

int main() {
  int someArray[MAX];
  int blah[] = {1, 2, 3, 4, 5};

  fillArray(&someArray, blah);

  return 0;
}

... I want to fill the array someArray, and have the changes persist outside the function.

This is part of a very large homework assignment, and this question addresses the issue without allowing me to copy the solution. I am given a function signature that accepts an int** as a parameter, and I'm supposed to code the logic to fill that array. I was under the impression that dereferencing &someArray within the fillArray() function would give me the required array (a pointer to the first element), and that using bracketed array element access on that array would give me the necessary position that needs to be assigned. However, I cannot figure out why I'm getting a segfault.

Many thanks!

解决方案

I want to fill the array someArray, and have the changes persist outside the function.

Just pass the array to the function as it decays to a pointer to the first element:

void fillArray(int* someArray, int* blah) {
    int i;
    for (i=0; i<MAX; i++)
        someArray[i] = blah[i]; 
}

and invoked:

fillArray(someArray, blah);

The changes to the elements will be visible outside of the function.

If the actual code was to allocate an array within fillArray() then an int** would be required:

void fillArray(int** someArray, int* blah) {
    int i;
    *someArray = malloc(sizeof(int) * MAX);
    if (*someArray)
    {
        for (i=0; i<MAX; i++)  /* or memcpy() instead of loop */
            (*someArray)[i] = blah[i];
    }
}

and invoked:

int* someArray = NULL;
fillArray(&someArray, blah);
free(someArray);

这篇关于C指针,指向帮助:磁盘阵列/指针等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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