Page 1 of 2 12 LastLast
Results 1 to 20 of 36

Thread: Need help on C language....

  1. #1

    Default Need help on C language....

    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?
    Photo Album - Photo Album

  2. #2

    Default Re: need help on C language....

    Quote Originally Posted by Zaknafein View Post
    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 ?

  3. #3

    Default Re: need help on C language....

    sorry man, i dunno programming forum

    return the variable to main, so main can printf the variable?
    Photo Album - Photo Album

  4. #4
    Senior Member
    Join Date
    Jul 2005
    Location
    sewage drain/manhole
    Posts
    5,011

    Default Re: need help on C language....

    void function( char []) and your wanna return?

    pass by reference or pass by value?

    if pass by reference no need return.

    YMMV

  5. #5

    Default Re: need help on C language....

    Quote Originally Posted by Zaknafein View Post
    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.

  6. #6

    Default Re: need help on C language....

    Quote Originally Posted by longkangman View Post
    void function( char []) and your wanna return?

    pass by reference or pass by value?

    if pass by reference no need return.

    YMMV

    the books says is pass by arguements, so i supposed its pass by value
    Photo Album - Photo Album

  7. #7
    Senior Member
    Join Date
    Jul 2005
    Location
    sewage drain/manhole
    Posts
    5,011

    Default Re: need help on C language....

    since gets() allocate the space you are ok.

  8. #8

    Default Re: need help on C language....

    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"
    Photo Album - Photo Album

  9. #9
    Senior Member
    Join Date
    Jul 2005
    Location
    sewage drain/manhole
    Posts
    5,011

    Default Re: need help on C language....

    why not?

  10. #10

    Default Re: need help on C language....

    Quote Originally Posted by longkangman View Post
    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?
    Photo Album - Photo Album

  11. #11
    Account Suspended
    Join Date
    Sep 2006
    Location
    I love my cup & coaster!
    Posts
    754

    Default Re: need help on C language....

    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.

  12. #12

    Default Re: need help on C language....

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

  13. #13
    Account Suspended
    Join Date
    Sep 2006
    Location
    I love my cup & coaster!
    Posts
    754

    Default Re: need help on C language....

    Quote Originally Posted by Zaknafein View Post
    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.
    Last edited by wazzup; 20th November 2006 at 09:58 PM.

  14. #14

    Default Re: need help on C language....

    ahhh wazzup, thank you for clarifying this!
    Photo Album - Photo Album

  15. #15
    Senior Member
    Join Date
    Jul 2005
    Location
    sewage drain/manhole
    Posts
    5,011

    Default Re: need help on C language....

    Quote Originally Posted by Zaknafein View Post
    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);
    }

  16. #16

    Default Re: need help on C language....

    Quote Originally Posted by Zaknafein View Post
    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"
    should give output of "Simon"
    See my Photo Gallery at the Clubsnap

  17. #17

    Default Re: need help on C language....

    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!
    Photo Album - Photo Album

  18. #18
    Account Suspended
    Join Date
    Sep 2006
    Location
    I love my cup & coaster!
    Posts
    754

    Default Re: need help on C language....

    Quote Originally Posted by longkangman View Post
    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.
    Last edited by wazzup; 20th November 2006 at 10:02 PM.

  19. #19
    Senior Member
    Join Date
    Jul 2005
    Location
    sewage drain/manhole
    Posts
    5,011

    Default Re: need help on C language....

    Using of gets() will allocate the memory that is the trick but I will never use that piece of code.

  20. #20
    Account Suspended
    Join Date
    Sep 2006
    Location
    I love my cup & coaster!
    Posts
    754

    Default Re: need help on C language....

    Quote Originally Posted by longkangman View Post
    Using of gets() will allocate the memory that is the trick but I will never use that piece of code.
    Actually for C, the proper way should be to use "malloc" first. I also rusty liao

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •