从函数C中分配数组和赋值

时间:2022-09-25 08:58:20

C question

Hi,

你好,

I am passing a double pointer to a function to allocate a double array and initialise the array inside the function with lets say 10.10;

我将一个双指针传递给一个函数来分配一个双数组并在函数内初始化数组,比如10.10;

I do the following but get segmentation fault when I access the array in main;

我做了以下操作,但是当我访问主数组时,会得到分割错误;

void function(double **array, int size){

    *array = (double*) malloc(size * sizeof(double)); 
    int i;
    for(i=0;i<size;i++){
    *array[i] = 10.10;
    }


}

int main(){

    double *array = NULL;

    function(&array,20);
    printf("array[0] = %lg\n",array[0]);// here is where I get segmentation fault


}

Any help ?

任何帮助吗?

2 个解决方案

#1


1  

*array[i]

doesn't mean what you think it does (look it up using a C operator precedence table).

并不表示您认为它是什么(使用C操作符优先表查找)。

Instead of unreadable, ugly and confusing (yes, it just confused you) code, use a temporary variable (and do not for the love of God cast the return value of malloc!):

代码不是不可读的、丑陋的和令人困惑的(是的,它只是让你困惑),而是使用一个临时变量(不要为了上帝的爱而使用malloc的返回值!)

void function(double **array, int size)
{
    if (array == NULL) return;

    double *tmp = malloc(size * sizeof(*tmp)); 
    if (tmp == NULL) {
        *array = NULL;
        return;
    }

    int i;
    for (i = 0; i < size; i++) {
        tmp[i] = 10.10;
    }

    *array = tmp;
}

Also, return 0; from main(). Really.

此外,返回0;从主()。真的。

#2


3  

You have fallen foul of operator precedence.

你违反了操作符优先级。

Change:

变化:

for(i=0;i<size;i++){
    *array[i] = 10.10;
}

to:

:

for(i=0;i<size;i++){
    (*array)[i] = 10.10;
}

Note that if you had compiled with warnings enabled (e.g. gcc -Wall ...) your compiler would have caught this for you. Always compile with warnings enabled and always pay heed to, understand and fix any warnings.

请注意,如果您使用已启用的警告(例如gcc -Wall…)进行编译,您的编译器将为您捕捉到这一点。经常使用警告进行编译,并始终注意、理解和修复任何警告。

#1


1  

*array[i]

doesn't mean what you think it does (look it up using a C operator precedence table).

并不表示您认为它是什么(使用C操作符优先表查找)。

Instead of unreadable, ugly and confusing (yes, it just confused you) code, use a temporary variable (and do not for the love of God cast the return value of malloc!):

代码不是不可读的、丑陋的和令人困惑的(是的,它只是让你困惑),而是使用一个临时变量(不要为了上帝的爱而使用malloc的返回值!)

void function(double **array, int size)
{
    if (array == NULL) return;

    double *tmp = malloc(size * sizeof(*tmp)); 
    if (tmp == NULL) {
        *array = NULL;
        return;
    }

    int i;
    for (i = 0; i < size; i++) {
        tmp[i] = 10.10;
    }

    *array = tmp;
}

Also, return 0; from main(). Really.

此外,返回0;从主()。真的。

#2


3  

You have fallen foul of operator precedence.

你违反了操作符优先级。

Change:

变化:

for(i=0;i<size;i++){
    *array[i] = 10.10;
}

to:

:

for(i=0;i<size;i++){
    (*array)[i] = 10.10;
}

Note that if you had compiled with warnings enabled (e.g. gcc -Wall ...) your compiler would have caught this for you. Always compile with warnings enabled and always pay heed to, understand and fix any warnings.

请注意,如果您使用已启用的警告(例如gcc -Wall…)进行编译,您的编译器将为您捕捉到这一点。经常使用警告进行编译,并始终注意、理解和修复任何警告。