Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c - How does this work? Would this work if I wanted to store more than 1 complex number?

It's a task from my college and im hard stuck on this one. It's the beginning of a Mandelbrot programm with different greyscalings but I get lost with structs and typedef really quick. The last part here were I used the "->" was something we had to code but Im still not sure how this works exactly when all have the same destination.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

/** complex numbers have a real and an imaginary part */
typedef struct {
    double real;
    double imag;
} complex_t;

/** constant 0 */
const complex_t complex_0 = {0,0};
/** constant 1 */
const complex_t complex_1 = {1,0};
/** constant i (often called j in electrical engineering) */
const complex_t complex_j = {0,1};


/**
 * Adds two complex numbers
 * @param c0 pointer to the complex number storing the result
 * @param c1 pointer to first number
 * @param c2 pointer to second number
 */
void complex_add( complex_t *c0, complex_t *c1, complex_t *c2 ) {
    c0->imag = c1->imag + c2->imag;
    c0->real = c1->real + c2->real;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

"how this works exactly when all have the same destination"?

The answer is simply: They do not have the same destination.

With an assignment like

c0->imag = c1->imag + c2->imag;

No two of the expressions "have the same destiantion",
at least not with a meaningful, or lets say usual, call.

Let's discuss this example:

complex_t complexA   = complex_1;
complex_t complexB   = complex_1;
complex_t complexSum = complex_0; /* init, later overwritten */

Note that two of those have the same value, but are different variables of type complex_t.

Then usual calling would look like

complex_add( &complexSum, &complexA, &complexB );

Now what happens with ... ?

c0->imag = c1->imag + c2->imag;
c0->real = c1->real + c2->real;

c0->imag access the imaginary part of what c0 is pointing to, i.e. complexSum.imag.
c1->imag access the imaginary part of what c1 is pointing to, i.e. complexA.imag.
c2->imag access the imaginary part of what c2 is pointing to, i.e. complexB.imag.
c0->real access the real part of what c0 is pointing to, i.e. complexSum.real.
c1->real access the real part of what c1 is pointing to, i.e. complexA.real.
c2->real access the real part of what c2 is pointing to, i.e. complexB.real.

See? No two are the same destination.

By the way, I think you can aviod upcoming trouble if you change to

void complex_add(      complex_t  * const cSum,
                  const complex_t * const c1,
                  const complex_t * const c2 );

I trust that the example explained in detail (involving more than one complex variable) also answer the questions in the title.

That will allow changing the content of the output parameter, but nothing else, especially not any of the pointers and not any of the operand values. And that in turn will allow to call with the constants you have shown as parameters for the operands.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...