Click here to Skip to main content
15,887,027 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Tarun Jha3-Jan-18 19:29
Tarun Jha3-Jan-18 19:29 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Victor Nijegorodov3-Jan-18 23:04
Victor Nijegorodov3-Jan-18 23:04 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Tarun Jha4-Jan-18 4:02
Tarun Jha4-Jan-18 4:02 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Richard MacCutchan3-Jan-18 23:21
mveRichard MacCutchan3-Jan-18 23:21 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Tarun Jha4-Jan-18 4:05
Tarun Jha4-Jan-18 4:05 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Richard MacCutchan4-Jan-18 4:10
mveRichard MacCutchan4-Jan-18 4:10 
GeneralRe: How to return a string from a user defined function to main function ? Pin
Tarun Jha5-Jan-18 0:48
Tarun Jha5-Jan-18 0:48 
GeneralRe: How to return a string from a user defined function to main function ? Pin
leon de boer4-Jan-18 14:32
leon de boer4-Jan-18 14:32 
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


modified 4-Jan-18 21:08pm.

GeneralRe: How to return a string from a user defined function to main function ? Pin
Tarun Jha5-Jan-18 0:46
Tarun Jha5-Jan-18 0:46 
QuestionTo make a function in C which does not except "\n" & EOF as input. Pin
Tarun Jha2-Jan-18 2:35
Tarun Jha2-Jan-18 2:35 
AnswerRe: To make a function in C which does not except "\n" & EOF as input. Pin
David Crow2-Jan-18 3:05
David Crow2-Jan-18 3:05 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
Tarun Jha3-Jan-18 3:48
Tarun Jha3-Jan-18 3:48 
AnswerRe: To make a function in C which does not except "\n" & EOF as input. Pin
Richard MacCutchan2-Jan-18 3:36
mveRichard MacCutchan2-Jan-18 3:36 
AnswerRe: To make a function in C which does not except "\n" & EOF as input. Pin
leon de boer2-Jan-18 13:38
leon de boer2-Jan-18 13:38 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
Tarun Jha3-Jan-18 3:40
Tarun Jha3-Jan-18 3:40 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
David Crow3-Jan-18 3:57
David Crow3-Jan-18 3:57 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
Tarun Jha3-Jan-18 4:59
Tarun Jha3-Jan-18 4:59 
QuestionRe: To make a function in C which does not except "\n" & EOF as input. Pin
David Crow3-Jan-18 5:02
David Crow3-Jan-18 5:02 
AnswerRe: To make a function in C which does not except "\n" & EOF as input. Pin
Tarun Jha3-Jan-18 5:45
Tarun Jha3-Jan-18 5:45 
QuestionRe: To make a function in C which does not except "\n" & EOF as input. Pin
David Crow3-Jan-18 5:48
David Crow3-Jan-18 5:48 
AnswerRe: To make a function in C which does not except "\n" & EOF as input. Pin
Tarun Jha3-Jan-18 5:54
Tarun Jha3-Jan-18 5:54 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
David Crow3-Jan-18 6:02
David Crow3-Jan-18 6:02 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
leon de boer3-Jan-18 14:45
leon de boer3-Jan-18 14:45 
GeneralRe: To make a function in C which does not except "\n" & EOF as input. Pin
Richard MacCutchan3-Jan-18 21:54
mveRichard MacCutchan3-Jan-18 21:54 
QuestionC Program to Calculate Difference Between Two Time Periods . Pin
Tarun Jha31-Dec-17 12:27
Tarun Jha31-Dec-17 12:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.