简体   繁体   中英

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. 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?

Q2: If I need to copy the tmp string entered into a new string, how would I do that? I can't seem pass tmp as the second parameter

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

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.

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;
}
  1. When you invoke your strcpy() in the main function, arguments src and dest are reversed.
  2. if you want to use the variable cpyString, then you are supposed to determine which to allocate pieces of memory from either static or dynamic.
    • In my example, I declared cpyString as an array of characters. Which means that the variable will occupy static memory partly.
    • You can also alternatively allocate bytes of dynamic memory to it by calling malloc() or calloc() function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM