Register to reply

Passing Strings as Arguments to Functions in C

by cepheid
Tags: arguments, functions, passing, strings
Share this thread:
cepheid
#1
May21-11, 04:24 PM
Emeritus
Sci Advisor
PF Gold
cepheid's Avatar
P: 5,196
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:


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.
Phys.Org News Partner Science news on Phys.org
Wildfires and other burns play bigger role in climate change, professor finds
SR Labs research to expose BadUSB next week in Vegas
New study advances 'DNA revolution,' tells butterflies' evolutionary history
Hurkyl
#2
May21-11, 04:39 PM
Emeritus
Sci Advisor
PF Gold
Hurkyl's Avatar
P: 16,092
[QUOTE=cepheid;3314968]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:


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.
cepheid
#3
May21-11, 04:57 PM
Emeritus
Sci Advisor
PF Gold
cepheid's Avatar
P: 5,196
Quote Quote by Hurkyl View Post
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?

Hurkyl
#4
May21-11, 05:54 PM
Emeritus
Sci Advisor
PF Gold
Hurkyl's Avatar
P: 16,092
Passing Strings as Arguments to Functions in C

Quote Quote by cepheid View Post
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)
pheeesics
#5
May21-11, 08:49 PM
P: 8
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.
DrGreg
#6
May21-11, 09:53 PM
Sci Advisor
PF Gold
DrGreg's Avatar
P: 1,843
Quote Quote by pheeesics View Post
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.)


Register to reply

Related Discussions
Converting open strings to closed strings Beyond the Standard Model 4
Fortran functions as arguments of functions Programming & Computer Science 2
Integral Involving Trigonometric Functions with Varying Arguments Calculus & Beyond Homework 5
Limits of Arguments of Functions Calculus 16
Fortran 90/95 : passing parameters to functions Programming & Computer Science 4