P
pereges
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 < 40)
(*size_ptr) ++;
}
*b = malloc(sizeof(int *) * (*size_ptr));
(*size_ptr) = 0;
for (i = 0; i < n; i++)
{
if (a < 40)
(*b)[*size_ptr++]= &a;
}
}
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));
return (0);
}
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 < 40)
(*size_ptr) ++;
}
*b = malloc(sizeof(int *) * (*size_ptr));
(*size_ptr) = 0;
for (i = 0; i < n; i++)
{
if (a < 40)
(*b)[*size_ptr++]= &a;
}
}
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));
return (0);
}