小编Tri*_*lla的帖子

Am I Correctly Understanding Pass By Value and Pass By Reference in C?

In one of my course's tutorial videos, I have encountered a situation where pass by value and pass by reference is occurring. In the example he gives us, he, firstly, admits that the code is written poorly, and then asks us to find out what exactly would be printed. The relevant code is as follows:

void set_array(int array[4]);
void set_int(int x);

int main(void){
    int a = 10; 
    int b[4] = {0, 1, 2, 3);
    set_int(a);
    set_array(b);
    printf("%d %d\n", a, b[0]); …
Run Code Online (Sandbox Code Playgroud)

c arrays pass-by-reference pass-by-value

2
推荐指数
1
解决办法
172
查看次数

为什么这个简单的乘法函数在 Main 中调用时不返回任何内容?

在 code::blocks 中编写以下代码时,在 main 中调用时不会产生所需的结果:

#include <stdio.h>
#include <stdlib.h>

float mult_two_floats(float a, float b);
int main()
{
    mult_two_floats(7,7);
    return 0;
}
float mult_two_floats(float a, float b){
    return a * b;
}
Run Code Online (Sandbox Code Playgroud)

当重写如下时,它确实有效:

#include <stdio.h>
#include <stdlib.h>


float mult_two_floats(float a, float b);
int main()
{
    mult_two_floats(7,8);
    return 0;
}
float mult_two_floats(float a, float b){
    printf("%f", a*b);
}
Run Code Online (Sandbox Code Playgroud)

请帮助我了解我做错了什么。谢谢你。

c function cs50

0
推荐指数
1
解决办法
68
查看次数

标签 统计

c ×2

arrays ×1

cs50 ×1

function ×1

pass-by-reference ×1

pass-by-value ×1