简体   繁体   English

将结构传递给函数的问题

[英]Issues with passing structures into functions

I have several years of experience with java, but I'm very new to C. I'm still struggling to figure out when and when not to use pointers. 我有几年使用Java的经验,但是我对C还是很陌生。我仍在努力弄清楚何时以及何时不使用指针。 I was given this base code and need to finish the 'push' method so that it pushes the next element onto the stack, but I'm getting errors saying: 给了我这个基本代码,需要完成'push'方法,以便将下一个元素压入堆栈,但是我得到了错误提示:

request for member 'top' in something not a structure. 请求不是结构的成员“ top”。

#include <assert.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>

int exit_status = EXIT_SUCCESS;
#define EMPTY (-1)
#define SIZE 16

typedef struct stack stack;
   struct stack {
   int top;
   double numbers[SIZE];
};

void push (stack *the_stack, double number) {
    if(&&the_stack.top>=SIZE-1){
       printf("error");
    }else{
       the_stack.numbers[&&the_stack.top++ ] = number;
    }
}
int main (int argc, char **argv) {
  if (argc != 1) {
     fprintf (stderr, "Usage: %s\n", basename (argv[0]));
     fflush (NULL);
     exit (EXIT_FAILURE);
  }
stack the_stack;
the_stack.top = EMPTY;
char buffer[1024];
for (;;) {
   int scanrc = scanf ("%1023s", buffer);
   if (scanrc == EOF) break;
   assert (scanrc == 1);
   if (buffer[0] == '#') {
     scanrc = scanf ("%1023[^\n]", buffer);
     continue;
  }
  char *endptr;
  double number = strtod (buffer, &endptr);
  if (*endptr == '\0') {
     push (&the_stack, number);
  }else if (buffer[1] != '\0') {
     bad_operator (buffer);
  }else {
     do_operator (&the_stack, buffer);
  }
}
 return exit_status;
}

Am I overlooking something very basic? 我是否忽略了一些非常基本的内容?

When accessing members of a structure, there are two operators: The dot operator . 访问结构的成员时,有两个运算符:点运算符. which is used for non-pointer structures, and the "arrow" operator -> which is used for pointers to structures. 它用于非指针结构,而“箭头”运算符->用于结构指针。

You have a pointer to a structure in your push function, so you need to use the "arrow" operator. 您在push函数中有一个指向结构的指针,因此您需要使用“箭头”运算符。


There are other problems with your code, like the double ampersand in the push function. 您的代码还有其他问题,例如push函数中的双“&”号。 You also don't need to flush any files when exiting the program, the runtime will do that for you. 您也不需要在退出程序时刷新任何文件,运行时将为您完成此操作。

It seems very clearly that you have less experience in C. There are lots of compile time errors in your code like for example : 

In push function top and numbers should be invoked as : the_stack->top the_stack->numbers[] 在push函数中,top和numbers的调用方式为:the_stack-> top the_stack-> numbers []
An arrow is used to invoke the members of a pointer variable. 箭头用于调用指针变量的成员。

Now Comparing it with Java: Java uses only references not pointers. 现在将其与Java进行比较:Java仅使用引用,而不使用指针。 But internally they are pointing to some location in memory. 但是在内部,它们指向内存中的某个位置。

https://www.dropbox.com/s/qfk86yb3drdlmz2/C%20to%20Java.JPG Image http://erikdemaine.org/papers/CPE98/paper.pdf Paper https://www.dropbox.com/s/qfk86yb3drdlmz2/C%20to%20Java.JPG图片http://erikdemaine.org/papers/CPE98/paper.pdf论文

Check this link to correlate C code and Java code. 检查此链接以关联C代码和Java代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM