0

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;
}
Luven
  • 13
  • 3
  • 2
    What do you mean by *when all have the same destination* ? Difficult to understand what is your problem. At first glance, everything looks OK here. – Damien Jan 12 '21 at 10:43
  • 1
    Is it the `->` operator you don't understand? `a->b` is shorthand for `(*a).b`. The chapter dealing with pointers in your beginner's C text book explains this very well. – Jabberwocky Jan 12 '21 at 10:48
  • Its the real and imag I kinda struggle to get my head around.The main question I have is: c0->imag = c1->imag + c2->imag does this save my values or what exactly does this do? The thing is that we dont use any books just shortscripts which are on the edge to complete trash – Luven Jan 12 '21 at 11:21
  • Welcome to SO. As Jabberwocky mentioned, `->` is dereferencing the pointer and accessing the given member of the struct. As you combine these 3 operands with a `=` and a `+` the two values are added and stored in first struct. You should take a look at [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to get some beginners text book – Gerhardh Jan 12 '21 at 11:34
  • I get that you point that at the object you defined to store these but I dont see how you would access them again after you did that or is there no need to do that because you are using a function? The code continues with more operations like divivde and subtract in simular fashion to the addition part. Because they are all in funtions is there no need to have multiple saving slots because you only need what is written there by the time you call your funciton? – Luven Jan 12 '21 at 11:50
  • And i'll read that for sure or a book in german if this is above my comprehension for the english language – Luven Jan 12 '21 at 11:51
  • You should be prepared to find most of the specific documentation only in English. – Gerhardh Jan 12 '21 at 12:20

1 Answers1

0

"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.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Ok this makes sense but because you use functions to call these you only need 1 value assigned to them at the time right? – Luven Jan 12 '21 at 12:54
  • I do not understand what you mean by "use functions", I do not see how that describes the example I discussed. And "only need 1 value assigned to them at the time right" is also unclear. By my understanding any variable is always only assigned a single value. A complex value has an imaginary and a real part, is that what you refer to? – Yunnosch Jan 12 '21 at 13:33