Passing Strings as Arguments to Functions in C

  • Thread starter Thread starter cepheid
  • Start date Start date
  • Tags Tags
    Functions Strings
Click For Summary

Discussion Overview

The discussion centers around the use of string literals as arguments in C functions that accept pointers to char. Participants explore the implications of passing string literals, memory allocation, and the necessity of using const char* for such cases.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants question whether it is valid to pass a string literal to a function that takes a char pointer, suggesting that it may only work if the function accepts a const char* instead.
  • One participant explains that string literals are typically stored in a constant section of the data segment of the executable and are converted into pointer constants.
  • Another participant emphasizes the need to change the argument type to const char* to properly handle string literals, noting that string literals are arrays of const char, not char.
  • Some participants express differing views on memory allocation, with one suggesting that the string is allocated on the stack, while another clarifies that the pointer is on the stack but the string itself may reside in read-only memory.

Areas of Agreement / Disagreement

Participants exhibit disagreement regarding the memory allocation of string literals and the appropriate function argument type. There is no consensus on the correct approach to handling string literals in function arguments.

Contextual Notes

Participants mention that compilers may allow passing string literals as char* for backward compatibility, but caution against modifying them due to potential undefined behavior.

cepheid
Staff Emeritus
Science Advisor
Gold Member
Messages
5,197
Reaction score
38
Hey everyone!

If I have a function that takes a pointer to char (i.e. char array) as an argument, can I pass a "string literal" (I think that is the right term) as an argument to that function, even if that string hasn't previously been declared anywhere? For example:

Code:
void myfunc(char *message) 
{

  /* function definition goes in here */

}

/* 
.
.
.
and then some time later
*/


myfunc("this is my message");

Is that allowed? I'm wondering where the memory comes from for the string.
 
Technology news on Phys.org
cepheid said:
Hey everyone!

If I have a function that takes a pointer to char (i.e. char array) as an argument, can I pass a "string literal" (I think that is the right term) as an argument to that function, even if that string hasn't previously been declared anywhere? For example:

Code:
void myfunc(char *message) 
{

  /* function definition goes in here */

}

/* 
.
.
.
and then some time later
*/


myfunc("this is my message");
This isn't really legal C -- this only works if your function takes const char* as its argument.


I'm wondering where the memory comes from for the string.
As far as the language is concerned, you're not supposed to know about "memory" -- you're only supposed to know about pointers and arrays. (and even then, you're only supposed to know about pointers that reference objects or pointers that point into dynamically allocated objects) The compiler can do whatever it wants, so long as your function receives a pointer which it can access as a string and see the contents "this is my message".

In reality, the string is probably placed in the data segment of your executable (in a constant section), and the string "this is my message" gets converted into a pointer constant that points into the right location in the data segment.
 
Hurkyl said:
In reality, the string is probably placed in the data segment of your executable (in a constant section), and the string "this is my message" gets converted into a pointer constant that points into the right location in the data segment.

Thanks for the explanation. Just to clarify, what you're saying above is what might happen in the case where I change the data type of the argument to const char*, which I need to do regardless in order to make this work, right?
 
cepheid said:
Thanks for the explanation. Just to clarify, what you're saying above is what might happen in the case where I change the data type of the argument to const char*, which I need to do regardless in order to make this work, right?

What I said is what (generally) happens to string literals.

The need to change the argument of your function is because string literals are arrays of const char, not arrays of char.

(For backwards compatibility, many compilers will let you pass them around as if they weren't const. Maybe the standard does too, I can't remember. But you still better not actually edit them)
 
It is my understanding that the string is allocated in a function 'frame' on the stack whenever you call myfunc("text"). Somebody PM me if I am mistaken.

Stick with char * str unless you want the string to be unedited. The const (basically) make the compiler alert you when you try to change the string.
 
Last edited:
pheeesics said:
It is my understanding that the string is allocated in a function 'frame' on the stack whenever you call myfunc("text").
The pointer to the string will be on the stack, but the string itself (array of chars) is likely to be in some other part of memory, as Hurkyl said, and it may well be in memory to be treated as read-only. (It is up to the compiler/linker to decide exactly where.)
 

Similar threads

Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
6
Views
2K