-4

Consider the following code, which works fine asusual for copying string

#include<stdio.h>

void strcp(char *s1, char *s2);

int main() {

    char s1[]="Hai, I am a good boy";                   //1
    char s2[]="Really I am a good boy";                 //2

    strcp(s1,s2);

    printf("%s",s1);
    return 0;
}

void strcp(char *s1, char *s2) {

    while(*s1++=*s2++);
}

What changes I have to made if I want to declare s1, s2 as pointers to char inside main()?

char *s1="Hai, I am a good boy";                   //1
char *s2="Really I am a good boy";                 //2

I tried to copy pointer values as shown below

void strcp(char *s1, char *s2) {

s1=s2;
}

But it didn't work.

hanugm
  • 1,127
  • 3
  • 13
  • 44
  • 3
    Did you try anything? – Oliver Charlesworth Jun 07 '17 at 09:36
  • I tried the same by declaring chars, but since the char pointers has no capability of changing contents inside locations, it is showing error....... – hanugm Jun 07 '17 at 09:38
  • with `while(*s1++=*s2++)` you are actually writing out of the bounds of `s1` (`s2` is larger than `s1`) – JFMR Jun 07 '17 at 09:38
  • Is this question substandard? – hanugm Jun 07 '17 at 09:39
  • 2
    Will be: segmentation fault. You can't modify literal strings, that are const – LPs Jun 07 '17 at 09:39
  • So, its impossible to copy strings when they are declared as char pointers? – hanugm Jun 07 '17 at 09:40
  • No, in the way you posted. But you can, obviously, switch pointers values. – LPs Jun 07 '17 at 09:41
  • I think @OliverCharlesworth's comment pretty much sums it up; this question doesn't show any attempt at doing what you are asking and seeing what the results are. Sometimes the best way to learn is to take what you have, make a small change, see what breaks (whether in terms of compilation errors or changed software behavior; the latter hopefully indicated by automated tests), fix that breakage, rinse and repeat until you are at where you want to be. Hence "this question does not show any research effort". – user Jun 07 '17 at 09:43
  • Your latest iteration modifies the value of the **local** pointer variable s1, since you pass the pointers by value. On the subject of modifying string literals, see https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s – Ilja Everilä Jun 07 '17 at 09:49
  • Yeah, but if I change the actual values, error happens – hanugm Jun 07 '17 at 09:50
  • Possible duplicate of [What is the difference between char s\[\] and char \*s?](https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s) – Antti Haapala -- Слава Україні Jun 07 '17 at 09:52
  • 3
    *Both* fragments are wrong and exhibit undefined behaviour. There is no point in learning C by experimentation. – Antti Haapala -- Слава Україні Jun 07 '17 at 09:54

4 Answers4

2
void strcp(char **s1, char **s2) {

*s1=*s2;
}

but it does not copy anything. After this s1 & s2 in main will point to the same memory location.

0___________
  • 60,014
  • 4
  • 34
  • 74
1

Both versions, even the one you state that it works fine, yield undefined behaviour, which may become apparent as a segmentation fault. Generally, accessing memory requires the addressed memory to be valid, i.e. properly allocated. This is particularly true for writing to memory, as any sort of strcpy does.

In the first case, you define a variable char s1[]="Hai, I am a good boy", which implicitly reserves a memory block of about 21 one bytes and copies the string literal provided into it. If you now copy another string into s1, and the other string is longer than 21 bytes, you actually address memory that is not reserved for s1, thereby yielding undefined behaviour.

In the second case, when you define variable char *s1="Hai, I am a good boy", then no memory block to which you may write is reserved, but s1 will rather point to a memory block in a non-writeable data segment where literal "Hai, I am a good boy" resides. Copying something to this address again yields undefined behaviour, yet for a different reason.

So allocate memory for s1, e.g using malloc, and then copy content to it.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1

First of all you can't modify literal strings, that are const. Second, your function strcp() doesn't care if you pass a pointer to characters or an array of characters, it should work in both cases.When you type just s1 the array is converted in to a pointer to char which points to the first element of the array.

s1 -->(char*)&s1[0]

Since you can't modify literal strings what you can do is just switch the pointers:

void strcp(char **s1, char **s2) { 
char *temp = *s1;
       *s1 = *s2;
       *s2 = temp;
}
Martin Chekurov
  • 733
  • 4
  • 15
-2
#include<stdio.h>

void strcp(char *s1, char *s2);

int main() {
    char *s1 = "Hi, I am a good boy";                   //1
    char *s2 = "Really!! I am a good boy";              //2

    strcp(&s1,&s2); //Pass Address to function 

    printf("s1 = %s", s1);
    printf("\ns2 = %s", s2);

    return 0;
}

/*
 * This method takes pointer address as arguments and simply swap the address 
 * pointing to string (i.e. char array).
 */
void strcp(char *s1, char *s2) { 
    char *temp = *s1;
           *s1 = *s2;
           *s2 = temp;
}

O/P :

s1 = Really!! I am a good boy
s2 = Hi, I am a good boy

http://ideone.com/wTH4VO

roottraveller
  • 7,942
  • 7
  • 60
  • 65
  • both versions are invalid. The first one on some systems will generate segmentarion fault (as literals are stored in the read only segments), will do nothng (ARMs for example) where writing to the flash does nothing, or on some systems where access to the read only memories is complicated (8-bit AVR) will work ok asthe literals are copied to the RAM. The second example does nothing. it only swaps the local (in the strcpy scope) variables which will vanish on function return. Both are bad. You will receive the warnings and eventually an error as *s1 is char not char *. – 0___________ Jun 07 '17 at 10:07
  • @PeterJ `*s1 is char not char *` ?? `*s1` can never be of type char. and swaping the memory address that the whole point of using pointer. – roottraveller Jun 07 '17 at 10:17
  • 2
    your function call _strcp(&s1,&s2);_ is incompatible with the function parameters _void strcp(char *s1, char *s2) {_ you pass char ** (and you will get a warning) but in the function they will be treated as char * only. So _*s1 = *s2;_ will only store one character in the location pointed by s1 destructing the original pointer. this is a classical UB. Expalaining: s1 is char *, *s1 is char and &s1 is char ** – 0___________ Jun 07 '17 at 10:22