c - 当调用函数时,当两者都采用指针类型参数时,为什么我们在某个地方需要 & 而在另一个地方不需要 & ?

标签 c pointers data-structures queue function-pointers

    #include <stdio.h>
    #include <stdlib.h>
    #define QUEUESIZE 30

    int qfull(int *r)
    {
        if(*r == QUEUESIZE-1)
            return 1;
        else
            return 0;
    }


    int qempty(int *f,int *r)
    {
        if(*f > *r)
            return 1;
        else
            return 0;
    }

    void enqueue(int item,int q[], int *r)
    {
        if(qfull(r))
        {
            printf("Cannot Insert. Queue full.\n");
            return;
        }

        (*r)++;
        q[*r] = item;
    }

    void dequeue(int q[], int *r, int *f)
    {
        if(qempty(f,r))
        {
            printf("The queue is empty\n");
            return;
        }

        int item_deleted = q[*f];
        (*f)++;

        if(*f > *r)
        {
            *f = 0;
            *r = -1;
        }
    }

    void display(int q[], int *f, int *r)
    {
        if(qempty(f,r))
        {
            printf("Nothing to display.\n");
            return;
        }

        for(int i=*f; i<=(*r); i++)
        {
            printf("%d\n",q[i]);
        }
    }

    int main()
    {
        int f = 0;
        int r = -1;

        int q[QUEUESIZE];

        int item,choice;

        while(1)
        {
            printf("Enter a choice: \n");
            printf("1. Enqueue\n");
            printf("2. Dequeue\n");
            printf("3. Display\n");
            printf("4. Exit\n");

            scanf("%d",&choice);

            switch(choice)
            {
                    case 1:
                    printf("Enter an item: \n");
                    scanf("%d",&item);
                    enqueue(item,q,&r);
                    break;

                    case 2:
                    dequeue(q,&r,&f);
                    break;

                    case 3:
                    display(q,&f,&r);
                    break;

                    default: exit(0);
            }
        }   
    }

这是我的代码。 当我在主程序中调用入队函数并使用 r 而不是 &r 时,它给了我一个 warning .

当我在 enqueue 的定义中调用 qfull() 函数时采用 &r 时,它给了我这个 warning .

我想知道为什么?

最佳答案

When I am calling the enqueue function in the main program

int main()
{
    ...
    int r = -1;
      ...
      enqueue(item,q,&r)

calling qfull() function in the definition of enqueue

void enqueue(int item,int q[], int *r)
{
    if(qfull(r))

int rint * r 没有定义相同的r

前者是一个int,后者是一个指向int的指针。

关于c - 当调用函数时,当两者都采用指针类型参数时,为什么我们在某个地方需要 & 而在另一个地方不需要 & ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41206564/

相关文章:

c - 为什么我的列表只返回最近添加的元素?

c++ - 在 C/C++ 中定义类型化常量

C++ 交换两个 void* 的内容

c - 代码是否自上而下阅读

c - Windows:防止 sleep 模式 - C 中的最小版本

java - JNI 在 android/java 中解析 jstring 或 char* 而不使用 std

pointers - 我可以返回一个结构,该结构使用特征实现中的 PhantomData 来为原始指针添加生命周期而不污染接口(interface)吗?

java - ArrayList<ArrayList<String>> 内存不足(Java 堆空间)。还有其他选择吗?

c++ - 在 std::vector<std::unordered_set<T>> 上使用 std::unique()

c++ - 从跳过列表中删除节点