simple arraycopy

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
closed account (z05DSL3A)
Line 9 should be copy[i] = arr1[i];
thanks,
i have edited the code (changed int with char). This does not work.
Last edited on
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'
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?
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.
Sorry Mr.
Will remember that.
Last edited on
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.