-1
#include<stdio.h>
#define ASIZE 50
void Reverse(char *str){
    int Asize,i=0;
    char temp;
    // Find the length of the string
    while(*(str+i)!='\0'){
            i++;
    }
    Asize=i;
    // string reverse
    for(i=0;i<(Asize/2);i++){
                temp = *(str+i);
                //may be below is some error with first input method 1
                //but for input method 2 it works perfectly
               *(str+i)  =  *(str+(Asize-(i+1)));
                *(str+(Asize-(i+1))) = temp;
    }
}
int main()
{
    //input method 1. (error aries while i pass the pointer as argument)
    char *s = "abcxyz";
    //input method 2 (works perfectly while as function parameter)
    char s[ASIZE];
    scanf("%s",s);

    Reverse(s);
    printf("%s",s);
}

In the main the input method 1 not working perfectly for the reverse of the string, but the method 2 works. The concept of mine is not clear with the memory representation of char pointer. Maybe I am not good to make the question correctly but Would someone please make me clear why the method 1 is not working. Thanks in advance for the help.

1 Answers1

-1

"abcxyz" is actually a const char[7] type that can decay to a const char* in certain situations.

It is not a char* type; the behaviour on attempting to modify the string is undefined.

char s[ASIZE]; on the other hand is an array of char with automatic storage duration. You are free to modify any element of that as you please.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Oops. I wanted malloc. `char *s = malloc(30); strcpy(s, "abcxyz");` – n.caillou Dec 22 '17 at 08:49
  • 1
    Instead of answering obvious duplicates like this, please cast a close vote for duplicate. You can quickly find a list of canonical ones in the [C tag wiki](https://stackoverflow.com/tags/c/info), FAQ section. Below "Strings" there are two suitable ones that can be used to close this question. – Lundin Dec 22 '17 at 09:02
  • 1
    @Lundin. I disagree on that. The reason here is buried quite deeply in the code the OP has submitted. SO is yet to achieve consensus on what is meant by an "exact duplicate". Personally I close a question as a duplicate if the duplicate target *question* matches. – Bathsheba Dec 22 '17 at 09:06
  • 1
    `char *s = "abcxyz";`... `scanf("%s",s);` hardly counts as deeply buried... . https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha is an exact duplicate of this very common FAQ. – Lundin Dec 22 '17 at 09:12
  • I don't know why people make the negative mark on the question so hurry. I am just thinking to remove my question but got 2 negative already. Yes, I understand later this is a duplicate. People do hurry to make something. sorry to make a duplicate question. – user1957167 Dec 22 '17 at 09:24
  • @Lundin: You and me are clearly polls apart on this. You are coming at this from a position of expertise. The OP is not. On very many levels, the C standard can proxy as the FAQ. I very much doubt the investors in SE would favour closing every C question as a duplicate to that. The question therefore is "where do you draw the line on what's a duplicate?". I draw it on a pretty much exact question match. You don't. And to call this an "obvious duplicate" is little more than hubris. – Bathsheba Dec 22 '17 at 09:36
  • 2
    I wouldn't even comment if this wasn't a black & white case. There is a strong community consensus about what to do here. I shouldn't need to explain how SO moderation works to someone with 141k rep. The OP can read the linked duplicate, quickly see that the code in the question is very similar to what they have, then find over 10 good answers explaining the problem - everything from normative C standard references, in-depth explanations to pedagogic, beginner-friendly ones. There is no need for us to re-invent the wheel 10 times per day, in order to farm reputation. – Lundin Dec 22 '17 at 09:47
  • @user1957167 Sometimes we get down votes for lack of research. Before asking, you can read pretty much any C FAQ and you'll find the answer there. Or you can search Stack Overflow and find hundreds, if not thousands of questions with exactly the same problem. Similarly, you'll find the answer in any C programming book, the chapter about strings. – Lundin Dec 22 '17 at 09:51
  • @Lundin: Your bringing reputation into this argument damages the credibitily of the statements you are making. Factually of course your statement "There is no need for us to re-invent the wheel 10 times per day, in order to farm reputation." is indeed correct. – Bathsheba Dec 22 '17 at 09:52
  • It's ok brother. I am sorry for not doing so. Would u please let me know how i can delete the question now.@Lundin. – user1957167 Dec 22 '17 at 09:53