静态矩阵转换为指针 [英] Static Matrix Casting to a pointer

查看:88
本文介绍了静态矩阵转换为指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有矩阵M:

float M[4][3] = {
    0, 0, 0,
    0, 1, 1,
    1, 0, 1,
    1, 1, 0};

我需要使用方法"foo"来强制转换M:

And I need to cast M with the purpose of use the method "foo":

foo(float **matrix){ 
    printf("%f",matrix[0][0]);
}

我使用以下命令成功编译了代码

I compiled sucessfully the code using:

foo( (float**) M )

但是当我执行它时,出现了段错误.怎么了?我在Ubuntu Oneiric中使用g ++.

But when I executed it, I got a segment fault. What is wrong? I am using g++ in Ubuntu Oneiric.

谢谢.

好的,谢谢陆谦,但是使用该怎么办?

Ok, thanks Luchian, but what about using:

float **M = new float*[4];
M[0] = {0,0,0};

我知道它不会编译,但是有类似的东西吗?

I know that it does not compile, but there it something similar?

推荐答案

好的,最好的方法:

float **M = new float*[4];
for(int i=0; i<4; i++){
    M[i] = new float[3];
    for(int j=0; j<3; j++){
        M[i][j] = something...
    }
}

这篇关于静态矩阵转换为指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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