将指针数组分配给字符串
Assigning array of pointers to string
我试过在网上搜索,但没有找到与我想做的一样的东西。希望你能帮帮我。
问题:我有 2 个字符数组,即 string
和 string2
。两者最初都是空的。然后我给这些字符串 2 个输入。我想使用指向 string2 的指针在字符串中分配值。
这是我的代码:
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,但我正在尝试学习使用指针来做到这一点。在这方面的任何帮助都会很棒。
因为您没有复制编译器可以理解的整个信息结构,所以您需要单独复制数组的每个元素。通常这是通过 for 循环检查 NUL 或大小来完成的,但我在作弊,只是向您展示了执行所需副本的语法:
#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] = '[=10=]';
string[1][0] = 'c'; string[1][1] = 'b'; string[1][2] = '[=10=]';
// 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] = '[=11=]';
string[1][0] = 'c'; string[1][1] = 'd'; string[1][2] = '[=11=]';
*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 次 - 每个输入字符一次。用 while() 围绕它检查 sourcePtr == '\0' 基本上把它变成 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.
我试过在网上搜索,但没有找到与我想做的一样的东西。希望你能帮帮我。
问题:我有 2 个字符数组,即 string
和 string2
。两者最初都是空的。然后我给这些字符串 2 个输入。我想使用指向 string2 的指针在字符串中分配值。
这是我的代码:
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,但我正在尝试学习使用指针来做到这一点。在这方面的任何帮助都会很棒。
因为您没有复制编译器可以理解的整个信息结构,所以您需要单独复制数组的每个元素。通常这是通过 for 循环检查 NUL 或大小来完成的,但我在作弊,只是向您展示了执行所需副本的语法:
#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] = '[=10=]';
string[1][0] = 'c'; string[1][1] = 'b'; string[1][2] = '[=10=]';
// 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] = '[=11=]';
string[1][0] = 'c'; string[1][1] = 'd'; string[1][2] = '[=11=]';
*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 次 - 每个输入字符一次。用 while() 围绕它检查 sourcePtr == '\0' 基本上把它变成 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.