指针初始化问题...... [英] Pointer initialization question...

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

问题描述

大家好:


以下是基本执行相同任务的两段代码。

版本A产生分段错误,而版本B工作

正确。我理解为什么版本B可以正常工作,但我不知道
理解为什么版本A不起作用。在我看来,在版本

A中,一个指向整数数组的指针作为参数传递给

init_array,无论该指针是否为NULL,应该是

初始化为一个新的整数数组。


任何人都可以详细说明吗?谢谢。


---------版本A(赛格错误)--------


# include< stdio.h>

#include< stdlib.h>


void init_array(int * array,int num)

{

array =(int *)malloc(sizeof(int)* num);

}


int main()

{

int array_size = 5;

int * array;

init_array(array,array_size );

array [3] = 10; / *这导致分段错误* /

返回1;

}


---------版本B(正确工作)--------


#include< stdio.h>

#include< stdlib.h>


int * init_array(int num)

{

int * array;

array = (int *)malloc(sizeof(int)* num);

返回数组;

}


int main()

{

int array_size = 5;

int * array;

array = init_array(array_size);

数组[3] = 10;

返回1;

}

Hi all:

Below are two pieces of code that basically perform the same task.
Version A produces a segmentation fault, while version B works
correctly. I understand why version B works correctly, but I do not
understand why version A does not work. It seems to me that in Version
A, a pointer to an array of integers is passed as a parameter to
init_array, and whether that pointer is NULL or not, is should be
initialized as a new array of integers.

Can anyone elaborate? Thanks.

---------Version A (Seg Fault)--------

#include <stdio.h>
#include <stdlib.h>

void init_array (int * array, int num)
{
array = (int *)malloc(sizeof(int) * num);
}

int main ()
{
int array_size = 5;
int * array;
init_array(array, array_size);
array[3] = 10; /* This causes a segmentation fault */
return 1;
}

---------Version B (Works Correctly)--------

#include <stdio.h>
#include <stdlib.h>

int * init_array (int num)
{
int * array;
array = (int *)malloc(sizeof(int) * num);
return array;
}

int main ()
{
int array_size = 5;
int * array;
array = init_array(array_size);
array[3] = 10;
return 1;
}

推荐答案

嘿,在版本A中无法初始化指向数组的指针

!试试这个:


#include< stdio.h>

#include< stdlib.h>


void init_array(int ** array,int num)

{

* array =(int *)malloc(sizeof(int)* num);

}


int main()

{

int array_size = 5;

int * array;

init_array(& array,array_size);

array [3] = 10; / *这不会导致分段错误* /

返回1;

}

// BTW:你必须通过硬链接指向初始化它的指针,

但不是指针的副本。当你传递指针的副本时,

指针只会在init_array

函数的范围内初始化。


抱歉我的英语不好。

Hey, in version "A" the pointer to the array was not initialized
anyway! Try this:

#include <stdio.h>
#include <stdlib.h>

void init_array (int ** array, int num)
{
*array = (int *)malloc(sizeof(int) * num);
}

int main ()
{
int array_size = 5;
int * array;
init_array(&array, array_size);
array[3] = 10; /* This will not causes a segmentation fault */
return 1;
}
// BTW: you must to pass a hard link to the pointer to initialize it,
but not a copy of the pointer. When you passing a copy of the pointer,
the pointer will only be initialized in the scope of the init_array
function.

Sorry for my bad English.




No No Luck <无********* @ hotmail.com>在消息中写道

news:11 ********************* @ z14g2000cwz.googlegro ups.com ...

"No Such Luck" <no*********@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
大家好:

以下是基本执行相同任务的两段代码。
版本A产生分段错误,而版本B正常工作。我理解为什么版本B正常工作,但我不理解为什么版本A不起作用。在我看来,在Version
A中,一个指向整数数组的指针作为参数传递给
init_array,并且该指针是否为NULL,应该被初始化为一个新的整数数组。

任何人都可以详细说明吗?谢谢。

---------版本A(Seg Fault)--------

#include< stdio.h>
#include< stdlib.h>

void init_array(int * array,int num)
{/> array =(int *)malloc(sizeof(int) * num);


1.删除(int *)。这不是必要的,可能会掩盖其他错误

(缺少#include< stdlib.h>)。

2.如果你想通过指针返回指针;阵列"使它成为一个int **数组

并分配给*数组。

现在,当你的程序离开时,你新分配的指针会丢失

init_array范围。

}

int main()
{array_size = 5;
int * array;
init_array(array,array_size);
array [3] = 10; / *这导致分段错误* /


难怪。 int *数组永远不会被初始化。见上面的(2)。

返回1;
}
---------版本B(正确工作)----- ---

#include< stdio.h>
#include< stdlib.h>

int * init_array(int num)
{
int * array;
array =(int *)malloc(sizeof(int)* num);
返回数组;
}
Hi all:

Below are two pieces of code that basically perform the same task.
Version A produces a segmentation fault, while version B works
correctly. I understand why version B works correctly, but I do not
understand why version A does not work. It seems to me that in Version
A, a pointer to an array of integers is passed as a parameter to
init_array, and whether that pointer is NULL or not, is should be
initialized as a new array of integers.

Can anyone elaborate? Thanks.

---------Version A (Seg Fault)--------

#include <stdio.h>
#include <stdlib.h>

void init_array (int * array, int num)
{
array = (int *)malloc(sizeof(int) * num);
1. Remove "(int *)". It''s not neccesary and may obscure other errors
(missing #include <stdlib.h>).
2. If you want to return a pointer through "array" make it an "int **array"
and assign to *array.
Right now, your newly allocated pointer is lost when your program leaves
the init_array scope.
}

int main ()
{
int array_size = 5;
int * array;
init_array(array, array_size);
array[3] = 10; /* This causes a segmentation fault */
No wonder. int* array is never initialized. See (2) above.
return 1;
}

---------Version B (Works Correctly)--------

#include <stdio.h>
#include <stdlib.h>

int * init_array (int num)
{
int * array;
array = (int *)malloc(sizeof(int) * num);
return array;
}




见上文(1)。现在arry *被正确地返回并且嘿,presto!你的

计划有效。


< snip>



See(1) above. Now arry *is* properly returned and "hey, presto!" your
program works.

<snip>


No Such Luck写道:
No Such Luck wrote:

以下是基本执行相同任务的两段代码。
版本A产生分段错误,而版本B正常工作
。我理解为什么版本B正常工作,但我不理解为什么版本A不起作用。


这完全包含在FAQ中,

main的正确返回值(你的不是其中之一)。请在发布前查看常见问题解答。

学会缩进代码。


在此期间,请将以下内容与您的代码进行比较:


#include< stdio.h>

#include< stdlib.h>


void init_arrayA(int ** a ,int num)

{

* a = malloc(num * sizeof ** a);

}

int * init_arrayB(int num)

{

int * a;

a = malloc(num * sizeof * a);

返回a;

}


int main()

{

int array_size = 5;

int * array;

init_arrayA(& array,array_size);

if(!array)

fprintf(stderr,malloc(A)failed\\\
);

else {

array [3] = 10;

printf(" array [3](A)=%d \ n",array [3]);

free(array);

}

if(!(array = init_arrayB(array_size)))

fprintf(stderr,malloc(B)failed\\\
);

else {

array [3] = 12;

printf(" array [3](B)=%d \ n",array [3] );

免费(数组);

}

返回0;

}

数组[3](A)= 10

数组[3](B)= 12


任何人都可以详细说明吗?谢谢。


我们真的不应该。通过发布

问题而不检查常见问题解答并首先关注新闻组来鼓励人们行为不端。

a糟糕的主意。


[OP 's代码]

#include< stdio.h>
#include< stdlib.h>

void init_array(int * array,int num )
{array =(int *)malloc(sizeof(int)* num);
}
int main()
{
int array_size = 5;
int * array;
init_array(array,array_size);
array [3] = 10; / *这导致分段错误* /
返回1;
}
---------版本B(正确工作)------ -

#include< stdio.h>
#include< stdlib.h>
int * init_array(int num)
{
int * array;
array =(int *)malloc(sizeof(int)* num);
返回数组;
}
int main ()
{array_size = 5;
int * array;
array = init_array(array_size);
array [3] = 10;
返回1;
}
Hi all:

Below are two pieces of code that basically perform the same task.
Version A produces a segmentation fault, while version B works
correctly. I understand why version B works correctly, but I do not
understand why version A does not work.
This is fully covered in the FAQ, as are the proper return values from
main (yours isn''t one of them). Please check the FAQ before posting.
And learn to indent your code.

In the meantime, compare the following to your code:

#include <stdio.h>
#include <stdlib.h>

void init_arrayA(int **a, int num)
{
*a = malloc(num * sizeof **a);
}
int *init_arrayB(int num)
{
int *a;
a = malloc(num * sizeof *a);
return a;
}

int main()
{
int array_size = 5;
int *array;
init_arrayA(&array, array_size);
if (!array)
fprintf(stderr, "malloc (A) failed\n");
else {
array[3] = 10;
printf("array[3] (A) = %d\n", array[3]);
free(array);
}
if (!(array = init_arrayB(array_size)))
fprintf(stderr, "malloc (B) failed\n");
else {
array[3] = 12;
printf("array[3] (B) = %d\n", array[3]);
free(array);
}
return 0;
}
array[3] (A) = 10
array[3] (B) = 12

Can anyone elaborate? Thanks.
We really shouldn''t. Encouraging people to misbehave by posting
questions without checking the FAQ and following the newsgroup first is
a bad idea.

[OP''s code]
#include <stdio.h>
#include <stdlib.h>

void init_array (int * array, int num)
{
array = (int *)malloc(sizeof(int) * num);
}

int main ()
{
int array_size = 5;
int * array;
init_array(array, array_size);
array[3] = 10; /* This causes a segmentation fault */
return 1;
}

---------Version B (Works Correctly)--------

#include <stdio.h>
#include <stdlib.h>

int * init_array (int num)
{
int * array;
array = (int *)malloc(sizeof(int) * num);
return array;
}

int main ()
{
int array_size = 5;
int * array;
array = init_array(array_size);
array[3] = 10;
return 1;
}



这篇关于指针初始化问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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