コンパイラが理解できる情報の構造全体をコピーしていないため、配列の各要素を個別にコピーする必要があります。通常、これは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.
参照[尋ねる]、および必要な情報を提供しています。あなたの質問は何ですか?注意:配列はポインタではありません!アドレス演算子を削除します。 – Olaf
'strcpy'関数の使い方を尋ねていますか? – Lundin
おそらく '* pointer [0] = string [0];の代わりに' strcpy(string2 [0]、string [0]); –