How can I create an array of pointers C from 2 Arrays of Pointers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lucpatricio982
    New Member
    • Oct 2017
    • 1

    How can I create an array of pointers C from 2 Arrays of Pointers?

    int main() { //

    int *A, *B, *C; //Arrays of pointers
    int i , j , k ;
    int nA, nB, nC; //Arrays size

    printf("Type the arrays size (A and B): \n");
    scanf("%d %d", &nA, &nB);

    nC = nA + nB;

    A = (int *)malloc(sizeof (int) * nA);
    B = (int *)malloc(sizeof (int) * nB);
    C = (int *)malloc(sizeof (int) * nC);

    srand((int)time ((time_t *)NULL));

    for(i = 0; i < nA; i++) {
    A[i] = rand() % 99;
    }

    for(j = 0; j<nB; j++) {
    B[j] = rand() % 99;
    }

    k = 0;
    while (k < nC) {
    if(k < nB) {
    C[k] = A[k];
    } else {
    C[k] = B[k];
    }
    printf("C[%d] = %d\n", k, C[k], nC);
    k++;
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This array C is what is called a 2-dimensional array.

    Read this and post me again if you still have questions.

    Comment

    Working...