我用C写作并且必须返回一个字符* 我试图复制strcpy函数。我有以下代码
int main()
{
char tmp[100];
char* cpyString;
const char* cPtr = &tmp[0];
printf("Enter word:");
fflush(stdin);
scanf("%s", &tmp);
cpyString = strcpy("Sample", cPtr);
printf("new count is %d\n", strlen(cpyString));
}
int strlen(char* s)
{
int count = 0;
while(*(s) != 0x00)
{
count++;
s = s+0x01;
}
return count;
}
char* strcpy(char* dest, const char* src)
{
char* retPtr = dest;
int i =0;
int srcLength = strlen(src);
for(i = 0; i< srcLength; i++)
{
*(dest) = *(src); //at this line program breaks
dest = dest + 0x01;
src = src + 0x01;
}
*(dest) = 0x00; //finish with terminating null byte
return retPtr;
}
Q1:如何在没有程序崩溃的情况下将src上的解除引用值分配给目标?
Q2:如果我需要将输入的tmp
字符串复制到新字符串中,我该怎么做?我似乎无法将tmp
作为第二个参数
答案 0 :(得分:6)
下面
cpyString = strcpy("Sample", cPtr);
^^^^^^^
const
你已经交换了参数。第一个参数是不允许写入的字符串文字(“sample”)。见https://stackoverflow.com/a/4493156/4386427
尝试
cpyString = strcpy(cPtr, "Sample");
我不确定第二行是你想要的,但至少它是合法的。
也许你真的想要:
cpyStringBuffer[100];
cpyString = strcpy(cpyStringBuffer, cPtr);
一般来说,main
中的代码比需要的更复杂。
尝试:
int main()
{
char input[100] = {0};
char dest[100];
printf("Enter word:");
scanf("%99s", input); // notice the 99 to avoid buffer overflow
strcpy(dest, input);
printf("new count is %d\n", strlen(dest));
return 0;
}
答案 1 :(得分:1)
我想你可能想要编码如下。
#include <stdio.h>
int strlen(char* s);
char* strcpy(char* dest, char* src);
int main()
{
char tmp[100];
char cpyString[100];
printf("Enter word:");
fflush(stdin);
scanf("%s", &tmp);
strcpy(cpyString, tmp);
printf("new count is %d\n", strlen(cpyString));
}
int strlen(char* s)
{
int count = 0;
while(*(s) != 0x00)
{
count++;
s = s+0x01;
}
return count;
}
char* strcpy(char* dest, char* src)
{
char* retPtr = dest;
int i =0;
int srcLength = strlen(src);
for(i = 0; i< srcLength; i++)
{
*(dest) = *(src); //at this line program breaks
dest = dest + 0x01;
src = src + 0x01;
}
*(dest) = 0x00; //finish with terminating null byte
return retPtr;
}
答案 2 :(得分:0)
I think you used non initialized destination and literal string pointer. You have to declare your destination as a buffer like
char dest[const_size]
So
char* strcpy(char* dest, const char* src)
{
char* retPtr = dest;
int i =0;
int srcLength = strlen(src);
for(i = 0; i< srcLength; i++)
{
*(dest) = *(src); //at this line program breaks
dest = dest + 0x01;
src = src + 0x01;
}
*(dest) = 0x00; //finish with terminating null byte
return retPtr;
}
int main()
{
char *arr="xxxxxx";
char *dest="fffff"; // this won't work because you can not modify const string
char *dest_1; // this won't work because it is uninitialized pointer
char dest_2[50]; // this will work fine
strcpy(x, y);
printf("%s",x);
//x still the same as point pointer
return 0;
}
答案 3 :(得分:-1)
您的程序崩溃了,因为您无法修改指向常量的指针。请在下面找到更正后的代码:
char *
mstrcpy (char *dest, const char *src)
{
char *retPtr = dest;
int i = 0;
int srcLength = strlen (src);
for (i = 0; i < srcLength; i++)
{
*(dest) = *(src); //now doesn't break at this line
dest = dest + 1;
src = src + 1;
}
*(dest) = 0x00; //finish with terminating null byte
return retPtr;
}
int
main ()
{
//char a = "abc"; // will cause crash
char a[] = "abc"; // won't crash
char *b = "xyz";
mstrcpy(a,b); //works fine !!!!
return 0;
}
请注意,在main函数中,如果使用char a = "abc"
,那么它会导致问题,因为它是指向常量的指针