我正在尝试在C语言中测试一种算法,但是在尝试调用该函数时,在主函数中调用函数的那一行出现错误“ Expected expression”。有人可以发现所犯的错误吗?
#include <math.h>
#include <stddef.h>
int fwsubst(
unsigned long n,
double alpha,
double **R, /* two-dimensional array, row-major */
double *b /* one-dimensional array */
);
int main(void){
fwsubst(2 ,5.0,{{2.0,2.0},{4.0,2.8}}, {1.0, 9.6});
return 0;
}
int fwsubst(
unsigned long n,
double alpha,
double **R, /* two-dimensional array, row-major */
double *b /* one-dimensional array */
){
double x;
for (size_t k = 0; k < n; k++) {
double sum = 0.0;
for (size_t i = 0; i < k; i++) {
sum += b[i] * R[i][k];
}
x = (b[k] - sum)/(alpha + R[k][k]);
if (!isfinite(x))
return -1;
if(alpha + R[k][k] == 0)
return -1;
b[k] = x;
}
return 0;
}
答案 0 :(得分:3)
OP的代码无效
// v-------------------v v--------v Invalid
fwsubst(2 ,5.0,{{2.0,2.0},{4.0,2.8}}, {1.0, 9.6});
使用C99,代码可以使用compound literals。
由括号类型名称和括号括起来的初始化程序列表构成的后缀表达式是复合文字。 C11dr§6.5.2.53
int fwsubst(unsigned long n, double alpha, double **R, double *b);
int main(void) {
fwsubst(2, 5.0,
// v------------------------------------------------------------v nested literal
// v--------------------v, v--------------------v literal
(double *[2]) {(double[2]) {2.0, 2.0}, (double[2]) {4.0, 2.8}},
(double[2]) {1.0, 9.6});
// ^--------------------^ literal
}
无需更改功能定义。
答案 1 :(得分:2)
这不是数组文字的正确语法。您需要:
function Shooter(shooter)
{
shooter.parent.style.width = "300px";
shooter.parent.style.height = "200px";
shooter.parent.style.border = "1px solid";
shooter.parent.style.border.borderColor = shooter.borderColor;
var canvas = document.createElement("canvas");
canvas.style.width = "300px";
canvas.style.height = "200px";
canvas.addEventListener('mousedown', drawBullet, false);
shooter.parent.appendChild(canvas);
}
function drawBullet(event){
var rect = this.getBoundingClientRect();
var x = event.clientX - rect.left;
var y = event.clientY - rect.top;
var ctx = this.getContext("2d");
console.log(this);
console.log(x,y);
console.log(event.clientX, event.clientY);
ctx.fillStyle = "#ff2626"; // Red color
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI * 2);
ctx.fill();
}
您需要更改函数定义以使其匹配,因为fwsubst(2 ,5.0,(double[2][2]){{2.0,2.0},{4.0,2.8}}, (double[2]){1.0, 9.6});
不会转换为double[2][2]
而是会转换为double **
:
double (*)[2]