Home>
Introduction

I was worried about what was written in the title in C language, and when I ran the program, I got an unclear result, so I was wondering what kind of specification it is, so I will ask you a question.

Basic code
#include<stdio.h>
void charrr (char *);
int main (void) {
    char c [5] = "ABC";
    charrr (c);
    puts ("a");
    puts (c);
    return 0;
}
void charrr (char * c) {
    c = "XYZ";
    c [1] = 'O';
}

First, this code compiles successfully.
And when I try it, it exits without executing puts ("a") ;. The cause may be that the pointer that passed the character string is accessed and assigned with a subscript in the function.
Next, comment out c [1] = 0;in the function and try again. Here are the results.

a
ABC


Of course, it's natural that you can't assign a string to an array, but I don't know why the above code would stop executing. I would like to know what specifications of the c language cause this. Thank you.

  • Answer # 1

    According to the C language specification, it is an access violation, so you don't know what will happen or you can't complain about anything.

    Well, I wonder if it will be an extremely correct reaction.

  • Answer # 2

    Assumption 1

    Even the same string literal has different meanings depending on the context.

    char c [5] = "ABC";
    This is the initialization of the array elements.
    Each character is reserved in the rewritable area, so you can replace the elements later.

    char * c = "ABC";
    After securing a character string in a continuous area that cannot be rewritten, the start address is assigned to c.

    Assumption 2

    Within the function charrr, c is just a pointer variable.
    I don't know if there is an array inside.

    void charrr (char * c) {
        c = "XYZ";// This "XYZ" is secured in the non-rewritable area
        c [1] = 'O';
    }
    Main subject
      

    The cause may be that the pointer that passed the character string is accessed and assigned with a subscript in the function.

    The reason is that it is trying to rewrite a non-rewritable area.

      

    You can't assign a string to an array, so of course

    The variable c in the function charrr is just a pointer variable, so the assignment itself succeeds.
    There is another case that does not affect the caller.

    Can you understand that "0" is not displayed when you write as follows.
    For the same reason, rewriting the pointer variable itself does not affect the caller.

    void func (int a) {
        a = 0;
    }
    int main (void) {
        int a = 42;
        func (a)
        printf ("% d \ n", a);// is displayed as 42
        return 0;
    }

  • Answer # 3

      

    You can't assign a string to an array, so of course

    House,charrrWithinc = "XYZ";IscofChanging the pointer valueJust passedchar c [5]TowardsHas no effect..

  • Answer # 4

    "Rewriting a string literal is useless. It's undefined." ← I tried to see what happens

    Or intentionally
    Even if asked, "Why do you stop?"