By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

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

P: 1
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++;
}
}
Oct 13 '17 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
This array C is what is called a 2-dimensional array.

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

https://bytes.com/topic/c/insights/7...rrays-revealed
Oct 13 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.