Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36

Thread: Need help on C language....

  1. #21

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

    Quote Originally Posted by wazzup View Post
    Actually for C, the proper way should be to use "malloc" first. I also rusty liao
    Yes... that is what I am about to point out...

    malloc ... nostalgic juz remember to "free"

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

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

    if that code is recursive good luck. hehehe

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

  3. #23

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

    wow u guys are talking too cheem liao lol
    haven revised to malloc yet!
    Photo Album - Photo Album

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

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

    no lah use sbrk better and nmap is even better.

  5. #25

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

    Quote Originally Posted by longkangman View Post
    no lah use sbrk better and nmap is even better.
    **blurrrrrr**
    Photo Album - Photo Album

  6. #26

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

    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.
    Last edited by Convoy; 21st November 2006 at 10:02 AM.

  7. #27
    Member Static's Avatar
    Join Date
    Jul 2006
    Location
    Surfing soft porn in CS
    Posts
    1,079

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

    Quote Originally Posted by longkangman View Post
    if that code is recursive good luck. hehehe

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


    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 ?
    Last edited by Static; 21st November 2006 at 05:41 PM.
    陳綺貞 2010 [夏季練習曲] 巡迴演唱會 Cheer Concert 13 Nov 2010 MAX Pavilion@Singapore Expo

  8. #28

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

    Quote Originally Posted by Static View Post


    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.

  9. #29
    Member Static's Avatar
    Join Date
    Jul 2006
    Location
    Surfing soft porn in CS
    Posts
    1,079

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

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

    Wahaha too lazy to test it out.. got to dig up my C++ installer...
    陳綺貞 2010 [夏季練習曲] 巡迴演唱會 Cheer Concert 13 Nov 2010 MAX Pavilion@Singapore Expo

  10. #30

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

    Quote Originally Posted by Static View Post
    Oh.. tot gets will not recognize "ENTER" as a valid character so it would just terminate gets ?
    it would print as a new line ?

    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.

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

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

    Quote Originally Posted by Static View Post


    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 ?


  12. #32
    Member Static's Avatar
    Join Date
    Jul 2006
    Location
    Surfing soft porn in CS
    Posts
    1,079

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

    Quote Originally Posted by longkangman View Post


    Hehe, programming not my forte. Paiseh Paiseh
    陳綺貞 2010 [夏季練習曲] 巡迴演唱會 Cheer Concert 13 Nov 2010 MAX Pavilion@Singapore Expo

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

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

    BTW, I am not a programmer/code developer nor do programming.

    I just learn from each others.

  14. #34

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

    Quote Originally Posted by Convoy View Post
    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
    thought i understand liao lol
    Photo Album - Photo Album

  15. #35

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

    Quote Originally Posted by Static View Post

    TS u revising for ur job interview ?
    ya, i was revising the other day for interview
    after ns, so long never touch programming liao... now rusty until all brown
    Photo Album - Photo Album

  16. #36

    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?
    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.

Page 2 of 2 FirstFirst 12

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
  •