• XENNEC Camera Bags GIVE AWAY - SHARE & TAG your Friend to WIN one XENNEC Stylish bags. Visit this thread thread for more information.

Need help on C language....


Status
Not open for further replies.

Zaknafein

New Member
Oct 29, 2005
1,526
0
0
my room
www.flickr.com
#1
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?
 

kitkat

New Member
Mar 5, 2005
568
0
0
#2
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?
wrong forum to ask this...

but again, what is the point of returning ? :devil:
 

kitkat

New Member
Mar 5, 2005
568
0
0
#5
sorry man, i dunno programming forum :(

return the variable to main, so main can printf the variable?
code looks ok to me. you can return anything even "nothing" itself. :)

Some forums like C can be found in newsgroup.
 

Zaknafein

New Member
Oct 29, 2005
1,526
0
0
my room
www.flickr.com
#8
maybe i should re-phrase my question... my english a bit jia lat lol ;)

lets say for the above program... the user input "Simon"
would the programme give an output of "simon"
 

Zaknafein

New Member
Oct 29, 2005
1,526
0
0
my room
www.flickr.com
#10
since gets() allocate the space you are ok.
i am sorry, i dun quite get u....
what u mean is... if the variable is a string, no need to pass back.
but if the variable is an int or float, the function getname would need to pass back to main, so main can printf it?
 

#11
Only if you use pointers, then your values will be passed by reference.

Code:
void getname(char* name) { } // this will pass by reference
void methods do not expect any values to be returned, using the "return" statement will cause the function to exit at the point of call.

Code:
char[20] getname(char n[20]) { return n; }
The above function will require a return value, "return" alone statements will not work.


Your above code will not print the name out.
 

#13
what i dun understand is, why does int or float need to return back to main function...
but a string doesnt need...?
It doesn't, in this case,

i) you don't return the string, the value will not get printed
ii) you didn't pass the string by reference, the value will not get printed.

Code:
getname(name);
printf("your name is %s\n", name);
"name" is passed only by value to the function getname(char n[20]);

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

return; // <====== need to return?
}
"n" only exists when the function is called, and is killed (garbage collected) after the "return" statement is called. The value of name in the main function will not be updated.
 

#15
what i dun understand is, why does int or float need to return back to main function...
but a string doesnt need...?

char *function(char[])
{
return &char ;
}


You design the code to work for you lah.


void function( void)
{

}


int function ()
{
return (X);
}
 

Zaknafein

New Member
Oct 29, 2005
1,526
0
0
my room
www.flickr.com
#17
yes, thanks longkangman :)
yeah, ur right. i believe no need to pass back if its by reference :)
and in this case, would need to pass back to main.

thanks everyone for ur help! :)
 

#18
char *function(char[])
{
return &char ;
}


You design the code to work for you lah.


void function( void)
{

}


int function ()
{
return (X);
}
Yo dude,

it's

Code:
void getname(char* n) { //code here ... }
Since the address of the variable from the main function is passed into the function, the address stack will be updated with the value "n" when the function exits. Thereafter the address of "name" will be pointing to the same address stack as "n" was pointing at.
 

Status
Not open for further replies.
Top Bottom