| Previous | Home | Next |
String can be passed to function in similar manner as arrays as, string is also an array. Learn more about passing array to a function.
#include <stdio.h>
void show(char string[]);
int main(){
char c[10]="Hello r4r";
show(c); // Passing string c to function.
return 0;
}
void show(char string[]){
printf("String Output: %s",string);
}
String Output: Hello r4r
Here, string c is passed from main() function to user-defined function show(). In function declaration, string[] is the formal argument.
# Passing Strings to a function using Pointer
#include <stdio.h>
/* function declaration */
void show(char *string[]);
int main(){
char c[10]="Hello r4r";
show(&c); // Passing string c to function.
return 0;
}
/* function definition to show the values */
void show(char *string[]){
printf("String Output: %s",string);
}
String Output: Hello r4r