Passing Strings as Arguments to Functions in C

In summary, the conversation discusses passing a string literal as an argument to a function that takes a pointer to char. It is clarified that this is only allowed if the function takes const char* as its argument. The memory for the string is likely to be placed in the data segment of the executable and converted into a pointer constant that points to the string's location. It is also noted that the string is allocated in a function frame on the stack, but the actual string may be in a different part of memory and treated as read-only.
  • #1
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,199
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
  • #2
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.
 
  • #3
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?
 
  • #4
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)
 
  • #5
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:
  • #6
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.)
 

1. What is the purpose of passing strings as arguments to functions in C?

Passing strings as arguments to functions in C allows for the manipulation and processing of strings within the function. This allows for more efficient and organized code, as the same function can be used to perform operations on different strings.

2. How do you declare a function that takes a string as an argument in C?

To declare a function that takes a string as an argument in C, the function prototype must include the data type "char" before the parameter name. For example:
void function_name(char string[]);

3. Can strings be passed by reference to functions in C?

Yes, strings can be passed by reference to functions in C. This means that the address of the string is passed to the function rather than a copy of the string. This allows for the function to modify the original string directly.

4. How do you pass a string as an argument to a function in C?

To pass a string as an argument to a function in C, the name of the string is placed within the parentheses after the function name. For example:
function_name(string);

5. Are there any limitations to passing strings as arguments to functions in C?

One limitation of passing strings as arguments to functions in C is that the size of the string must be known at compile time. This means that the size of the string cannot be changed within the function. Additionally, the length of the string must be explicitly specified in the function prototype.

Similar threads

  • Programming and Computer Science
Replies
5
Views
871
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
5
Views
804
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
931
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
895
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
26
Views
2K
Back
Top