0

Hi I'm not a "native" C/C++ programmer. I'm able to write some basic things and do a coding that is common to other languages. But I have this problem and I don't know even how to ask different then explaining it (thus google searching won't fit me).

I've got in my code

typedef float point3[3]; 

And now I'm initilizing a bunch of points in 3D (x,y,z) by this:

point3 cpoint = {computeX(u,v),computeY(u,v)-5,computeZ(u,v)};

What functions does and values of u and v are irrevelent to my question (but I can provide code if asked).

Now I want to declare an array (one dimensional) of point3. So when I call array[0] I will get a point3 type variable. How I can do that?

EDIT:

I provided insufficient information. My bad. I need to retain: typedef float point3[3]; because I'm using OpenGL with GLUT and using this function glVertex3fv(cpoint); where cpoint is point3 type. So I'm pretty sure I can't use struct.

1
  • 1
    You can provide a cast or conversion from a struct to a float* which you then pass to the function... Also, there is no such thing as "C/C++". They are different languages, and while it's possible to write things that would compile as either, you are shooting yourself in the foot by trying to do so. Pick one and stick with it. C++ provides many, many useful tools that you are denying yourself. Commented Dec 13, 2010 at 10:42

5 Answers 5

2
point3 cpoint[2] = {
   {computeX(a, b), computeY(a,b)-5, computeZ(a,b)},
   {computeX(c, d), computeY(c,d)-5, computeZ(c,d)}
}
Sign up to request clarification or add additional context in comments.

3 Comments

If this code is something like this: point3 test[2]; point3 cpoint = {computeX(u,v),computeY(u,v)-5,computeZ(u,v)}; point3 test[0] = cpoint;? If yes that isn't working and that's my case.
@Ertai: You must distinguish between initialization and assignment. Initialization of arrays is possible and should be done, assignment as a whole not. I recently wrote things up arround that theme: gustedt.wordpress.com/2010/12/12/assignment-and-initialization
Ok, so basicly my problem is that I don't have the right assignment operator. I think I could use an assigment If I get my pointers right... problem is I don't know how to do this, and learning about pointers in C++ is a bit of hard thing to do (and a bit of overkill for simple assigment that I have) just to assign a value (array) to an array of values.
2

Assuming C++, the simplest way would be to create a class to encapsulate your point structure and then create a vector of that type:

struct Point
{
    Point(float x, float y, float z) : x_(x), y_(y), z_(z)
    float x_;
    float y_;
    float z_;
};

typedef std::vector<Point> Points;

Then you can create a collection for storing the points:

Points points;

then later in the code, once populated you can access using:

Point a = points[ 3 ]; // for example

2 Comments

I provided insufficient information. My bad. I need to retain: typedef float point3[3]; because I'm using OpenGL with GLUT and using this function glVertex3fv(cpoint); where cpoint is point3 type. So I'm pretty sure I can't use struct.
@Ertai: I've added an answer which expands upon this solution with automatic conversion.
1

At first declare you own point3 like this

struct point3
{
 float x;
 float y;
 float z;
};

then declare your array

point3 points[2] = {{calculateX(a1, b1), calculateY(a1, b1), calculateZ(a1, b1)},{calculateX(a2, b2), calculateY(a2, b2), calculateZ(a2, b2)}};

2 Comments

I provided insufficient information. My bad. I need to retain: typedef float point3[3]; because I'm using OpenGL with GLUT and using this function glVertex3fv(cpoint); where cpoint is point3 type. So I'm pretty sure I can't use struct.
Have you tried? Not so sure if you really can't use pointer to struct.
0

I'd like to expand Robin Welch's answer in a way which allows to stay compatible with the point3 typedef:

typedef float point3[3]; 


struct Point
{
    Point(float x, float y, float z)
    : x_(values_[0]), y_(values_[1]), z_(values_[2])
    {
        values_[0] = x;
        values_[1] = y;
        values_[2] = z;
    }

    float& x_;
    float& y_;
    float& z_;
    point3 values_;

    operator const point3&() const { return values_; }
    operator point3&() { return values_; }
};

Now you can just use this simple struct, and pass it to OpenGL functions just as if it weren't this struct at all. It contains the original point3 array for OpenGL, yet convenient access and a constructor for yourself.

Comments

0

In C, I wouldn't initialize the array when declaring it unless it's a small array with constant values.

typedef float point3[3]; /* point is a type */
point3 *pp;              /* pp points to objects of type point3 */
point3 ap[NELEMS];       /* each (of NELEMS) element of ap is of type point3 */

pp = malloc(NELEMS * sizeof *pp);
if (pp) {
    /* use pp */
    pp[0][0] = computeX(); pp[0][1] = computeY(); pp[0][2] = computeZ();
    (*(pp+42))[0] = computeX(); (*(pp+42))[1] = computeY(); (*(pp+42))[2] = computeZ();
    free(pp);
}

ap[0][0] = computeX(); ap[0][1] = computeY(); ap[0][2] = computeZ();

7 Comments

I'm assuming that (*(pp+42)) is like array[42]?
Yes, same thing. You can use the array subscript syntax instead. I just wanted to show the pointer syntax.
This line pp = malloc(NELEMS * sizeof pp); in my code is like this pp = malloc(NN * sizeof pp); where N is const int N = 20; And I've got "invalid conversion from 'void' to 'float (*)[3]'"
@Ertai: It sounds like you are using C++ not C. You need to do an explicit cast with C++ on malloc. In fact, you should be using new instead if you are using C++.
I've modified that line to do casting: pp = (point3*)malloc(NN * sizeof *pp); and it compiles... but something is very wrong as I don't get my points right. Could you provide me how a new syntax would be equal to the code above? I've got this: pp = new point3[NN]; and still my points are all wrong (it compiles thou).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.