forked from inspirezonetech/TeachMeCLikeIm5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from RR-08/RR-08-patch-1
passing-string-to-function
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
------------------------------------------------------------------------------------ | ||
Strings can be passed to a function in a similar way as arrays. | ||
To pass a one dimensional string to a function as an argument we just write the name of the string array variable. | ||
In the following example we have a string array variable str and it is passed to the function. | ||
Method 1: Pass by value / without using pointers | ||
void function(char str[]); | ||
char str[50]; | ||
function(str); // Passing string to a function. | ||
Method 2: Using Pointers | ||
void function(char* str); | ||
char str[50]; | ||
function(str); // Passing string to a function. | ||
------------------------------------------------------------------------------------ | ||
*/ | ||
|
||
// Code here explaining concept with comments to guide | ||
#include <stdio.h> | ||
void sayWelcome(char str[]); // Passing string as reference | ||
void sayGoodbye(char* str); // Passing string as a pointer variable | ||
|
||
int main() | ||
{ | ||
char str[50]; | ||
printf("Enter string: "); | ||
fgets(str, sizeof(str), stdin); | ||
sayWelcome(str); // Passing string to function | ||
sayGoodbye(str); | ||
return 0; | ||
} | ||
void sayWelcome(char str[]) | ||
{ | ||
printf("Welcome, "); | ||
puts(str); | ||
} | ||
void sayGoodbye(char* str){ | ||
printf("Good Bye, "); | ||
puts(str); | ||
} | ||
/* | ||
------------------------------------------------------------------------------------ | ||
Challenge: Create a user defined function which accepts string as argument and counts the total number of character in the inputed string. | ||
------------------------------------------------------------------------------------ | ||
*/ |