simple arraycopy

Feb 18, 2009 at 7:53pm
EDITED: new problem
Hi kind people. Why does this not work? :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;


int main(){
    char A,B;
    char arr1[2] = {A,B};
    char copy[2];
    for(int i=0;i<2;i++){    //i want to make a copy of arr1
        copy[i]=arr1[i];
    }
            cout << copy[0] << copy[1]; //output wierd symbols :P
    return 0;
}

Last edited on Feb 18, 2009 at 8:13pm
Feb 18, 2009 at 8:00pm
closed account (z05DSL3A)
Line 9 should be copy[i] = arr1[i];
Feb 18, 2009 at 8:03pm
thanks,
i have edited the code (changed int with char). This does not work.
Last edited on Feb 18, 2009 at 8:07pm
Feb 18, 2009 at 8:12pm
It does work, it just doesn't do what you think it does.

Since copy is an array of characters, it outputs the characters represented by ASCII values 1 and 2. ASCII values 1 and 2 are unprintable characters.

Either change your array to int or change 1,2 to '1','2'
Feb 18, 2009 at 8:17pm
sorry, i forgot to correct line7:
char arr1[2] = {A,B};

And yes, it works if i put the " ' " around the letters:
char arr1[2] = {'A','B'};

but is the array for example sortable now? say i have
char arr1[20] = {'A','B','D','K','J','O',.............};
will i be able to sort the array alphabetically now?
Feb 18, 2009 at 8:22pm
closed account (z05DSL3A)
And can you NOT change the original post in the future, it makes the replies nonsense to people coming to the thread later on.
Feb 18, 2009 at 8:23pm
Sorry Mr.
Will remember that.
Last edited on Feb 18, 2009 at 8:23pm
Feb 19, 2009 at 2:16pm
Doing char arr1[2] = { A, B }; doesn't make sense since A and B are both uninitialized variables. However, using the quotes makes an array of the literal characters which you could sort.


Topic archived. No new replies allowed.