-2

I'm converting C++ code to C for an exercise (we are just learning c++ now), and I am lost at this part.

First, the c++ code:

Point()
{
    x = y = 0;
}

main()
{
    const int N = 200;
    Point *A = new Point[N], sum;    
}

Here's my C version of it:

struct Point //constructor
{
    int x;
    int y;
} Point;

main()
{
    int N = 200;
    Point* A = malloc(N * sizeof(*Point[]));
}

That should give you an idea of what I'm trying to do. Questions:

  1. Is sum in the C++ code the C++ sum function, or is it aPointstruct`?
  2. For allocating the memory in C, I don't think my method works. Should I do a for loop where it mallocs each index of A[]? (A should be an array of Point structs).

Any assistance would be greatly appreciated.

EDIT: Got asked for the context of the code.

Here's the whole C++ program:

#include <iostream>

// a point on the integer grid

struct Point
{
  // constructor
  Point()
  {
    x = y = 0;
  }

  // add point componentwise
  void add(const Point &p)
  {
    x += p.x;
    y += p.y;
  }

  // print to standard output
  void print() const
  {
    std::cout << "[" << x << "," << y << "]" << std::endl;
  }

  // data
  int x, y;
};


int main()
{
  const int N = 200;
  Point *A = new Point[N], sum;

  for (int i=0; i < N; ++i) {
    sum.print();
    A[i].x = i; A[i].y = -i;
    sum.add(A[i]);
  }
  sum.print();
  delete [] A;
}

Ultimately, I have to emulate that in C. Currently stuck at the question I asked: re: what does that line do. I have since figured out that I need to make a struct of Point called sum, and print that after running the add function on all its members.

Murbly
  • 15
  • 2

2 Answers2

2
  1. Is sum in the c++ code the c++ sum function, or is it a Point struct?

In your case, it is a Point struct.

   Point *A = new Point[N], sum;

is equivalent to:

   Point *A = new Point[N];
   Point sum; //I have no idea why naming is sum

If you need Point* for sum, you should write it in the following way:

     Point *A = new Point[N], *sum;
  1. For allocating the memory in C, I don't think my method works

It does not work, syntax is wrong. Try:

EDIT: thanks to @mch, you should not use cast for malloc.

typedef struct Point Point;
Point* A = malloc(N * sizeof(Point));
taocp
  • 23,276
  • 10
  • 49
  • 62
2

In your C version:

struct Point //constructor
{
int x;
int y;
} Point;

should be:

typedef struct //constructor
{
int x;
int y;
} Point;

Because in your case, you defined a global variable named Point.

And, the C programming language has the const keyword as C++ as well, so you can do this in the C language:

const int N = 200;

And the C++ code:

Point *A = new Point[N], sum;

In C version, should be:

Point *A = malloc(N * sizeof(Point)), sum;

But in this version, the memory isn't initialized by zero.

You can use the calloc function instead of the malloc to allocate memory and initialize it with zero:

Point *A = calloc(N, sizeof(Point)), sum;

Then back to your question:

  1. Is sum in the c++ code the c++ sum function, or is it a Point struct?

It is a Point type variable.

  1. For allocating the memory in C, I don't think my method works. Should I do a for loop where it mallocs each index of A[]? (A should be an array of Point structs).

No, there's no necessary to write a for loop. The malloc function will do exactly what you want.

MicroAleX
  • 519
  • 3
  • 10