Need help on C language....


Status
Not open for further replies.

kitkat

New Member
Mar 5, 2005
568
0
0
#21
Actually for C, the proper way should be to use "malloc" first. I also rusty liao :bsmilie:
Yes... that is what I am about to point out...

malloc ... nostalgic juz remember to "free" :bsmilie:
 

Convoy

New Member
Aug 2, 2003
75
0
0
SG
Visit site
#26
Eh, that piece of code posted by threadstarter is correct. I've tried it out on my computer and it works. There is no need to explicitly return n back to main(). The array parameter for getname() is converted into a pointer parameter by the compiler, so it is pass-by-reference.
 

Static

New Member
Jul 5, 2006
1,079
0
0
Surfing soft porn in CS
#27
if that code is recursive good luck. hehehe :)

void getname( char[])
{
getname( char);
}
:bigeyes:

u get an error when u compile this..


To my understanding.. the 'return' is to return back to the previous command it is executing... Not to return any value, int or char......

#include <stdio.h> : declares standard i/o library

void getname (char n[20]); : declares a function call "getname" with char n
of 20 characters ( not string )


main() : main body
{
char name[20]; : declares local variable char "name" of 20 characters

getname(name); : runs function "getname" with variable name

printf("your name is %s\n", name); : print out input variable "name" and print new line

return(0); : end prog.}

void getname (char n[20]) : function "getname" with variable of 20 character max
{
printf("pls enter name: "); : obvious

gets(n); : get input as string:variable'n' is throw back as "name"

return; // <====== need to return? : how else to end a function than return ??
}


if not wrong... the sentence will stick all together...

pls enter name: Simonyour name is Simon


correct me if wrong.

TS u revising for ur job interview ?
 

Convoy

New Member
Aug 2, 2003
75
0
0
SG
Visit site
#28
:bigeyes:

if not wrong... the sentence will stick all together...

pls enter name: Simonyour name is Simon


correct me if wrong.
You have to press Enter after you entered the name, so there will be a newline.
 

Static

New Member
Jul 5, 2006
1,079
0
0
Surfing soft porn in CS
#29
You have to press Enter after you entered the name, so there will be a newline.
Oh.. tot gets will not recognize "ENTER" as a valid character so it would just terminate gets ?
it would print as a new line ? :think:

Wahaha too lazy to test it out.. got to dig up my C++ installer...
 

Convoy

New Member
Aug 2, 2003
75
0
0
SG
Visit site
#30
Oh.. tot gets will not recognize "ENTER" as a valid character so it would just terminate gets ?
it would print as a new line ? :think:

Wahaha too lazy to test it out.. got to dig up my C++ installer...
No, it won't get printed as a newline. I mean when you are entering the name "Simon" at the prompt, you have to press Enter to get gets() to read in the characters. That will cause the cursor to move to the next line.
 

#31
:bigeyes:

u get an error when u compile this..


To my understanding.. the 'return' is to return back to the previous command it is executing... Not to return any value, int or char......

#include <stdio.h> : declares standard i/o library

void getname (char n[20]); : declares a function call "getname" with char n
of 20 characters ( not string )


main() : main body
{
char name[20]; : declares local variable char "name" of 20 characters

getname(name); : runs function "getname" with variable name

printf("your name is %s\n", name); : print out input variable "name" and print new line

return(0); : end prog.}

void getname (char n[20]) : function "getname" with variable of 20 character max
{
printf("pls enter name: "); : obvious

gets(n); : get input as string:variable'n' is throw back as "name"

return; // <====== need to return? : how else to end a function than return ??
}


if not wrong... the sentence will stick all together...

pls enter name: Simonyour name is Simon


correct me if wrong.

TS u revising for ur job interview ?

:bsmilie: :bsmilie:
 

Zaknafein

New Member
Oct 29, 2005
1,526
0
0
my room
www.flickr.com
#34
Eh, that piece of code posted by threadstarter is correct. I've tried it out on my computer and it works. There is no need to explicitly return n back to main(). The array parameter for getname() is converted into a pointer parameter by the compiler, so it is pass-by-reference.
wow really? it works??? ackkk gotta revise again :confused:
thought i understand liao lol
 

shojibake

New Member
Dec 7, 2004
387
0
0
#36
hello guys, need some help here :) its been a long time i've touch this and im revising my work now, so need some help as i have something that im not too sure of....so here it is



#include <stdio.h>

void getname (char n[20]);

main()
{
char name[20];

getname(name);
printf("your name is %s\n", name);

return(0);
}

void getname (char n[20])
{
printf("pls enter name: ");
gets(n);

return; // <====== need to return?
}


this is an example from a book, so i was wondering if this book is wrong...
would i need to return the variable "n" back to main, in order for main function to printf it?
Haven't done this in a while, but
char name[20];
is a reference. name actually points to the start of an array of 20 chars. The function decalaration in the beginning for getname should be able to do char *. Essentially all declarations of arrays are pointers.

Because the scope of the char name[20] exists in the main block and is passed by reference to getname, there is no need to explicitly allocate memory for that. Since it is passed by reference, there is no need to return(n) just a simple return will do.
 

Status
Not open for further replies.
Top Bottom