Reputation:
Lets say I have an array = {2, 3, ABCD}
First, I need to make the third element equal to a new Array. And I know char newArr [] = array [2] wont work, so how do I go about this?
Second, I need to print out the characters of the newArr one by one. So my output should be A B C D
They should be separated from each other. I know how to do this in java, but I have no idea what the syntax is in C. Your help is greatly appreciated.
Upvotes: 0
Views: 1146
Reputation: 3990
No extra library needed:
typedef struct {char x[100];}helper;
char *a[]={"2","3","ABCD"}, b[100];
*(helper*)b=*(helper*)a[2];
puts(b);
Upvotes: 0
Reputation: 455272
Something like:
char *array[] = {"2", "3", "ABCD"}; // your existing array.
char n = strlen(array[2]); // size of 2nd element.
char *newArr = malloc(n); // create new array.
int i;
// populate the new array.
for(i=0;i<n;i++) {
newArr[i] = array[2][i];
}
// print.
for(i=0;i<n;i++) {
printf("%c\n",newArr[i]);
}
Upvotes: 0