[ create a new paste ] login | about

Link: http://codepad.org/zX53XG8v    [ raw code | output | fork ]

C++, pasted on Jun 16:
//#include <cstdio>
//#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>

#define N 20
 
void function(int n, int *a, int **&b) {
  if ((b = (int **)malloc(sizeof(int *) * n)) != 0) {
    for (int i = 0; i < n; i++)
      b[i] = &a[i];
  } else {
    b = 0;
  }
}
 
int main() {
  int *a, **b;
 
  if ((a = (int *)malloc(sizeof(int) * N)) != 0) {
    for (int i = 0; i < N; i++)
      a[i] = i * 2;

    if (function(N, a, /* ref */b), b != 0) {
      for (int i = 0; i < N; i++)
        printf("%d ", *b[i]);
      putchar('\n');
      free(b);
    }    
    free(a);
  }    
  return 0;
}
/* end */


Output:
1
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 


Create a new paste based on this one


Comments: