有没有人知道如何解决警告:当我返回new_cost时,return从整数中生成指针而没有强制转换[默认启用]?
int *cost(int num_nodes, edge new_solution[][10])
{
int new_cost = 0;
int num_edges = 1; //set number of edges back to default
int x, y, weight;
for (x = 1; x <= num_nodes; x++) //print out new_solution
{
weight =0;//find the largest tx on each node
for (y = 1; y <= num_nodes; y++)
if (new_solution[x][y].label == 1)
{
printf("\n Edge %d:(%d %d) energy:%d",
num_edges++, x, y, new_solution[x][y].weight);
if (weight < new_solution[x][y].weight) //find highest energy used per node
{
weight = new_solution[x][y].weight;
//printf("\n weight:%d accum:%d", weight, new_cost);
}
}
new_cost += weight; //find total weight
}
printf("\n Total cost is %d\n\n", new_cost);
return new_cost;
}
是因为我无法将权重变量分配给数组吗?对于返回成本,我收到以下函数的相同警告。有谁知道如何解决这个问题?
int *acceptance_prob(float T, int old_cost, int new_cost, edge new_solution[][10],
edge current_SA[][10], FILE *fp, int num_nodes)
{
int delta = 0, x, y;
float ap = 0.0;
int cost;
delta = (old_cost - new_cost);
ap = (exp(delta/T)); //this is the typical equation used
if (new_cost < old_cost)//if new_solution has less energy select it
{
fprintf(fp, "NO");
for (x = 1; x <= num_nodes; x++)
for (y = 1; y <= num_nodes; y++)
current_SA[x][y] = new_solution[x][y];
old_cost = new_cost;
}
else //if new_solution uses more energy maybe select it
{
if (ap > rand_float())
{
fprintf(fp,"YES");
for (x = 1; x <= num_nodes; x++)
for (y = 1; y <= num_nodes; y++)
current_SA[x][y] = new_solution[x][y];
old_cost = new_cost;
}
}
cost = old_cost;
return cost;
}
答案 0 :(得分:6)
有没有人知道如何解决警告:当我返回
new_cost
时,返回从整数中生成指针而没有强制转换[默认启用]?
将函数的返回类型从int*
更改为int
。
从该函数返回int*
无论如何都没有意义。