-2

I have got a question i am working on and this is as follows (with a bunch of constants that aren't relevant)

Use a struct to represent an (x, y) coordinate. A second struct must then be used to represent the sound source — its (x, y) coordinate and the W value. You will also need to declare an appropriate array of struct variables. You may assume that not more the 100 sound sources will be involved in the plant/site being analysed.

This is what i got so far;

    struct point
{
    double x, y;
};
/* for sound sources */
struct source
{
    struct point location;
    float power;
};

Is there anyway to rewrite this in a different way? or a 'more correct' way?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rico
  • 11

2 Answers2

0

There is nothing wrong with what you have written, although it doesn't include the last part of your spec (the array).

There are many other ways to do this; how relatively "correct " they are to this one depends on what criteria you have for comparing them, none of which you have provided. Besides, that kind of discussion is better suited for a code review, and hence a question for a different site.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

That looks fine, but there is another way to write this that I tend to prefer:

typedef struct tagPoint
{
    double x, y;
} point, *pPoint;

typedef struct tagSource
{
    point location;
    float power;
} source, *pSource;

I find this method a bit cleaner, as you do not need to write out the struct each time you use the structure.

Brett Wolfington
  • 6,587
  • 4
  • 32
  • 51
  • But be aware that some view typedef'ing a structure to be a mistake: (http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c) The Linux kernel code style guide, for instance, explicitly disallows it. – Calpau Oct 24 '14 at 19:36
  • That's true, but on the other hand, the Windows APIs use it frequently. I think there is reasonable room for disagreement on this one. – Brett Wolfington Oct 25 '14 at 21:08