本文介绍了传递指针数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以告诉我为什么这个程序无法正常运行
。我有一个数组a,我正在尝试创建一个

指针数组b,它指向一个小于40的元素。


#include< stdio .h>

#include< math.h>

#include< stdlib.h>


void create_ptr_list( int * a,int *** b,int n,int * size_ptr)

{

int i;

* size_ptr = 0;


for(i = 0; i< n; i ++)

{

if(a [i]< 40)

(* size_ptr)++;

}


* b = malloc(sizeof(int *)*( * size_ptr));

(* size_ptr)= 0;

for(i = 0; i< n; i ++)

{

if(a [i]< 40)

(* b)[* size_ptr ++] =& a [i];

}


}


int main(无效)

{

int a [] = {5,-6,45,-100,20,-150,160,40,0,0,1};

int ** b;

int size;

int i;


create_ptr_list(a,& b,10,& size);

for(i = 0; i
printf("%d \ n",*(b [i]));


return(0);

}


Hi, can some one please tell me why this program is not able to
function properly. I have a array a and i am trying to create a
pointer array b which points to elements less than 40 in a.

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

void create_ptr_list(int *a, int ***b, int n, int *size_ptr)
{
int i;
*size_ptr = 0;

for (i = 0; i < n; i++)
{
if (a[i] < 40)
(*size_ptr) ++;
}

*b = malloc(sizeof(int *) * (*size_ptr));
(*size_ptr) = 0;
for (i = 0; i < n; i++)
{
if (a[i] < 40)
(*b)[*size_ptr++]= &a[i];
}

}

int main(void)
{
int a[] = { 5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
int **b;
int size;
int i;

create_ptr_list(a, &b, 10, &size);
for(i = 0; i <size; i++)
printf("%d\n", *(b[i]));

return (0);
}

推荐答案



将int *** b更改为int ** b。

将main()中的''int ** b''更改为int * b。

Change int ***b to int **b.
Change the ''int **b'' in your main() to int *b.




不会int * b导致数组而不是指针数组?我需要
指针数组因此int ** b。

Wouldn''t int *b lead to an array instead of array of pointers ? I
needed array of pointers hence int **b.




我会这样写:


/ * BEGIN new.c * /


#include< stdio.h>

#include< stdlib.h>


int * create_ptr_list (int * a,size_t n,size_t * size_ptr)

{

int * b;

size_t i;


* size_ptr = 0;

for(i = 0; ni; ++ i){

if(40 a [i]){

++ * size_ptr;

}

}

b = malloc(* size_ptr * sizeof * b);

* size_ptr = 0;

if(b!= NULL){

for(i = 0; ni; i ++){

if(40 a [i]){

b [(* size_ptr)++] = a [i];

}

}

}

返回b;

}


int main(void)

{

int a [] = {5,-6,45,-100,20,-150,160,40,0,0,1};

int * b;

size_t size;

size_t i ;


b = create_ptr_list(a,10,& size);

if(b!= NULL){

for (i = 0;大小i; ++ i){

printf("%d \ n",b [i]);

}

} else {

put(" b == NULL");

}

免费(b);

返回0;

}


/ * END new.c * /


计数器和可排序数据之间的区别

用size_t计数器更明显。

我不喜欢阅读排序功能

所有内容都是int。


-

pete

I would write that this way:

/* BEGIN new.c */

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

int *create_ptr_list(int *a, size_t n, size_t *size_ptr)
{
int *b;
size_t i;

*size_ptr = 0;
for (i = 0; n i; ++i) {
if (40 a[i]) {
++*size_ptr;
}
}
b = malloc(*size_ptr * sizeof *b);
*size_ptr = 0;
if (b != NULL) {
for (i = 0; n i; i++) {
if (40 a[i]) {
b[(*size_ptr)++]= a[i];
}
}
}
return b;
}

int main(void)
{
int a[] = {5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
int *b;
size_t size;
size_t i;

b = create_ptr_list(a, 10, &size);
if (b != NULL) {
for (i = 0; size i; ++i) {
printf("%d\n", b[i]);
}
} else {
puts("b == NULL");
}
free(b);
return 0;
}

/* END new.c */

The distinction between counters and sortable data
is more obvious with size_t counters.
I don''t like to read sorting functions
where everything is int.

--
pete


这篇关于传递指针数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:46