0

I am studying for an Intro to C test that requires me to write functions that involve structs. The question only asks me to write the function, but I want to write an entire code that runs so I can put in numbers and see if my code is running properly. I think I wrote the function properly, I just don't know how to code numbers in and have it print out the numbers.

This is the question.

The question.

Here is what I've written for the above question.

struct complex_numb Add_Complex(struct complex_numb C1, struct complex_numb C2){

struct complex_numb C3;

C3.real = C1.real + C2.real;
C3.imaginary = C1.imaginary + C2.imaginary;

return (C3);

};

We aren't using typedef yet.

I thought it would be something like this, but it isnt working.

    #include <stdio.h>

struct complex_numb Add_Complex(struct complex_numb C1, struct complex_numb C2);

int main(){

struct complex_numb{
    float real;
    float imaginary;
};

Add_Complex(1,2,3,4);

printf("%f %f", C3.real, C3.imaginary);

}

struct complex_numb Add_Complex(struct complex_numb C1, struct complex_numb C2){

struct complex_numb C3;
C3.real = C1.real + C2.real;
C3.imaginary = C1.imaginary + C2.imaginary;


return (C3);

};
beginner
  • 31
  • 5
  • 1
    Why are you passing in 4 things as a parameter in Add_Complex? – Omid CompSCI Nov 08 '16 at 04:12
  • 1
    You aren't creating a C1 and a C2 structure and passing those to `Add_Complex()`. You're invoking undefined behaviour, and your compiler should be shrieking at you. If it isn't, you need to turn on more compilation warnings (or get a better/newer compiler). – Jonathan Leffler Nov 08 '16 at 04:12
  • Wouldn't there be four numbers because there are 2 real numbers and 2 imaginary numbers? That was my logic, I'm not really sure as to how it is all put together as I am still learning about functions and structs. – beginner Nov 08 '16 at 04:14
  • @beginner check my answer below working code. – Omid CompSCI Nov 08 '16 at 04:20
  • 1
    Don't define your own types. C provides a `_Complex` type already. – too honest for this site Nov 08 '16 at 04:20
  • 1
    @Olaf I think he is trying to understand basic concept of something like "Operator Overloading" no? – Omid CompSCI Nov 08 '16 at 04:21
  • @OmidCompSCI: C does not allow operators to be overloaded by users. – too honest for this site Nov 08 '16 at 04:22
  • @Olaf I know but it is pseudo op. overloading as you are technically adding two non-primative datatypes the way the user wants. – Omid CompSCI Nov 08 '16 at 04:23
  • @OmidCompSCI: The term "operator overloading" is pretty clear. Using a function **with distinct name** to generate a result from input is clearly not related. Where do you see an overloaded operator or even an overloaded function?? – too honest for this site Nov 08 '16 at 04:30
  • Note: `return` is a statement. The parentheses are not required and potentially troublesome, as they can shadow syntax errors. And "but it isnt working." is not a **specific** problem description. Read [ask] and follow the advice. – too honest for this site Nov 08 '16 at 04:32
  • @Olaf I understand, but this can be seen as "overloading" the + sign even though the + sign is in the form of a function name Add_Complex. You can not simply do Operator overloading in C, and simply compiler does not know what C1 + C2 means, therefore abstracting the addition symbol in a function call that is meant to add the two non-primative datatypes the way he wants it. I understand what you are saying, but isn't this a solution to "operator overloading" in C? – Omid CompSCI Nov 08 '16 at 04:33
  • @Olaf this kind of explains what I am trying to say? http://stackoverflow.com/questions/3417413/operator-overloading-in-c – Omid CompSCI Nov 08 '16 at 04:35
  • @OmidCompSCI: Please read the definition of "operator overloading". with your argumentation, every function would overload something, be it a bunch of operators. It is **not** operator overloading, nor related! – too honest for this site Nov 08 '16 at 04:35
  • @Olaf many people in that link above agree with me. – Omid CompSCI Nov 08 '16 at 04:36
  • @OmidCompSCI: Well, you must see a different site than what I see. Did you even read the answers, including the accepted one?? It is fine you know a term. But you should not use it where inappropriate. That discussion is pointless. You apparently refuse to accept you are wrong. I'm out of this. – too honest for this site Nov 08 '16 at 04:43
  • @Olaf thanks, but the obvious answer is obviously C does not have operator overloading, but there is a theoretical psuedo way of doing it which is what the other answers that have been upvoted explain. Some people want theoretical and other solutions rather than straight up no, because I am pretty sure they already know there is no Op. Overload, but "what is an alternative, and how can I achieve it?" is something that people want to learn about. Thanks. – Omid CompSCI Nov 08 '16 at 05:04
  • @beginner please accept my answer below so others can use it as a reference for a similar question asked. Thanks. – Omid CompSCI Nov 08 '16 at 05:11

2 Answers2

0

Create your C1 and C2, add those values to the attributes inside your struct, and create your return object of complex_numb which called "C3" and have that = to the result of the function call that takes in C1 and C2. Changed your struct using typedef and moved it out of main. Here is a compiling and working version:

#include <stdio.h>

typedef struct {
        float real;
        float imaginary;
    }complex_numb;

complex_numb Add_Complex(complex_numb C1, complex_numb C2);

    int main()
    { 
        complex_numb C1;
        C1.real = 1;
        C1.imaginary = 2;
        complex_numb C2;
        C2.real = 3;
        C2.imaginary = 4;

    //Add_Complex(1,2,3,4);
    complex_numb C3;
    C3 = Add_Complex(C1,C2);
    printf("%f %f", C3.real, C3.imaginary);

    }

 complex_numb Add_Complex(complex_numb C1, complex_numb C2){

    complex_numb C3;
    C3.real = C1.real + C2.real;
    C3.imaginary = C1.imaginary + C2.imaginary;


    return (C3);

    }
Omid CompSCI
  • 1,861
  • 3
  • 17
  • 29
0

Your actual and formal parameters do not match. And when you return c3, you do not store the structure before you try and print its contents.

Obito
  • 79
  • 9