zoukankan      html  css  js  c++  java
  • c语言学习(4)

    c语言从函数中返回多个值?

    用指针参数来实现。

    #include <stdio.h>
    return_more(int x, int y, int *sum, int *a, int *b);
    int *return_pointer(int *p, int n);
    int main() {
            int x, y;
            x = 131;
            y = 2;
            int *ptr;
    
            int arr[] = {1,2,3,4};
            printf("Address of arr = %u
    ", arr);
            *arr = *arr + 15;
            ptr = return_pointer(arr, 22);
            printf("Address of arr = %u
    ", arr);
            printf("Address of ptr = %u
    ", ptr);
            for (int i = 0; i<=4; i++) {
                    printf("%d
    ", arr[i]);
            }
            //printf(arr);
    
            //int sum, diff, prod;
            //return_more(x, y, &sum, &diff, &prod);
            //printf("sum:%d
    ", sum);
            //printf("diff:%d
    ", diff);
            //printf("prod:%d
    ", prod);
            return 0;
    }
    
    int *return_pointer(int *p, int n) {
            printf("Address of reference: = %u
    ", p);
            //p = p+n;
            printf("%d
    ", p);
            printf("%d
    ", p+1);
            printf("%d
    ", p+2);
            printf("%d
    ", p+3);
    
            printf("%d
    ", *p);
            printf("%d
    ", *(p+1));
            printf("%d
    ", *(p+2));
            printf("%d
    ", *(p+3));
            return p;
    }
    
    return_more(int x, int y, int * sum, int * diff, int * prod) {
            *sum = x+y;
            *diff = x-y;
            *prod = x*y;
    }
    

      

    -------------------------------------------

     原文: https://overiq.com/c-programming-101/returning-more-than-one-value-from-function-in-c/

    Returning more than one value from function in C

     Last updated on July 27, 2020


    In the chapter Return statement in C, we have learned that the return statement is used to return a value from the function. But there is one limitation, a single return statement can only return one value from a function. In this chapter, we will see how to overcome this limitation by using call by reference.

    Consider the following problem.

    Q - Create a function to return sum, difference and product of two numbers passed to it.

    Tell me how would you approach this problem?

    One way to approach this problem is to create three functions for 3 operations and then use the return statement in each one of them to return sum, difference and product. By using call by reference we can solve this much easily.

    The following program demonstrates how you can return more than one value from a function using call by reference.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include<stdio.h>
    void return_more_than_one(int a, int b, int *sum, int *diff, int *prod);
    
    int main()
    {
        int x = 40, y = 10, sum, diff, prod;
    
        return_more_than_one(x, y, &sum, &diff, &prod);
    
        printf("%d + %d = %d
    ",x, y, sum);
        printf("%d - %d = %d
    ",x, y, diff);
        printf("%d * %d = %d
    ",x, y, prod);
    
        // signal to operating system program ran fine
        return 0;
    }
    
    void return_more_than_one(int a, int b, int *sum, int *diff, int *prod)
    {
        *sum = a+b;
        *diff = a-b;
        *prod = a*b;
    }
    

    Expected Output:

    1
    2
    3
    40 + 10 = 50
    40 - 10 = 30
    40 * 10 = 400
    

    How it works:

    In return_more_than_one() function a and b are passed using call by value, whereas sumdiff and prod are passed using call by reference. As a result return_more_than_one() function knows the address of sumdiff and prod variables, so it access these variables indirectly using a pointer and changes their values.

    --------------------------------------------

    C Program to Access Array Elements Using Pointer

    In this example, you will learn to access elements of an array using a pointer.

     

    To understand this example, you should have the knowledge of the following C programming topics:


    Access Array Elements Using Pointers

    #include <stdio.h>
    int main() {
        int data[5];
    
        printf("Enter elements: ");
        for (int i = 0; i < 5; ++i)
            scanf("%d", data + i);
    
        printf("You entered: 
    ");
        for (int i = 0; i < 5; ++i)
            printf("%d
    ", *(data + i));
        return 0;
    }
    

    Output

    Enter elements: 1
    2
    3
    5
    4
    You entered: 
    1
    2
    3
    5
    4
    

    In this program, the elements are stored in the integer array data[].

     

    Then, the elements of the array are accessed using the pointer notation. By the way,

    • data[0] is equivalent to *data and &data[0] is equivalent to data
    • data[1] is equivalent to *(data + 1) and &data[1] is equivalent to data + 1
    • data[2] is equivalent to *(data + 2) and &data[2] is equivalent to data + 1
    • ...
    • data[i] is equivalent to *(data + i) and &data[i] is equivalent to data + i

    Visit this page to learn about the relationship between pointers and arrays.

    -------------------------------------------------------------

    Returning a Pointer from a Function in C

     Last updated on July 27, 2020


    We have already seen a function can return data of types int , float, char etc. Similarly, a function can return a pointer to data. The syntax of a function returning a pointer is as follows.

    Syntax: type *function_name(type1, type2, ...);

    Some examples:

    1
    2
    3
    int *func(int, int); // this function returns a pointer to int
    
    double *func(int, int); // this function returns a pointer to double
    

    The following program demonstrates how to return a pointer from a function.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #include<stdio.h>
    int *return_pointer(int *, int); // this function returns a pointer of type int
    
    int main()
    {
        int i, *ptr;
        int arr[] = {11, 22, 33, 44, 55};
        i = 4;
    
        printf("Address of arr = %u
    ", arr);
    
        ptr = return_pointer(arr, i);
    
        printf("
    After incrementing arr by 4 
    
    ");
    
        printf("Address of ptr = %u
    
    " , ptr);
        printf("Value at %u is %d
    ", ptr, *ptr);
    
        // signal to operating system program ran fine
        return 0;
    }
    
    int *return_pointer(int *p, int n)
    {
        p = p + n;
        return p;
    }
    

    Expected Output:

    1
    2
    3
    4
    5
    6
    7
    Address of arr = 2686736
    
    After incrementing arr by 4
    
    Address of ptr = 2686752
    
    Value at 2686752 is 55
    

    How it works:

    Since the name of an array is a pointer to the 0th element of the array. Here we are passing two arguments to the function return_pointer(). The arr is passed using call by reference (notice that name of the array is not preceded by & operator because the name of the array is a constant pointer to the 0th element of the 1-D array) and i is passed using call by value. Inside the function pointer p is incremented by n and reassigned to p. Finally, the pointer p is returned to the main() function and reassigned to ptr.

    Never return a pointer to local variable from a function.

    Consider the following code.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    #include<stdio.h>
    int *abc(); // this function returns a pointer of type int
    
    int main()
    {
        int *ptr;
        ptr = abc();
        return 0;
    }
    
    int *abc()
    {
        int x = 100, *p;
        p = &x;
        return p;
    }
    

    Can you point out the problem with above code?

    In the function abc() we are returning a pointer to the local variable. Recall that a local variable exists only inside the function and as soon as function ends the variable x cease to exists, so the pointer to it is only valid inside the function abc().

    Even though the address returned by the abc() is assigned to ptr inside main(), the variable to which ptr points is no longer available. On dereference the ptr you will get some garbage value.

  • 相关阅读:
    DDL
    [笔记]NFC笔记——初始化RF碰撞避免
    [笔记]Java没有C语言的编译开关怎么办?
    [笔记]NFC笔记——通用初始化及单设备检测(SDD)流程
    [笔记]NFC笔记——NFCIP1协议命令集(NFCIP1 Protocol Command Set)
    [笔记]C++代码演示SingletonMap 单类Map实例
    [笔记]NFC笔记——传输帧格式
    [笔记]C++代码演示Singleton单类实例
    [笔记]山寨中文编程语言
    [笔记]NFC笔记——ATR_REQ 消息结构
  • 原文地址:https://www.cnblogs.com/oxspirt/p/13511531.html
Copyright © 2011-2022 走看看