How to return a string from a user defined function to main function ?
-
#include
#includeint input();
int main()
{
char name[100];printf("Enter your name:\\n"); strcpy(name,input()); printf("\\n%s", name); return 0;
}
int input()
{
char c[100];
gets(c);return(c);
}
i am getting this error while running this code. [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast. I have tried this code. I know I can do it similarly as that of returning an array from a function to main, but that's too long is there any alternative short method.
As per your last post lets fix my code from it into a function .. which is what I assume you are trying to do
int StringInput (char* name, int namemax)
{
int c;
int namelen = 0; // initialize array position index to zero
if (name && namemax > 1) { // Lets be safe and check pointer valid and space > 1
do {
c = getc(stdin); // fetch the integer keyboard read value .. EOF safe
name[namelen++] = (char) c; // convert the integer to a char and place in array .. increment position
} while (c !=EOF && c != '\n' && namelen < namemax-1); // repeat until EOF or new line or hit max length-1 (need space for '\0')
temp.name[namelen-1] = '\0'; // Make C null terminated string (overwrite last EOF or '\n')
}
return(namelen); // return the name length
}/** HOW to USE *//
int main(void)
{
char name[100];
int len = StringInput(&name[0], sizeof(name));
// OR if you don't need length just
StringInput(&name[0], sizeof(name));
}Now if this is for uni or commercial then you probably want this form ... An excercise for you is to understand why and how to do it
int StringInput (const char* name, const int namemax, const FILE* source)
In vino veritas
-
As per your last post lets fix my code from it into a function .. which is what I assume you are trying to do
int StringInput (char* name, int namemax)
{
int c;
int namelen = 0; // initialize array position index to zero
if (name && namemax > 1) { // Lets be safe and check pointer valid and space > 1
do {
c = getc(stdin); // fetch the integer keyboard read value .. EOF safe
name[namelen++] = (char) c; // convert the integer to a char and place in array .. increment position
} while (c !=EOF && c != '\n' && namelen < namemax-1); // repeat until EOF or new line or hit max length-1 (need space for '\0')
temp.name[namelen-1] = '\0'; // Make C null terminated string (overwrite last EOF or '\n')
}
return(namelen); // return the name length
}/** HOW to USE *//
int main(void)
{
char name[100];
int len = StringInput(&name[0], sizeof(name));
// OR if you don't need length just
StringInput(&name[0], sizeof(name));
}Now if this is for uni or commercial then you probably want this form ... An excercise for you is to understand why and how to do it
int StringInput (const char* name, const int namemax, const FILE* source)
In vino veritas
-
Quote:
if (name && namemax > 1)
. why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
Tarun Jha wrote:
Quote:
if (name && namemax > 1)
. why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
You could rewrite it to be:
if (name != NULL && namemax > 1))
that means that name points to some space in memory and the length of this "space" (character array) is > 1. And "it works fine if i remove it" only means that name != NULL and namemax > 1
-
Quote:
if (name && namemax > 1)
. why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
-
Tarun Jha wrote:
Quote:
if (name && namemax > 1)
. why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
You could rewrite it to be:
if (name != NULL && namemax > 1))
that means that name points to some space in memory and the length of this "space" (character array) is > 1. And "it works fine if i remove it" only means that name != NULL and namemax > 1
-
Tarun Jha wrote:
And it works fine if i remove it.
It does not work fine if you remove it because you now have a rather hidden bug in your code. Get hold of a good book on C (as I have repeatedly suggested) and issues like this will become clear.
-
Quote:
if (name && namemax > 1)
. why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
Again Richard MacCutchan is correct so let me explain my code name is a "pointer to a char" NOT a char So this line
if (name && namemax > 1)
Is a shortcut of writing
if ( (name != 0) && (namemax > 1) )
writing
if (anything)
Basically checks if the thing called "anything" is not zero its a shortcut So what the line it is checking is the pointer called name is not 0 (or null which is an invalid pointer) the space for the buffer name is greater than 1 .. why well the code specifically makes an '\0' terminated C string so it needs a minimum buffer size of 1 to hold the '\0' So name == 0 or namemax == 0 would bug the code so they are specifically handled. So the line is a specific safety to make sure you could NEVER bug the routine doing either the function will simply return 0 which is correct it read no character data. There are a couple of other possible background bugs and program hints which was in my hint to change the interface. 1.) The use of "stdin" could be possibly problematic in some esoteric systems .. you don't know it's actually a device there might be no keyboard etc on the system. As such bringing it in thru the interface pass that responsibility out to the caller code. 2.) We don't ever change name or namemax internally so placing a const in front of them makes it clear that is the case and also allows the optimizer to really go to work. So it's not a bug fix but a help to the compiler and anyone using the code. So that is as robust to function input error as I can make your function. You are left with two possible errors outside my control passing in a pointer to some wrong place and passing in a wrong maximum buffer size. I would have to be a mind reader to be able to deal with those but I have dealt with all the obvious possible errors.
In vino veritas
-
Again Richard MacCutchan is correct so let me explain my code name is a "pointer to a char" NOT a char So this line
if (name && namemax > 1)
Is a shortcut of writing
if ( (name != 0) && (namemax > 1) )
writing
if (anything)
Basically checks if the thing called "anything" is not zero its a shortcut So what the line it is checking is the pointer called name is not 0 (or null which is an invalid pointer) the space for the buffer name is greater than 1 .. why well the code specifically makes an '\0' terminated C string so it needs a minimum buffer size of 1 to hold the '\0' So name == 0 or namemax == 0 would bug the code so they are specifically handled. So the line is a specific safety to make sure you could NEVER bug the routine doing either the function will simply return 0 which is correct it read no character data. There are a couple of other possible background bugs and program hints which was in my hint to change the interface. 1.) The use of "stdin" could be possibly problematic in some esoteric systems .. you don't know it's actually a device there might be no keyboard etc on the system. As such bringing it in thru the interface pass that responsibility out to the caller code. 2.) We don't ever change name or namemax internally so placing a const in front of them makes it clear that is the case and also allows the optimizer to really go to work. So it's not a bug fix but a help to the compiler and anyone using the code. So that is as robust to function input error as I can make your function. You are left with two possible errors outside my control passing in a pointer to some wrong place and passing in a wrong maximum buffer size. I would have to be a mind reader to be able to deal with those but I have dealt with all the obvious possible errors.
In vino veritas
-
K&R is probably the easiest introduction to C. If you find it hard then you need to read it through quite a few times.