0

While initializing a const char array, I tried to change the string and I was able to change it without any issue.

I was learning how to initialize a const char array. I think I am doing some mistake here which I am not able to find.

int main(int argc, char const *argv[])
{

    const char *strs[10];
    strs[0] = "wwww.google.com";
    printf("%s\n", strs[0]);
    strs[1] = "https://wwww.google.com";
    strs[0] = "ss";
    printf("%s\n", strs[0]);
    return 0;
}

Output:

1st init: wwww.google.com
2nd init: ss -> Here, I expect it to throw error

5
  • 2
    The pointers you’re setting (not initializing) are not constant; the data they point at can’t be modified via the pointers, though. You don’t try to modify the strings; all is well. Commented Jul 14, 2019 at 7:33
  • If the pointers were constant, your assignments would generate compiler warnings. Commented Jul 14, 2019 at 7:36
  • 1st init. Nah, the pointers are default initialized first. And then you assign for a second time with "wwww.google.com". Commented Jul 14, 2019 at 7:38
  • const char *strs[10]; defines strs as an array of ten pointers to constant characters. The array itself isn't constant, and neither is it an array of characters. Commented Jul 14, 2019 at 7:39
  • 2
    Incidentally, those are assignments, not reinitializations. Initialization is something that happens when an object is created, and you cannot do it more than once. Commented Jul 14, 2019 at 11:24

3 Answers 3

3
const char* s = "Hi";

tells the compiler that the content that the pointer points to is constant. This means that s[0] = 'P'; will result in a compilation error. But you can modify the pointer. On the other hand,

char* const s = "Hi";

tells the compiler that that the pointer is constant. This means that s = "Pi"; will result in a compilation error. But no compilation error will be thrown when you try to modify the string*

Your code depicts the former behaviour, not the latter as you seem to have thought


* Modifying string literals will invoke Undefined Behaviour

Sign up to request clarification or add additional context in comments.

Comments

2
const char *strs[10];

strs is an array of 10 pointers to const char. You can change the pointers; you cannot change the chars

strs[2] = NULL; // ok: change the pointer
strs[0][0] = '#'; // wrong; cannot change the char

Maybe try

const char * const strs[10] = {"www.google.com",
                               "https://www.google.com",
                               "www.google.com/",
                               "https://www.google.com/",
                               NULL, NULL };

which makes strs an array of 10 read-only pointers to const char. You cannot change the pointers after initialization.

Comments

0

To put this in simple English(not necessarily 100% accurate but serves to conceptualise), this

const char *strs[10];

initialises a contant array strs which contains none constant elements. Thus, the elements in the array can be changed but the array itself cannot be changed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.