-5

I wanted to know in what ways a string can be passed to a function and what is the corresponding syntax of the function declaration.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    C or C++? Two different languages. And Google don't bite – Jorengarenar Aug 14 '20 at 08:15
  • 2
    Read the chapter dealing with strings in your beginner's C text book. It's explained in details there. – Jabberwocky Aug 14 '20 at 08:18
  • Unless you wrap the string inside a struct you can only pass it by passing a pointer to the first character: `foo("string") /*pointer to 's'*/;` or `struct Bar { char foo[10]; }; struct Bar bar; strcpy(bar.foo, "abcdefghi"); call(bar) /*string wrapped inside strut*/;` – pmg Aug 14 '20 at 08:19
  • Othwerwise googling your verbatim question title should also show what you need. – Jabberwocky Aug 14 '20 at 08:20

4 Answers4

0

In C there is no type called string, it is an array of char. You can pass it to the function by passing the address of the first element and the size of the array or you will depend on the terminating character '\n'.

To define a string

char str[] = "A String";

the above line is stored in memory as: enter image description here

pass it to a function

To pass the array the function prototype must be as:

returnType funName(char * str);
  • note that returnType can be any type

Function Call will be like:

funName(&str[0]); or funName(str);

Manipulation inside the function (suppose you want to print the string character by character)

returnType funName(char * str) {
  uint8_t loopIndex = 0;
  while(str[loopIndex] != '\n') {
    printf("%c", str[loopIndex]);
  }
  return whatEver;
}
  • note that you can pass the size of the array to the function as a parameter
Ahmed Yasen
  • 145
  • 2
  • 13
0

In C, strings are nothing but an array of characters. Below is the example

void myfun(char *str) {    // collecting in a pointer
printf("%s", str);
}

int main() {
char str[] = "Independance" ;  
myfun(str);                // Passing address of an array
return 0;
}
0

In C, strings aren't passed by value to a function. They are passed by reference through a pointer to char.

Related:

Function declaration:

void foo (char *p_s);

p_s is a pointer to char.

If you don't want to modify the string passed by reference you can define p_s as const char * p_s. If you don't want the pointer to be changed to point to anywhere else except the beginning of the string in the caller, you can define p_s as char * const p_s. If you want both you can declare p_s as char const * const p_s.

Function call:

const char s[] = "Hello World!";

foo(s);
0

In C, a string is a sequence of character values including a zero-valued terminator - the string "hello" is represented as the sequence {'h', 'e', 'l','l', 'o', 0}.

Strings (including string literals) are stored in arrays of character type:

char str[] = "hello";

Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration (like above), an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T" and the value of the expression will be the address of the first element of the array.

So if you call a function like

foo( str );

what the function actually receives is a char *, not an array of char, so the prototype will be

void foo( char * );           // declaration
void foo( char *str ) { ... } // definition

In the context of a function parameter declaration, T a[N] and T a[] are adjusted to T *a, so you could also write the prototype as

void foo( char str[] )

or

void foo( char str[N] )

but they'll both be interpreted as

void foo( char *str )
John Bode
  • 119,563
  • 19
  • 122
  • 198