【发布时间】:2012-09-25 18:30:14
【问题描述】:
我有一个函数void copy(char *temp,char input[length])。我需要做的是将 temp 的值复制到输入数组中,但从输入数组的末尾开始。
我没有正确地提出问题。 这就是我想要做的。
我输入了长度为 20 的数组。我得到了一个值为“test”的临时数组。然后我想在 input[19]=t,input[18]=e 等的输入数组中复制 temp。
现在当我再次调用该函数时,如果我想将 xyz 复制到输入数组,那么 input[15]=x,input[14]=y,input[13]=z。
我想这样做,直到我用所有值填充输入数组。
void copy(char *temp,char input[])
这是函数定义。
现在让我们看看我们必须做什么,
char* temp={"abcde"};
char input[100]={};
//I want to move data from temp into input array such that
printf("%c",input[95]); // gives output as a
printf("%c",input[96]); // gives output as b
这是我到目前为止写的。
char* copy(char* ptr,char data[])
{
int start=sizeof(data)/sizeof(char)-strlen(data);
int end=start-strlen(ptr);
int j=0;
int counter=0;
for(counter=start;counter>end;counter--)
{
data[counter]=ptr[j++];
}
printf("%s",data);
return data;
}
当我调用这个函数时,我会以这种方式得到另一个错误,
char data[100]={};
char* temp={"abcde"};
char* output=copy(temp,data);
data=output;
对于 data=output 行,将“char*”分配给“char [100]”时的类型不兼容
【问题讨论】:
-
What have you tried? 特别是,您需要一个额外的参数,以便您知道数组的长度是多少。或者如果数组包含一个以 NULL 结尾的字符串,您可以使用 strlen() 来获取 c 字符串的长度。
-
不,这不是家庭作业。我正在尝试从数组末尾开始存储一个临时变量并将数据复制到临时数组的长度
-
从头开始的意思是说
temp的内容在input中反转? -
定义一个合适的函数接口,然后添加一个带有输入和输出的用例(代码)。
-
@DavidRodríguez-dribeas 进行了更多更改,希望对您有所帮助