4

I think this question is an extension of this SO answer. Say I have the following code:

#include <stdio.h>
#include <string.h>

void func(char *str)
{
    strcpy(str, "Test");
}

int main()
{
    char testStr[20] = "Original";
    func(testStr);
    printf("%s\n", testStr);  /* Prints "Test" followed by a new-line */
    return 0;
}

By my understanding, shouldn't func expect a pointer to a read-only literal as an argument? Whereas, what is being passed is a copy on the stack of a read-only literal.

Even though this yields correct results, is doing this 100% correct? Would it improve readability of code if func() accepted char [] instead of char *?

Community
  • 1
  • 1
Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63

2 Answers2

8

Your understanding is correct. The code is fine.

The following, on the other hand, isn't:

void func(char *str)
{
    strcpy(str, "Test");
}

int main()
{
    char* testStr = "Original";
    func(testStr);
}

This attempts to modify a string literal, resulting in undefined behaviour.

As to the question of readability, that's subjective.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

All strings are pointers to arrays of char, so it makes no difference. There's no copy of the string allocated on the stack in the function, it's the same one as passed, and declaring the parameter as an array doesn't allocate any storage for the string in the function, it's just another way of writing the same thing.

However, if you initialise an array with a string literal, a copy of the literal is made into the array. It doesn't matter whether the string came from a literal, as it's entirely separate.

If you're expecting a literal you can declare the function parameter as const if you want to, but there's certainly no requirement. In C string literals are not read only, but it'll cause undefined behaviour to change them.

teppic
  • 8,039
  • 2
  • 24
  • 37