Thread: C help

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When you find yourself copy/pasting blocks of code, then that code is a good candidate for a function. The only challenge you may face, if you're still very new, is how to get two values (e.g. "first" and "second") from a function, when a function can only return one value. The solution is to pass pointers to the variables of interest, so their values can be updated in the called function.

    For example:

    Code:
    #include <stdio.h>
    
    void get_two_numbers( int *n1, int *n2 );  // expects two pointers to int
    
    int main(void)
    {
        int x1, x2;
    
        get_two_numbers(&x1, &x2);  // pass addresses of x1 and x2
    
        printf("\nx1 is %d\nx2 is %d\n", x1, x2);
    
        return 0;
    }
    
    void get_two_numbers( int *n1, int *n2 )
    {
        int num1, num2;
    
        puts("Enter 1st number:");
        scanf("%d", &num1);         // error checking omitted
        puts("Enter 2nd number:");
        scanf("%d", &num2);         // error checking omitted
    
        *n1 = num1;  // dereference "n1" and store the value of "num1"
        *n2 = num2;  // dereference "n2" and store the value of "num2"
    }
    In "get_two_numbers()", you wouldn't even need local variables. Note the slightly difference scanf() calls, since "n1" and "n2" are already pointers:

    Code:
    void get_two_numbers( int *n1, int *n2 )
    {
        puts("Enter 1st number:");
        scanf("%d", n1);         // error checking omitted; no '&' operator since n1 already a pointer
        puts("Enter 2nd number:");
        scanf("%d", n2);         // error checking omitted; no '&' operator since n2 already a pointer
    }
    If this seems over your head at this time, an easier solution would be to pass a char that indicates the operation you wish to perform. Then your function can read two values, perform the indicated operation, and simply return an answer.

  2. #17
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    Matticus thank you very much for help, you are very good in teaching. Next chapter i think is about pointers and im gonna read it today and try this solution that you told me.

    If i get it right, in function get_two_numbers() i dont need num1 and num2 because there are pointers n1 and n2 and i dont need to use & for pointers in scanf, same as string for example
    Code:
    ("%s", string)
    .

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by kzzmzz View Post
    If i get it right, in function get_two_numbers() i dont need num1 and num2 because there are pointers n1 and n2 and i dont need to use & for pointers in scanf, same as string for example
    Code:
    ("%s", string)
    .
    Assuming you mean scanf("%s", string), then you are correct. In this example, "string" is (presumably) a char array, so using the name of the array by itself acts as a pointer to its first element - since it's a pointer already, you don't need the '&' operator. In the example I posted, "n1" and "n2" are already pointers, so you don't need the '&' there either.

    Sounds like you understand what's going on. Keep up the good work.

  4. #19
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    Yes i mean scanf("%s", string). Thank you very much again, can i send you pm or ask you here on forum if i got in trouble again?

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome. Just create a new thread if you have any problems going forward, you'll get the benefit of various members' advice.

Popular pages Recent additions subscribe to a feed

Tags for this Thread