0

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.

4
  • You should state when you're asking about homework at the start of your question. Also, show what work you've done and where, specifically, you're having problems. Otherwise, you're cheating, which does you no service when it comes time for tests or (god help us) you enter the workforce. Commented Nov 25, 2011 at 10:47
  • @outis Well, this is a part of a homework problem which is asked indirectly and is in no use in the 500 line program I am working on. I am a java programmer and im just learning C syntax. You dont need to be rude... Commented Nov 25, 2011 at 12:12
  • Even if it's not the entirety of a homework problem, it should still be noted that it's homework. Full disclosure is necessary to avoid cheating. Never do I say you should post the entirety of your source code. Far from it; sample code should be complete and concise. Commented Nov 25, 2011 at 12:33
  • ... Note the "you" in my first comment becomes a generic you partway through (the lack of a proper third person animate neuter in English can be quite a problem); the ambiguity is my fault. When I mention "you enter the workforce", I wasn't thinking of you in particular, but the many poor coders who never properly applied themselves. They just end up causing more problems, costing more time and make more work for other people. Commented Nov 25, 2011 at 12:34

2 Answers 2

0

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]);
}       
Sign up to request clarification or add additional context in comments.

Comments

0

No extra library needed:

  typedef struct {char x[100];}helper;
  char *a[]={"2","3","ABCD"}, b[100];
  *(helper*)b=*(helper*)a[2];
  puts(b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.