关于C中的多种功能使用功能,声明一个变量,并用它 [英] Using functions in C, declaring a variable and using it in multiple functions

查看:114
本文介绍了关于C中的多种功能使用功能,声明一个变量,并用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个从我的主要别的地方称为链表。此链接列表是由节点。

I want to make a linked list that is called from my main somewhere else. This linked list is made of nodes.

例如,这不是我的code只是简化了它的版本。

For example, this isn't my code just simplified version of it.

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

struct nodeTest
{
    int data;
    struct nodeTest* next;
};

然后,我有另外一个文件尝试使用结构:

Then I have another file trying to use that struct:

 #include <stdio.h>
 #include <stdlib.h>
 #include "nodeTest.h"

 int main(void) {
     struct nodeTest* first = malloc(sizeof(struct nodeTest));
     first->data = 0;
     return 0;
 }

void addLL(int data){
    if (first.data = 0)
    {
        printf("No head\n");
    }
}

我首先要成为第一个元素在链表让我怎么定义的addElement它,利用第一?现在,当我第一次叫我正在一个无法识别的错误。

I want first to be the first element in a linked list so how do I define an addElement to it that utilizes first? Right now when I call first I am getting an unrecognized error.

下面是我进去我的链接列表文件中的错误主要在

Here is where I get the error inside my linked list file after the main

void addLL(int data){
    if (head.data = 0)

我在,如果行得到的错误,头不被认可。

I get the error at the if line, head is not recognized.

推荐答案

右键。你得到这个错误,因为变量才可见从内。有两种方式解决此问题:

Right. You get that error because the variable head is only visible from inside main. There are two ways around this:


  1. 您可以声明作为一个全局变量,外面的任何职能;或

  2. 您可以通过需要它作为一个指针的功能

  1. You can declare head as a global variable, outside of any functions; or
  2. You can pass head to any functions that need it as a pointer

让我们来看看这些选项是如何工作的:

Let's see how these options work:

#include <stdio.h>
#include <stdlib.h>
#include "nodeTest.h"

/* Declare a global variable. The initialization to NULL is redundant
 * since global variables are automatically initialized to zero. But
 * let's be thorough.
 */

struct nodeTest* head = NULL;

int main(void) {
   head = malloc(sizeof(struct nodeTest));
   /* check if head is NULL here. Just in case. */
   head->data = 0;

   addLL(1);

   return 0;
}

/* other functions here */
void addLL(int data) {
   /* since we allocate head in main it should never be NULL and
    * if it is, something is wrong. So assert.
    */
   assert(head != NULL);

   /* Notice: in your original code you had: if (first.data = 0)
    * which is wrong. First of all, there's no variable named first
    * declared anywhere. If you meant head, that should have been
    * head->first since head is a pointer. And lastly, first.data = 0
    * would set first.data to 0, instead of comparing it for equivalency, 
    * and would then cause the if to never execute because the compiler
    * would first set first.data to zero, then check first.data, and never
    * enter the loop since it was zero.
    */
   if (head->data == 0)
   {
      printf("No head");
      return;
   }

   /* other code here */
}

此版本是容易的,需要最小的变化。一个好处是,这需要修改头功能,可以很容易地做到这一点。

This version is easy and requires minimal changes. A benefit is that functions that need to modify head can easily do it.

这确实需要,在不平凡的计划,很多虽然全局变量,这被认为是不好的编程习惯和应该在可能的情况是可以避免的。

It does require, in non-trivial programs, a lot of global variables though, which is considered bad programming practice and ought to be avoided if possible.

#include <stdio.h>
#include <stdlib.h>
#include "nodeTest.h"

int main(void) {
   struct nodeTest* head = malloc(sizeof(struct nodeTest));
   /* check if head is NULL here. Just in case. */
   head->data = 0;

   /* we call addLL, passing the head that we just allocated
    * to it.
    */
   addLL(1, head);
   return 0;
}

void addLL(int data, struct nodeTest *head) {
   if (head == NULL) || (head->data == 0))
   {
      printf("No head");
      return;
   }

   /* other code here */
}

这个方法要求我们通过到每个能需要它的功能。此功能界面略显复杂,但可以让我们更多的灵活性,因为我们并不需要有很多的全局变量的多个列表,我们将在我们的节目。

This method requires that we pass head into every function that can need it. This complicates the function interface slightly but allows us more flexibility because we don't need to have many global variables for the many lists we'll have in our program.

但还有另一个问题。如果任何这些功能需要修改指针(例如,假设你删除列表中的第一个元素),那么你有一个问题。指针(是的不同的的从事物所指向的)是本地的功能,所以它所做的任何更改将在函数外不可见的。

But there is another problem. If any of those functions needs to modify the head pointer (for example, let's say you're deleting the first element in a list) then you have an issue. The pointer (which is distinct from the thing pointed to) is local to the function so any changes made to it will be invisible outside the function.

有两种方法来解决这个问题:

There are two ways to get around this issue:


  1. 接受每个函数作为参数,可能需要改变它必须返回一个指向新的头部。这工作,但它可以是繁琐的实施,很容易出错。

  2. 接受每个函数作为参数,可能需要更改将接受一个双指针:一个指针的指针头部。这种运作良好,但复杂的事情,可能捕捉一些新手程序员措手不及。这也是一个有点容易出错,但没有那么多为previous选项。

  1. Every function that accepts head as a parameter and may need to change it would have to return a pointer to the new head. This works but it can be cumbersome to implement and very prone to error.
  2. Every function that accepts head as a parameter and may need to change it would accept a double pointer: a pointer to a pointer to the head. This works well but complicates things and may catch some novice programmers off-guard. It's also a bit prone to error but not as much as the previous option.

我希望这回答了你的问题,解释了为什么你不能这样做,你做了什么,存在的变通方法。

I hope that this answers your question, explains why you can't do what you did and the workarounds that exist.

这篇关于关于C中的多种功能使用功能,声明一个变量,并用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆