如何从 C 中的函数返回二维数组? [英] How to return a 2D array from a function in C?

查看:22
本文介绍了如何从 C 中的函数返回二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Ruby 程序员,最终为 C 开发了一个代码生成器.这就像让一辆豪华轿车拖一辆 1960 年代的卡车.不管怎样.

I am a Ruby programmer who has ended up developing a code generate for C. Its like asking a Limo to tow a 1960s truck. Any way.

这是我认为应该有效但无效的方法.

Here is what I thought should work but doesnt work.

float[][] pixels()
{
  float x[][]= { {1,1},{2,2} };
  return x
}

void drawLine(float x[][2])
{
  //drawing the line
}

//inside main
drawLine(pixels());

我把头撞在桌子上,试图让这件事发挥作用.请帮忙.

I have banged my head on my desk trying to get this thing work. Please help.

推荐答案

谢谢大家的回答,特别是对数组指针关系的详细解释.

Thank you all for your answers and more specifically for the detailed explanation of the array-pointer relationship.

我将数组封装在一个结构中

I encapsulated the array in a structure

 struct point_group1 {
        float x[3];
        float y[3];
};

struct point_group1 pixels(){
    struct point_group1 temp;

    temp.x[0] = 0.0;
    temp.x[1] = 1.0;
    temp.x[2] = -1.0;

    temp.y[0] = 0.0;
    temp.y[1] = 1.0;
    temp.y[2] = 1.0;

    return temp;    
}



struct point_group1 points1  = pixels();
axPoly(points1.x, points1.y ,3, 0.0);

这篇关于如何从 C 中的函数返回二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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