【发布时间】:2017-08-15 07:05:21
【问题描述】:
正如您在下面看到的,我创建了一个二维字符串数组。我也使用一个名为“缓冲区”的字符数组。我想将缓冲区的值复制到二维数组的 [5][0] 位置。 问题是当缓冲区的值发生变化时,数组单元格的值也会发生变化。 我想保留第一个值。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * strNameList[10][2] = {
{"Luca", "Daniel"} ,
{"Vivan", "Desmond"},
{"Abdul", "Justin"},
{"Nina", "Marlene"},
{"Donny", "Kathlene"}
};
int main()
{
int j, i;
int pos = 5;
char buffer[10204];
strcpy(buffer, "A Value");
strNameList[pos][0] = buffer;
strNameList[pos][1] = "Surname";
for (i = 0; i < 9; i++) {
printf("\n");
for (j = 0; j < 2; j++)
printf(" %s", strNameList[i][j]);
}
strcpy(buffer, "B Value");
for (i = 0; i < 9; i++) {
printf("\n");
for (j = 0; j < 2; j++)
printf(" %s", strNameList[i][j]);
}
}
输出:
Luca Daniel
Vivan Desmond
Abdul Justin
Nina Marlene
Donny Kathlene
A Value Surname
Luca Daniel
Vivan Desmond
Abdul Justin
Nina Marlene
Donny Kathlene
B Value Surname
【问题讨论】:
-
你的问题是什么?
-
那么你的问题到底是什么?请具体说明。
-
我希望第一个值是永久的