动态数组和结构体

发布于 2024-10-25 13:17:09 字数 416 浏览 2 评论 0原文

谢谢!我只需要将作业的右侧投射到 Term 即可。

我必须创建一个动态多项式数组,每个多项式都有一个动态项数组。当给该项一个指数和系数时,我收到错误“'{'标记之前的预期表达式”。分配值时我做错了什么?

另外,是否有一种简单的方法可以保持动态术语数组按其指数排序?我只是打算循环遍历,打印最大值,但更愿意按顺序存储它们。

谢谢!

polynomialArray[index].polynomialTerm[0] = {exponent, coefficient};  // ISSUE HERE

更改为

polynomialArray[index].polynomialTerm[0] = (Term){exponent, coefficient};

Thanks! I just had to cast the right side of the assignment to Term.

I have to make a dynamic array of polynomials that each have a dynamic array of terms. When giving the term a exponent and coefficient, I get an error "expected expression before '{' token". What am I doing incorrectly when assigning the values?

Also, is there an easy way of keeping the dynamic array of terms ordered by their exponent? I was just planning on looping through, printing the max value but would prefer to store them in order.

Thanks!

polynomialArray[index].polynomialTerm[0] = {exponent, coefficient};  // ISSUE HERE

change to

polynomialArray[index].polynomialTerm[0] = (Term){exponent, coefficient};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

々眼睛长脚气 2024-11-01 13:17:09
polynomialArray[index].polynomialTerm[0]->exponent = exponent;
polynomialArray[index].polynomialTerm[0]->coefficient = coefficient; 
polynomialArray[index].polynomialTerm[0]->exponent = exponent;
polynomialArray[index].polynomialTerm[0]->coefficient = coefficient; 
五里雾 2024-11-01 13:17:09

您的代码中存在效率问题:

  if(index > (sizeof(polynomialArray)/sizeof(Polynomial)))
      polynomialArray = (Polynomial*)realloc(polynomialArray, index * sizeof(Polynomial));

由于 polynomialArray 是一个指针,我认为 sizeof(polynomialArray) 将始终为 4 或 8(64 位系统)。因此,只要 index 大于 0,上面的 if 语句就始终为 true。

There's an efficiency problem here in your code:

  if(index > (sizeof(polynomialArray)/sizeof(Polynomial)))
      polynomialArray = (Polynomial*)realloc(polynomialArray, index * sizeof(Polynomial));

as polynomialArray is a pointer, I think sizeof(polynomialArray) would always be 4 or 8(64-bit system). So the above if statement will always true as long as index is greater than 0.

瘫痪情歌 2024-11-01 13:17:09

如果这是C99,我想你需要

polynomialArray[index].polynomialTerm[0] = (Term){exponent, coefficient};

If this is C99, I think you need

polynomialArray[index].polynomialTerm[0] = (Term){exponent, coefficient};
染火枫林 2024-11-01 13:17:09

您不能像这样赋予值(仅在声明期间)。

你应该这样分配:

polynomialArray[index].polynomialTerm[0].exponent = exponent;
polynomialArray[index].polynomialTerm[0].coefficient = coefficient;

关于另一个问题,你真的不需要在这里断言。如果指针有一个分配给它的值 malloc,则它不会为 NULL。如果没有,最好为NULL,这样可以测试malloc是否失败。

要对其进行排序,您需要使用某种排序算法进行排序。我认为如果您正在寻找一种简单的方法,那么您所做的方法就很好。如果订购至关重要(例如实时应用程序),那么您需要重新考虑该方法。如果没有,请保留并继续前进!

小心,
贝科

You cannot attribute values like that (only during declaration).

You should assign like this:

polynomialArray[index].polynomialTerm[0].exponent = exponent;
polynomialArray[index].polynomialTerm[0].coefficient = coefficient;

About the other question, you really don't need assert here. The pointer will not be NULL if it has a value malloc allocated to it. If not, it is better to be NULL, so you can test if malloc failed.

To have it ordered, you will need to order using some sort algorithm. I think that if you are looking for an easy way, the way you are doing is fine. If it is critical to be ordered (like real time applications), than you need to rethink the approach. If not, keep it and go forward!

Take care,
Beco

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文