ç传递一个int类型的函数吗? [英] C Passing an int in a function?

查看:116
本文介绍了ç传递一个int类型的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个函数传递一个INT时,得到不正确的结果:

I am getting incorrect results when passing a int in a function:

int recruit(int var1, int re_unit, char *char_buffer, int var2) {
 int run = 1;
 int int_buffer = 0;
 printf("Test1 %d\n", var1);
 printf("Test2 %d\n", var2);
 ...
}
void some_other_function(structs, struct1[]) {
 int var1 = 0;
 int var2 = 0;
 int re_unit = 0;
 char char_buffer[] = "What ever";

 //strucs[1].first = 50 this is done in a other section

 var1 = strucs1[1].first;
 var2 = strucs1[1].first;


 recruit(var1, re_unit, char_buffer, var2);
  // Ind the full verstion of the program this function is called 2 times:
  // The first time nothing is worng, how ever the second time, the result
  // is as explaned below


  //strucs[2].first = 50 // this is done in a other section
  var1 = struct1[2].first;
  var2 = struct1[2].first;

 recruit(var1, re_unit, char_buffer, var2);

}

int main(void) {
 ...
}

现在的结果是,
 第一次
 Test1的打印:2684032,和
 Test2的打印:50

Now the results is that The first time Test1 prints: 2684032, and Test2 prints: 50

第二次
 测试1打印:2684032和
 测试2打印:50;

The second time Test 1 prints: 2684032 and Test 2 prints: 50;

他们都应该打印50。

我已经测试了struct1值[1]在功能使用的招之前。首先是50。

I have tested that the value of struct1[1].first is 50 before it's used in the function recruit.

是否有任何人知道为什么会发生这种情况?

Does any one know a reason why this could happen?

推荐答案

用C的功能未初始化的值不为0,他们只是垃圾任何记忆了在那个特定的时刻。

Uninitialized values in C's functions are not 0, they are just garbage whatever memory had at that particular moment..

#include <stdio.h>
int main()
{
    int a;
    printf("%i\n", a);
    return 0;
}

这应该表现出的理念...

That should demonstrate the idea...

希望我间preTED正确的问题。

Hopefully I interpreted the question correctly.

这篇关于ç传递一个int类型的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆