[英]Can't assign const char* to char*
I'm writing in C and have to return a char* I am trying to replicate the strcpy function. 我正在用C编写并且必须返回一个char *,我正在尝试复制strcpy函数。 I have the following code
我有以下代码
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: How can I assign the dereferenced value at the src to the destination without the program crashing? Q1:如何在程序不崩溃的情况下将src处的解引用值分配给目标?
Q2: If I need to copy the tmp
string entered into a new string, how would I do that? Q2:如果我需要将输入的
tmp
字符串复制到新字符串中,该怎么办? I can't seem pass tmp
as the second parameter 我似乎无法通过
tmp
作为第二个参数
Here 这里
cpyString = strcpy("Sample", cPtr);
^^^^^^^
const
you have swapped the arguments. 您已交换参数。 The first argument is a string literal ("sample") that you are not allowed to write to.
第一个参数是不允许写入的字符串文字(“样本”)。 See https://stackoverflow.com/a/4493156/4386427
参见https://stackoverflow.com/a/4493156/4386427
Try 尝试
cpyString = strcpy(cPtr, "Sample");
I'm not sure that the second line is exactly what you want but at least it is legal. 我不确定第二行是否正是您想要的,但至少是合法的。
Maybe you really want: 也许您真的想要:
cpyStringBuffer[100];
cpyString = strcpy(cpyStringBuffer, cPtr);
In general your code in main
is more complicated than needed. 通常,您的
main
的代码比所需的更为复杂。
Try: 尝试:
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;
}
I guess you might have wanted to code like below. 我想您可能想像下面这样编码。
#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;
}
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;
}
Your program is crashing because you cant modify the pointer to a constant. 您的程序崩溃,因为您无法将指针修改为常量。 Please find the corrected code below:
请在下面找到更正的代码:
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;
}
Note that in the main function if you use char a = "abc"
, then it will cause problem because its a pointer to a constant 请注意,在主函数中,如果使用
char a = "abc"
,则会引起问题,因为其指向常量的指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.