c - C 编程中潜在的指针问题

标签 c pointers struct

我一直在解决这个问题,并且我相信我的结构中的指针指向的位置有错误。但是,我似乎无法弄清楚我哪里出了问题。

我的代码旨在接受 2 个复数,然后将它们相乘和相除,然后输出两个答案。

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

struct complex_t
{
  double real;      // real part  
  double imag;      // imaginary part
} complex_t;

// Multiplication Function //

void *multiply()
{
struct complex_t a, b, c;

      c.real == ((a.real * b.real) - (a.imag * b.imag));
      c.imag == ((a.imag * b.real) + (a.real * b.imag));

      if ( c.imag >= 0 )                            
        printf("Multiplication = %d + %di\n", c.real, c.imag);              // Postive Imaginary case

      else                                  
        printf("Multiplication = %d %di\n", c.real, c.imag);                // Negative Imaginary case
}

// Division Function //

void *divide()
{
int numer1, numer2, denom;

struct complex_t a, b, c;

if ( b.real == 0 || b.imag == 0 )                       // Case to Avoid Dividing by 0
        printf("Division by 0 + 0i is not allowed.");

else
   {
        numer1 = ((a.real * b.real) + (a.imag * b.imag));
        numer2 = ((a.imag * b.real) - (a.real * b.imag));
         denom = ((b.real * b.real) + (b.imag * b.imag));

    c.real == (numer1/denom);
    c.imag == (numer2/denom);


          if (numer2/denom >= 0)                            
            printf("Division       = %d + %di \n", c.real, c.imag);     // Postive Imaginary case
          else                                      
            printf("Division       = %d   %di \n", c.real, c.imag);     // Negative imaginary case

        }
      }


// Main - to execute the two functions // 

int main() {  

struct complex_t a, b, c;

      printf("Enter a and b where a + ib, for the first complex number.");
      printf("\na = ");
      scanf("%d", &a.real);
      printf("b = ");
      scanf("%d", &a.imag);

      printf("Enter c and d where c + id, for the second complex number.");
      printf("\nc = ");
      scanf("%d", &b.real);
      printf("d = ");
      scanf("%d", &b.imag);

multiply();
divide();
return 0;
}

这是该程序生成的示例:

乘法 = 69144 -4196352i

除法 = -13339222 0i

任何关于我可以从哪里开始解决这个错误的提示都会很棒。

最佳答案

C 是一种精确的语言。不存在足够接近的语法之类的东西。这是C语言的一大优势,但它是初学者必须接受和理解的一个障碍任何真正的学习都可以发生。这包括了解每行的每个部分的作用,包括格式字符串。如果您不完全理解每行的每个部分的作用 - 查找。阅读手册页,搜索更多信息,直到您找到为止。从长远来看,这将为您节省大量时间。

最重要的是,您可以做的一件事是在启用警告的情况下进行编译,这将帮助您发现代码中的问题。这意味着至少包含-Wall -Wextra 在你的编译字符串中。例如,在您的代码中,屏幕上充斥着警告,包括没有明显功能的代码expected double but has int。这些事情告诉您,您可以尝试运行您的代码 - 但不要指望它能正常工作。您必须修复这些问题,然后才能有合理的信心从代码中得到的不仅仅是垃圾(或崩溃)。

必须进行的另一个主要学习是始终初始化变量(如果没有其他情况,则初始化为零)。尝试访问未初始化的变量是未定义行为。 (任何人都可以猜测会发生什么。)

话虽这么说。您的部分代码是正确的。您的问题基本上需要放慢,阅读编译器告诉您错误的内容,修复它,然后重试。这就是 C 的关键,放慢速度并做好

废话够多了——你到底要不要帮忙?当然。阅读以下内容。了解为什么需要进行更改,您将能够认为这是今天的一个很好的学习。然而,下面代码中的修复并不像上面的 C 编程指南那么重要。 (授人以鱼......):

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

typedef struct {
    double real;        // real part
    double imag;        // imaginary part
} complex_t;

// Multiplication Function //

void multiply (complex_t *a, complex_t *b, complex_t *c) {
    /* struct complex_t a, b, c; */

    c->real = ((a->real * b->real) - (a->imag * b->imag));
    c->imag = ((a->imag * b->real) + (a->real * b->imag));

    if (c->imag >= 0)
        printf ("\nMultiplication = %f + %fi\n", c->real, c->imag); // Postive Imaginary case

    else
        printf ("\nMultiplication = %f %fi\n", c->real, c->imag);   // Negative Imaginary case
}

// Division Function //

void divide (complex_t *a, complex_t *b, complex_t *c) {
    int numer1, numer2, denom;

    /* struct complex_t a, b, c; */

    if (b->real == 0 || b->imag == 0)   // Case to Avoid Dividing by 0
        printf ("Division by 0 + 0i is not allowed.");

    else {
        numer1 = ((a->real * b->real) + (a->imag * b->imag));
        numer2 = ((a->imag * b->real) - (a->real * b->imag));
        denom = ((b->real * b->real) + (b->imag * b->imag));

        c->real = (numer1 / denom);
        c->imag = (numer2 / denom);


        if (numer2 / denom >= 0)
            printf ("\nDivision       = %f + %fi \n", c->real, c->imag);    // Postive Imaginary case
        else
            printf ("\nDivision       = %f   %fi \n", c->real, c->imag);    // Negative imaginary case

    }
}


// Main - to execute the two functions //

int main () {

    complex_t a = { 0, 0 }, b = { 0, 0 }, c = { 0, 0 };

    printf ("\nEnter a and b where a + ib, for the first complex number.\n\n");
    printf ("  a (a.real) = ");
    scanf ("%lf", &a.real);
    printf ("  b (a.imag) = ");
    scanf ("%lf", &a.imag);

    printf ("\nEnter c and d where c + id, for the second complex number.\n\n");
    printf ("  c (b.real) = ");
    scanf ("%lf", &b.real);
    printf ("  d (b.imag) = ");
    scanf ("%lf", &b.imag);

    multiply (&a, &b, &c);
    divide (&a, &b, &c);

    printf ("\n");

    return 0;
}

输出:

$ ./bin/divmult

Enter a and b where a + ib, for the first complex number.

  a (a.real) = 10
  b (a.imag) = 3

Enter c and d where c + id, for the second complex number.

  c (b.real) = 5
  d (b.imag) = 5

Multiplication = 35.000000 + 65.000000i

Division       = 1.000000 + 0.000000i

关于c - C 编程中潜在的指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27181962/

相关文章:

ios - 使用结构根据第一个选择器 View 过滤第二个和第三个选择器 View

C 中的相互依赖定义

c - 警告 : initialization makes integer from pointer without a cast

c++ - 将结构插入集合 C++ 时遇到问题

c# - 在 C# 中使用指针

c - 函数中声明的结构指针

c++ - 释放指向结构的指针的内存

谁能帮助我纠正这个程序的编写并运行这个程序?

c++ - 在文本文件中搜索字符串

c - 如何在特定位置打印文本?