2016-04-10 15 views
-3

私は新しい配列(ARR)へのこのポインタを逆参照しようとしていますが、下の例では、逆の処理を行うときに*issubは最初の文字のみを持ちます。どうすればこの状況を修正し、ARR(subscount)を私が欲しい言葉にすることができますか?配列の参照を間接的に削除する

#include <iostream> 
    int main(){ 
    char inp[3]={'O','I','L'}; 
    int lll=3; 
    char ARR[3]; 
    int subscount=0; 

    char * issub= new char[lll]; 
    for(int i=0;i<lll;i++){ 
    issub[i]=inp[i]; 
    } 
    ARR[subscount]=*issub; 
} 
+0

あなたのコード@おかげで、悪いです! – Poriferous

+0

正確に何をしたいですか? 'ARR'のすべてのセルを' inp'、**または**と同じになるように 'ARR [subsount]'にポインタを置くとその単語を指し示しますか? – gsamaras

+0

@gsamaras inp –

答えて

1

私はあなたが混乱していることを感じるので、私はあなたのための例作ら:

#include <iostream> 

int main() { 
    char inp[4] = {'O','I','L', '\0'}; 
    int lll = 4; 
    // initialize elements of 'ARR' to 0 for safety 
    char ARR[4] = {0}; 
    int subscount = 0; 

    // dynamic allocation, DO NOT forget to de-allocate 
    char* issub = new char[lll]; 
    // copy every element of 'inp' to 'issub' 
    for(int i=0;i<lll;i++) { 
    issub[i]=inp[i]; 
    } 
    // this will copy where '*issub' points to, 
    // that is the 'issub[0]', to 'ARR[subscount]' 
    ARR[subscount] = *issub; 
    std::cout << ARR << "\n"; // prints: O 
    // you can use a for loop as before to copy the contents, 
    // from 'inp', 'issub' to 'ARR' 

    // However, we will do something different here, 
    // so I am de-allocating 'issub' 
    delete [] issub; 

    // We will use an array of pointers, with size 2, 
    // thus it will have two pointers in total. 
    char* ptr_arr[2]; 

    // Assign the first pointer to 'inp' 
    ptr_arr[0] = &(inp[0]); 

    std::cout << ptr_arr[0] << "\n"; // prints OIL 

    // we don't use the second pointer, 
    // set it to NULL 
    ptr_arr[1] = NULL; 
    return 0; 
} 

希望に役立ちます(それは実際にかなりstd::stringを使用しなければならないC++、より、Cのことを思い出します)。


nullで終了する文字列で更新されました。 What is a null-terminated string?

INPは、範囲外のインデックスを使用してアクセスされているので、M.M

+0

私はそれを知ったと思いますが、 'inp [0]'最初の文字に過ぎないのですか? –

+0

@ J.Barbosa 'inp [0]'が最初の文字です。はい。 –

関連する問題