-1

trying to copy one char pointer to another but getting segmentation fault, cant really see whats going on. I also tried doing q = my_strcpy(q, p) but doesnt work. Any help is appreciated.


 #include <stdio.h>
  char *my_strcpy(char *destination, const char *source);
  int main()
   { 
        char *p = "Avani";
        char *q = "";
        my_strcpy(q, p);
        printf("%s", q);
        return 0;
   }
   char *my_strcpy(char *destination, const char *source)
     {
       char temp = *source;

         while (temp != '\0')
            {
                *destination = temp;
                // Increment the pointers, so that they point to the next indexes
                source++;
                destination++;
                // Reassign value of temp to the next value
                temp = *source;
            }
            // Copy the null terminator
            *destination = '\0';

            // Return pointer to the destination
            return destination;
        }
  • 3
    You've not allocated enough space for writing to `q` in `main()`. Further, the space that's there is a string literal. You can't modify string literals — trying to do so frequently causes trouble. Use `char space[32]; char *q = space;` and your code stands a chance of working. – Jonathan Leffler May 19 '20 at 05:03
  • Notice, though, that if things work, you return a pointer to the null byte at the end of the string, so when you print the string in `main()`, there's nothing to print. You forgot to add a newline to the output too. – Jonathan Leffler May 19 '20 at 05:05
  • That's a very long loop. The classic implementation would be `while ((*destination++ = *source++) != '\0') ; return destination - 1;` (three lines, the middle one with just a semicolon on it). There are those who would not write `!= '\0'` — you could omit it, but my compiler then warns about an assignment in a condition, so you have to add some characters to say "no, it's not just an assignment", and I think the `!= '\0'` is a lot clearer than doubling up the parentheses, which also works. YMMV. – Jonathan Leffler May 19 '20 at 05:09
  • 1
    Or you could use: `while ((*destination = *source++) != '\0') destination++; return destination;`. That works too. – Jonathan Leffler May 19 '20 at 05:10

1 Answers1

0

Just try char *q; This is your problem

onur
  • 47
  • 4
  • 1
    No, that is not the problem. The problem is that the pointer isn't pointing at valid read/write memory. – Lundin May 19 '20 at 10:27
  • if you change the char *q = ""; to "char *q; program will works correctly. – onur May 19 '20 at 10:39
  • No. How will changing to an uninitialized pointer solve anything for the better? – Lundin May 19 '20 at 10:44
  • when we define uninitialized pointer we make a wild pointer, it is bad practise ı accept that but in function we are not equalize an integer, we point to different address which is a temp and it is not become a wild pointer now. So if we compile and run with this code, it can really works. – onur May 19 '20 at 10:58
  • It's not just bad practice, it is 100% incorrect practice. The temp variable in the posted function is not related to the destination, but the source. If no memory has been allocated, it's just plain wrong. See https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-read-to-an-uninitializ – Lundin May 19 '20 at 11:00
  • I am not recommended too but in this case and in this code we are doing this like with temp,source, destination. We are not try to equalize wild pointer to random number or adressless strings. If we try to equalize wild pointer to 8 for example it is very dangereous. But we are not. So i am not saying it just fine but it works. – onur May 19 '20 at 11:12
  • It also works to drag my old sofa into the middle of the highway during rush hour and store it in the middle lane from now on. I didn't get hit by a car and neither did the sofa, so "it works" and storing my sofa in the middle of the highway must therefore be correct. I will now teach others on the Internet to do the same. – Lundin May 19 '20 at 11:25