You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用call by value和call by pointers实现指定C数组生成程序?

解决C语言数组的传值与指针传递实现问题

Hey there! Let's break this down step by step since you're a C beginner—no worries, we'll keep it clear and practical. First, let's start by completing your original program so we have a baseline to work from, then we'll adapt it into the two requested versions.

原程序完整实现

First, here's a complete version of your original program that generates the 10×10 array, fills it with random values between -100 and 100, and prints the array along with the main diagonal elements read from bottom to top:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define M 10

int main() {
    // Initialize random seed
    srand(time(NULL));
    
    int arr[N][M];
    
    // Fill array with random values (-100 to 100)
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            arr[i][j] = rand() % 201 - 100; // 0-200 → -100 to 100
        }
    }
    
    // Print the full array
    printf("10×10 Array:\n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            printf("%4d ", arr[i][j]);
        }
        printf("\n");
    }
    
    // Print main diagonal from bottom to top
    printf("\nMain Diagonal (bottom to top):\n");
    for (int i = N - 1; i >= 0; i--) {
        printf("%d ", arr[i][i]);
    }
    printf("\n");
    
    return 0;
}

1. Call by Value(值传递)实现

In C, arrays passed directly to functions decay to pointers automatically—so to achieve true call-by-value (where the function works on a copy of the array, not the original), we need to wrap the array inside a struct. When you pass a struct to a function, C creates a full copy of it, so any changes inside the function won't affect the original struct.

Here's how to implement this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define M 10

// Define a struct to wrap the 10×10 array
typedef struct {
    int data[N][M];
} ArrayWrapper;

// Function to generate the array (call by value: receives a copy, but we'll return the filled struct)
ArrayWrapper generateArrayByValue() {
    ArrayWrapper wrapper;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            wrapper.data[i][j] = rand() % 201 - 100;
        }
    }
    return wrapper;
}

// Function to print the array and diagonal (receives a copy of the wrapper struct)
void printArrayByValue(ArrayWrapper wrapper) {
    printf("10×10 Array (Call by Value):\n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            printf("%4d ", wrapper.data[i][j]);
        }
        printf("\n");
    }
    
    printf("\nMain Diagonal (bottom to top):\n");
    for (int i = N - 1; i >= 0; i--) {
        printf("%d ", wrapper.data[i][i]);
    }
    printf("\n");
}

int main() {
    srand(time(NULL));
    
    // Generate array via call-by-value function
    ArrayWrapper myArray = generateArrayByValue();
    
    // Print array via call-by-value function
    printArrayByValue(myArray);
    
    return 0;
}

Key Notes:

  • We use a ArrayWrapper struct to hold the array. When we pass this struct to printArrayByValue(), C makes a full copy of the entire array inside the struct.
  • The generateArrayByValue() function creates and fills a local wrapper, then returns it—this is also a form of value transfer (the struct is copied back to the main function).

2. Call by Pointers(指针传递)实现

This is the more common approach in C for handling arrays. We pass a pointer to the array so the function can directly access and modify the original array's memory without making a copy.

Here's the implementation:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define M 10

// Function to generate array (receives a pointer to the 2D array)
void generateArrayByPointer(int (*arr)[M]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            arr[i][j] = rand() % 201 - 100;
        }
    }
}

// Function to print array and diagonal (receives a pointer to the 2D array)
void printArrayByPointer(int (*arr)[M]) {
    printf("10×10 Array (Call by Pointer):\n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            printf("%4d ", arr[i][j]);
        }
        printf("\n");
    }
    
    printf("\nMain Diagonal (bottom to top):\n");
    for (int i = N - 1; i >= 0; i--) {
        printf("%d ", arr[i][i]);
    }
    printf("\n");
}

int main() {
    srand(time(NULL));
    
    int myArray[N][M];
    
    // Pass the array's address (it decays to a pointer automatically)
    generateArrayByPointer(myArray);
    
    // Print the array via pointer
    printArrayByPointer(myArray);
    
    return 0;
}

Key Notes:

  • The function parameter int (*arr)[M] is a pointer to an array of M integers—this correctly represents a 2D array of size N×M.
  • When we pass myArray to the function, it automatically decays to a pointer to its first row, so we're directly working with the original array in memory.
  • This is more memory-efficient than call-by-value because no copy of the large array is made.

验证输入输出一致性

All three programs (original, call-by-value, call-by-pointer) will produce identical output: a random 10×10 array followed by the main diagonal elements printed from bottom to top. Just make sure to run them separately (since the random seed is based on time, running them at the same time will give the same values too).

内容的提问来源于stack exchange,提问作者Iuliana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:01:56