C中的指针,了解差异

时间:2016-07-19 05:36:49

标签: c pointers

if (!utils.toSkipEmail(emailsStr)) {

  meetingSchema.findById(n.meetingId, function(err, meeting) {
    if (meeting.name.displayValue.indexOf('test', 'Test') == -1) {
      numNotes++;
    }
    next();
  });
} else {
  next();
}

我知道指针的名称包含变量的内存地址。

但是,当void noOfClients(struct noOfClients *q ); 带有指针时,它代表该位置的内容。

在上面的代码行中,当通过引用时,我们会说:

*

但为什么?

谢谢。

3 个答案:

答案 0 :(得分:2)

*在变量/参数声明中使用时以及当它用作指针解引用运算符时具有不同的含义。

在变量/参数声明中,它声明变量/参数是指针类型。

struct noOfClients *q

声明q是指向struct noOfClients的指针。

在表达式中使用时,

*q

取消引用q所指的位置。

<强> PS

void noOfClients( &q);

不是调用该函数的正确方法。只需使用:

noOfClients(&q);

如果将q声明为对象,那将会有效。

struct noOfClients q;
noOfClients(&q);

答案 1 :(得分:0)

void func(foo *a);

这是一个函数的函数原型,它带有指向foo的指针。有些人喜欢把它写成

void func(foo* a);

没有区别,但你可能会说该函数采用“foo - 指针”,而不是“指向foo的指针”。

  • afoo*
  • *afoo

没有区别。

答案 2 :(得分:0)

因为&amp;表示特定变量的地址以及处理函数的时间,因此当您将变量传递给函数以便对该变量进行一些更改时,有时值的反映不会在该变量中完成,但如果您使用其传递变量地址然后反思将正确完成只是尝试并理解以下两个代码,它一定会帮助你

没有&amp;

    #include <stdio.h>

    void swap(int, int);

    int main()
    {
       int x, y;

       printf("Enter the value of x and y\n");
       scanf("%d%d",&x,&y);

       printf("Before Swapping\nx = %d\ny = %d\n", x, y);

       swap(&x, &y); 

       printf("After Swapping\nx = %d\ny = %d\n", x, y);

       return 0;
    }

    void swap(int a, int b)
    {
       int temp;

       temp = b;
       b   = a;
       a   = temp;   
    }
  

与&amp;

#include <stdio.h>

void swap(int*, int*);

int main()
{
   int x, y;

   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   swap(&x, &y); 

   printf("After Swapping\nx = %d\ny = %d\n", x, y);

   return 0;
}

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b   = *a;
   *a   = temp;   
}