2016-10-27 18 views
1

私はウェブ上で検索しようとしましたが、何をしようとしているのか分かりませんでした。あなたが私を助けてくれることを願っています。文字列へのポインタの配列の割り当て

問題:2つのchar配列、つまりstringstring2があります。どちらも最初は空です。私はこれらの文字列に2つの入力を与えます。 string2へのポインタを使ってstringの値を代入したいと思います。

は、ここに私のコードです:

int main(void) { 
    char string[2][MAXLEN]; 
    char string2[2][MAXLEN]; 
    char *pointer[2]; 
    pointer[0] = &string2[0]; 
    pointer[1] = &string2[1]; 
    scanf("%s", string[0]); //Assume i scan in "ab" 
    scanf("%s", string[1]); //assume i scan in "cd" 

    //Now string[0] contains "ab" and string[1] contains "cd" 
    //I want to deferences the pointer and assign "cd" to it, so string2[0] will contain "cd" 

    *pointer[0] = string[0]; 
    printf("%s", string2[0]); //but this does not print "cd" 
} 

*編集 私はstrcpyのを使用することができます理解しますが、イムは、それがポインタを使用して行うことを学ぶしようとしています。この点についての助けは素晴らしいでしょう。

+0

参照[尋ねる]、および必要な情報を提供しています。あなたの質問は何ですか?注意:配列はポインタではありません!アドレス演算子を削除します。 – Olaf

+0

'strcpy'関数の使い方を尋ねていますか? – Lundin

+1

おそらく '* pointer [0] = string [0];の代わりに' strcpy(string2 [0]、string [0]); –

答えて

0

コンパイラが理解できる情報の構造全体をコピーしていないため、配列の各要素を個別にコピーする必要があります。通常、これはNULまたはサイズをチェックforループで行われますが、私は不正行為とちょうどあなたが望むコピーするだろう構文を示すのです:ポインタについては

#define MAXLEN 10 

int main(void) 
{ 
    char string[2][MAXLEN]; 
    char string2[2][MAXLEN]; 
    char *pointer[2]; 
    pointer[0] = &string2[0]; 
    pointer[1] = &string2[1]; 

    // Replace scanf for simplicity 
    string[0][0] = 'a'; string[0][1] = 'b'; string[0][2] = '\0'; 
    string[1][0] = 'c'; string[1][1] = 'b'; string[1][2] = '\0'; 

    // For loop or strcpy/strncpy/etc. are better, but showing array method of copying 
    pointer[0][0] = string[1][0]; 
    pointer[0][1] = string[1][1]; 
    pointer[0][2] = string[1][2]; 

    printf("%s", string2[0]); 

    return 0; 
} 

を、あなたはこれを行うことができます:

#define MAXLEN 10 

int main(void) { 
    char string[2][MAXLEN]; 
    char string2[2][MAXLEN]; 
    char *pointer[2]; 
    pointer[0] = &string2[0]; 
    pointer[1] = &string[1]; // changed this 

    string[0][0] = 'a'; string[0][1] = 'b'; string[0][2] = '\0'; 
    string[1][0] = 'c'; string[1][1] = 'd'; string[1][2] = '\0'; 

    *pointer[0]++ = *pointer[1]++; 
    *pointer[0]++ = *pointer[1]++; 
    *pointer[0]++ = *pointer[1]++; 

    printf("%s", string2[0]); 

    return 0; 
} 

ポインタの魔法は、上記に変身:

char temp = *pointer[1]; // Read the char from dest. 
    pointer[1]++; // Increment the source pointer to the next char. 
    *pointer[0] = temp; // Store the read char. 
    pointer[0]++; // Increment the dest pointer to the next location. 

、私はそれを3回行う - 入力の各文字について1。それを囲んで、sourcePtr == '\ 0'に対してwhile()チェックを行うと、基本的にstrcpy()に変わります。

デリファレンスは、あなたが期待するかもしれないもう一つの楽しみの例:

typedef struct foo 
{ 
    char mystring[16]; 
} FOO; 

FOO a,b; 

// This does a copy 
a = b; 

// This also does a copy 
FOO *p1 = &a, *p2=&b; 
*p1 = *p2; 

// As does this 
*p1 = a; 

// But this does not and will not compile: 
a.mystring = b.mystring; 

// Because arrays in 'C' are treated different than other types. 
// The above says: Take the address of b.mystring and assign it (illegally because the array's location in memory cannot be changed like this) to a.mystring. 
+0

こんにちは、お返事ありがとうございます。私はただ質問することができますか? * pointer [0] ++ = * pointer [1] ++; * pointer [0] ++ = * pointer [1] ++; * pointer [0] ++ = * pointer [1] ++; ?どのように私はそれを読む必要がありますように。 私の2番目の質問は、ポインタ[1]を&string [1]に変更する必要があるということですか?私はちょうどそれを文字列[1]を介して割り当てることによってそれを行うことができる方法はありますか? ありがとう、 – Herman

+0

ええ、それはちょっと濃密です。これは、システムプログラミングの共通のイディオムです。私はもう少しあなたの答えでそれを解読します。 –

+0

申し訳ありません。私は&string [1]にポインタ[1]を代入する必要があるかどうかという大きな疑問があると思います。 これを唯一の方法で1つずつ割り当てていますか?だから私は文字列全体を割り当てることでそれを行うことはできませんか? 例として、可変ポインタを使用します。 char string = "Hello" char * pointer = string2。 参照ポインタ:*ポインタ=文字列。 これはstring2 helloを正しく割り当てる必要がありますか?私は私のケースで同じことをすることができますか? – Herman

関連する問題