Is there a way to make a function that stores a string, and then call that function in int main() that displays it on the screen? I have been searching a lot and haven't found a clear example. Here is my code. I would like to be able to call it without using the if statement
#include <stdio.h>
/* function declaration */
int StrPrint(char *str);
/* main() function */
int main()
{
char str[] = "The string i am returning n";
int (*ptr)(char *str);
ptr = StrPrint;
if (!(*ptr)(str))
printf("Done!\n");
return 0;
}
/* function definition */
int StrPrint(char *str)
{
printf("%s\n", str);
return 0;
}
The code you've posted is far more complicated than the simple task you're trying to accomplish.
Why not something like this:
#include <stdio.h>
void StrPrint(char* str);
int main(void)
{
char str[] = "The string i am returning n";
StrPrint(str);
return 0;
}
void StrPrint(char* str)
{
printf("%s\n", str);
}
This conflicts slightly with your requirement, in that the function doesn't store a string it just prints out the string passed to it as an argument. But according to the code you posted, this looks like what you're trying to accomplish.
As mentioned by you if you don't like to use pointers it can be done as shown below:
#include <stdio.h>
int StrPrint(char s[])
{
printf("%s\n", s);
return 0;
}
int main()
{
char str[] = "The string i am returning n";
if (!StrPrint(str))
printf("Done!\n");
return 0;
}
Related
Im trying to pass a string like "Hello World" into a a function that looks at each character and prints it (as a base line function for something else) I looked up how to do so and read this post pass char array as argument and while it worked great for one word strings, I can't get it working for two word strings, what can I do to get it working?
#include <stdio.h>
void printer(char *string);
char string[11];
int main(){
scanf("%s", string);
printer(string);
return 0;
}
void printer(char *words) {
for (int i = 0; i < 51; i++) {
printf("%c", words[i]);
}
}
#include <stdio.h>
void printer(char * ptr);
int main()
{
char buff[] = "hello world";
printer(buff);
return 0;
}
void printer(char * ptr)
{
printf("The String is : %s",ptr);
}
So i have a piece of code where i want to store 5 random names no longer than 10 letters each
void printname(char *s);
int main() {
char NAME[10][5];
int NAMECOUNTER=0;
while(NAMECOUNTER<5) {
scanf("%s",NAME[NAMECOUNTER]);
printname(&NAME[NAMECOUNTER]);
NAMECOUNTER++;
}
}
void printname(char *s) {
printf("Hello %s\n",*s);
return;
}
And lets say i want the name to print itself through function Printname. Why does this not work and prints "Hello (null)"?
One problem I see
char NAME[10][5];
Should be
char NAME[5][11];
This way you're declaring 5 name slots, each with 10 char max. The extra space is for the string null terminator.
You have
void printname(char *s) {
printf("Hello %s\n",*s);
return;
}
and you need
void printname(char *s) {
printf("Hello %s\n",s); // Took off the * from the *s
return;
}
because s is already your pointer.
I give 6 glitches to fix. See comments prefacing #<num>
#include <stdio.h>
#include <stdlib.h>
void printname(char *s);
int main() {
char NAME[5][11] = {0}; // #1 value initialize to zero
// #2 the second dimension should be 11, since 10 letters plus null-character
int NAMECOUNTER=0;
while(NAMECOUNTER<5) {
scanf("%10s",NAME[NAMECOUNTER]); // #3 %10s width specifier, limit the length of name up to 10 character long
while ( (ret = getchar()) != '\n') ; // #4 trim exceeding characters
printname(NAME[NAMECOUNTER]); // #5 no &
NAMECOUNTER++;
}
}
void printname(char *s) {
printf("Hello %s\n",s); // #6 no *
return;
}
Your code doesn't even print anything to me, it just freeze and closes. Also it's not so clear.
You are mistyping the "NAME" array inside printname. This function does not have a return value, since it has a void return, so you shouldn't type any return. Also you can save some code lines writing it before main function which is the best practice:
void printname(){
//todo
}
int main(){
return 0;
}
This is a better way to implement your function:
void printname(char Names[5][11], int index){
printf("Hello %s\n", Name[index]);
}
You could replace the while structure with for structure, since it is the best application for the purpose in that case:
Your code piece:
while(NAMECOUNTER<5) {
scanf("%s",NAME[NAMECOUNTER]);
printname(&NAME[NAMECOUNTER]);
NAMECOUNTER++;
}
You don't have to put "&" in the first argument of printname.
Best:
for(NameCounter = 0; NameCounter < 5; NameCounter++){
scanf("%s",NAME[NAMECOUNTER]);
printname(NAME[NAMECOUNTER]);
}
Also make sure to keep your code indentation clear. This is your final code:
void printname(char Names[][11], int index){
printf("Hello %s\n", Names[index]);
}
int main(){
char NAME[5][11];
int NAMECOUNTER;;
for(NAMECOUNTER = 0; NAMECOUNTER < 5; NAMECOUNTER++){
scanf("%s", NAME[NAMECOUNTER]);
printname(NAME, NAMECOUNTER);
}
}
I have tried the following code but am getting an error:conflicting types for fun.Is there any solution that doesn't require the use of malloc.
#include <stdio.h>
int main()
{
printf("%s",fun());
return 0;
}
char* fun()
{
static char str[]="Hello";
return str;
}
It is because you have not declared prototype for fun.
#include <stdio.h>
char* fun(void);
int main()
{
printf("%s",fun());
return 0;
}
char* fun(void)
{
static char str[]="Hello";
return str;
}
C does not allow an array to be returned from a function, but it does allow a struct to be returned from a function. You can define a struct type to hold strings in an array, and return such a struct from your function to be copied into a receiving struct:
#include <stdio.h>
struct String
{
char body[1024];
};
struct String fun(void);
int main(void)
{
struct String my_string = fun();
printf("%s\n", my_string.body);
return 0;
}
struct String fun(void)
{
return (struct String){ .body = "Hello" };
}
char* fun()
{
static char str[]="Hello";
return str;
}
str hold base address of your string. (Assume 1000). Now when you return str, it'll return only base address.
printf("%s",fun());
Here you want to print string so you gave %s but this fun return base address of your character array(string) but not string (as you assume).
First, you need to dereference your fun() in printf so that it'll give first character of string array as str gave base address which points to first character of your string.
Also, you need to give formatter as %c so that it'll give H.
Now to print whole string, you need to increment what's inside your char pointer.
See below Code:
#include <stdio.h>
char* fun();
int main()
{
int i;
for(i=0;i<6;i++){
printf("%c",*(fun()+i));
}
return 0;
}
char* fun()
{
static char str[]="Hello";
return str;
}
Here you can see that I dereference fun() first to print the first character and then I made a for loop so that I can use loop variable i to increment what's inside in pointer returned by fun().
Try and let me know if you face any problem here.
im very new to programming, trying to learn C and cant figure out how to create/use a simple function.
Im trying to create a function called stringtest and then call it into the main and simply make the string strA print ABC.
void stringtest(char strA[20])
{
strA = "ABC";
}
int main()
{
char strA;
stringtest(strA[20]);
printf("This is strA", strA);
return 0;
}
You need to read up on pointers and the C syntax in general.
This is one way you could do it.
#include <stdio.h>
#include <string.h>
void stringtest(char *strA) {
strcpy(strA, "ABC");
}
int main(int argc, const char * argv[]) {
char strA[20];
stringtest(&strA[0]);
printf("This is strA -> %s \n", strA);
return 0;
}
Take care,
/Anders.
I don't think your code ran!!
There are a lot of bugs and errors in your code.
See the code given below to understand how to do this:
#include <stdio.h>
char strA[20];
void stringtest(){
strA[0]='A';
strA[1]='B';
strA[2]='C';
strA[3]='\0';
}
int main(){
stringtest();
printf("This is strA %s",strA);
}
I am trying to use a function with a string as a parameter. I am running into a couple of error messages. First, it says that string[i] is not an array, pointer, or vector, despite the fact that string is a character array. Secondly, it says that I am doing a pointer to integer conversion. Here is my code:
#include <stdio.h>
#include <string.h>
void example (char string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf (string[i]);
}
}
int main (void) {
example("I like pie");
return 0;
}
void example(char string) should be void example(char *string). You declared it to take a character, you want it to take a character pointer or array.
Also, you need to tell printf you are giving it a character: printf("%c", string[i]);.
Your API is wrong it should be
void example (char *string) { // string is a pointer
int i;
size_t n = strlen(string);
for (i = 0; i < n; i++) {
printf ("%c",string[i]); // print character using %c
}
}
Calculate the string length before the loop , calling strlen() in each iteration is not a good idea.
PS: what string points to is read-only you can't modify it
You should use void example(char *string) instead of void example (char string).
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
for (i = 0; i < strlen(string); i++) {
printf ("%c",string[i]);
}
printf("\n");
}
int main (void) {
example("I like pie");
return 0;
}
Your function example just receives a character. To get a string you can use a pointer. Also you can use "%s" format specifier in printf instead of using the for loop and strlen() function.
#include <stdio.h>
#include <string.h>
void example (char *string) {
int i;
printf ("%s\n",string);
}