It shows nothing when you pass the string to the function
int main(void){
char *string[200];
getChar(string);//starts function
printf("This is the string %s",*string);//prints
return 0;
}
Void getChar(char *String[200]){
scanf(" %s",String[200]);//gets string
}
There are multiple problems:
char instead of an array of char *,scanf():Void has no capital: voidgetChar is confusing to read a string and should be declared or defined before use.scanf(" %s", is redundant: %s already skips initial spaces.scanf() the maximum number of characters to store into the destination array, otherwise you will have undefined behavior if the input has too many characters.Here is a modified version:
#include <stdio.h>
int getword200(char *buf) {
return scanf("%199s", buf);
}
int main() {
char word[200];
if (getword200(word) == 1)
printf("This is the string: %s\n", word);
return 0;
}
The above function assumes the array has a length of at least 200. It would be more general to pass the actual array length and modify the code to handle any length:
#include <limits.h>
#include <stdio.h>
int getword(char *buf, size_t size) {
char format[32];
int length;
if (size == 0)
return NULL;
if (size == 1) {
*buf = '\0';
return buf;
}
if (size > INT_MAX)
length = INT_MAX;
else
length = size - 1;
snprintf(format, sizeof format, "%%%ds", length)
return scanf(format, buf);
}
int main() {
char word[200];
if (getword(word, sizeof word) == 1)
printf("This is the string: %s\n", word);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With