指针并将变量值从一个函数传递到另一个函数。如何使用指针传递变量值

问题描述:

我需要碗函数将b1和b2传入转台,以便将这两个函数一起添加到不同的函数中。这是代码,我做错了什么?指针并将变量值从一个函数传递到另一个函数。如何使用指针传递变量值

void bowl(){ 
    int b1=rand()%11; 
    int b2=rand()%(11-(b1)); 
    int turntotal(&b1,&b2); 
} 

int turntotal(int *b1, int *b2){ 
    int bowltotal; 
    bowltotal=((b1)+(b2)); 
    return(bowltotal); 
} 
+2

你的学习资源有书[这里](一个好名单http://*.com/questions/388242/the-definitive-c-book-guide-and-list),看起来你需要一个。 – molbdnilo

您需要取消引用指针从它指向的,所以你会写地址获取值:

int turntotal(int *b1, int *b2) { 
    return (*b1) + (*b2); 
} 

但是你的函数不修改任何参数,所以你可以简单地写:

int turntotal(int b1, int b2) { 
    return b1 + b2; 
} 

另外行:

int turntotal(&b1,&b2); 

没有意义。你可能想要分配从该函数返回到一个新的变量,因此你可以写值:

int sum = turntotal(&b1,&b2); 

int sum = turntotal(b1,b2); 

如果你,你不必不使用指针。

正如意见建议 - 这是一些基本的东西,你应该考虑改变以one of the good books.