C 指针初始化之间的差异

C 指针初始化之间的差异,c,pointers,C,Pointers,我是在标准公司K&R C发言 鉴于: const char a[] = {1, 2, 3}; const char *p = NULL; 这两种说法是否等效: *p=a p=a 它们中的每一个都位于代码段的第三行 1和2看起来肯定不一样 那么这两者有什么区别呢?没有 p=a初始化指针以指向其他对象(通常它复制另一个指针,或者您将指向引用,alap=&a) *p=a初始化p引用的内容。您正在“取消引用”(查看)p指向的内容。如果p像您的示例中那样指向NULL,您将崩溃(这很好!您不希望意外访问某

我是在标准公司K&R C发言

鉴于:

const char a[] = {1, 2, 3};
const char *p = NULL;
这两种说法是否等效:

  • *p=a

  • p=a

  • 它们中的每一个都位于代码段的第三行

    1和2看起来肯定不一样

    那么这两者有什么区别呢?

    没有

    p=a
    初始化指针以指向其他对象(通常它复制另一个指针,或者您将指向引用,ala
    p=&a

    *p=a
    初始化p引用的内容。您正在“取消引用”(查看)p指向的内容。如果p像您的示例中那样指向NULL,您将崩溃(这很好!您不希望意外访问某些内容并将程序弄乱)

    在这种情况下,
    p=a
    将指向数组a[]的第一个,而
    *p=a
    将尝试更改数组的第一个(它将不起作用;您已将其声明为const)

    这里是C++中的一个小例子,与C.</P>语法几乎相同

    #include <iostream>
    
    int main()
    {
        char arr[5] { 'a', 'b', 'c' }; // arr[3] and arr[4] are set to 0
        char *ptr = arr; //point to 'a'
    
        for (int i = 0; i != 5; i++)
        {
           *ptr = 'f'; //this changes the array
           ptr++; //this changes what the pointer points to; moves it to next in array
        }
    
        for (int i = 0; i != 5; i++)
        {
            std::cout << *ptr << " ";
        }
    
        //outputs f f f f f
    }
    
    #包括
    int main()
    {
    char arr[5]{'a','b','c'};//arr[3]和arr[4]设置为0
    char*ptr=arr;//指向“a”
    对于(int i=0;i!=5;i++)
    {
    *ptr='f';//这会更改数组
    ptr++;//这会更改指针指向的对象;将其移动到数组中的下一个
    }
    对于(int i=0;i!=5;i++)
    {
    标准::coutNo

    p=a
    初始化指针以指向其他对象(通常它复制另一个指针,或者您将指向引用,ala
    p=&a

    *p=a
    初始化p引用的内容。您正在“取消引用”(查看)p指向的内容。如果p像您的示例中那样指向NULL,您将崩溃(这很好!您不希望意外访问某些内容并将程序弄乱)

    在这种情况下,
    p=a
    将指向数组a[]的第一个,而
    *p=a
    将尝试更改数组的第一个(它将不起作用;您已将其声明为const)

    这里是C++中的一个小例子,与C.</P>语法几乎相同

    #include <iostream>
    
    int main()
    {
        char arr[5] { 'a', 'b', 'c' }; // arr[3] and arr[4] are set to 0
        char *ptr = arr; //point to 'a'
    
        for (int i = 0; i != 5; i++)
        {
           *ptr = 'f'; //this changes the array
           ptr++; //this changes what the pointer points to; moves it to next in array
        }
    
        for (int i = 0; i != 5; i++)
        {
            std::cout << *ptr << " ";
        }
    
        //outputs f f f f f
    }
    
    #包括
    int main()
    {
    char arr[5]{'a','b','c'};//arr[3]和arr[4]设置为0
    char*ptr=arr;//指向“a”
    对于(int i=0;i!=5;i++)
    {
    *ptr='f';//这会更改数组
    ptr++;//这会更改指针指向的对象;将其移动到数组中的下一个
    }
    对于(int i=0;i!=5;i++)
    {
    
    std::cout第一个取消引用空指针,并尝试为其分配数组的地址。这将是一个编译器错误,因为
    char!=char[]
    。否则,它可能会崩溃


    第二个将
    p
    设置为指向数组的指针。

    第一个将取消对空指针的引用,并尝试为其分配数组的地址。这将是一个编译器错误,因为
    char!=char[]
    。如果不是,它可能会崩溃


    第二组将
    p
    设置为指向阵列的方向。

    否,它们不是等效的


    如果
    p=NULL
    ,则执行
    *p=a
    将导致分段错误。

    否,它们不是等价的

     In the left side of declarations you should read *x as pointer(x) while in
     statements it must be read as value_pointed_by(x). &x on the other hand
     would be pointer_to(x)
    
    如果
    p=NULL
    ,则执行
    *p=a
    将导致分段错误

     In the left side of declarations you should read *x as pointer(x) while in
     statements it must be read as value_pointed_by(x). &x on the other hand
     would be pointer_to(x)
    
  • 因为“*p”取消了指针的引用,这不会使“p”成为“char**”
  • 这将按预期将“p”指向第一个数组
  • 我想他们不一样

  • 因为“*p”取消了指针的引用,这不会使“p”成为“char**”
  • 这将按预期将“p”指向第一个数组

  • 我想它们是不一样的。

    这是我学习C语言时使用的一个技巧(现在仍然使用)

    每当您在代码中看到变量前面的*时,自动将其读取为“what is pointed by”

    因此,您应该能够很容易地看到,将“p”设置为“a”与将“p指向的对象”设置为“a”非常不同

    另外,由于p应该指向一个字符,如果幸运的话,将char p指向一个字符指针(a)的设置(当前内存位置0处的“char”假设为null为0)在编译时可能会失败(取决于编译器和lint设置,它实际上可能会成功)

    from comment:在函数声明中,如f(char c),我通常尝试将变量名与它的其余部分分开——因此它将是f((char)c)。因此c是char*。与变量定义完全相同

    另外&通常读作“的地址”,但这变得更加不确定。一些我自己读东西的例子。可能对你有帮助,也可能没有帮助

    int a[] = {1,2,3}; // I mentally parse this as (int[]) a, so a is an int array.
    int *p;            // p is a pointer to "integers"
    int i;
    p=a;               // p acts exactly as a does now. 
    
    i=*p;          // i is "What is pointed to by" p (1)
    i=p;           // i is some memory address
    i=*a;          // i is what is pointed to by a (1)
    i=p[1];        // Don't forget that * and [] syntax are generally interchangable.
    i=a+1;         // Same as above (2).
    p=&i;          // p is the address of i (it can because it's a pointer)
                   // remember from hs algebra that = generally reads as "is", still works!
    *p=7;          // what is pointed to by p (i) is 7;
    a=*i;          // whoops, can't assign an array.  This is the only difference between
                   // arrays and pointers that you will have to deal with often, so feel
                   // free to use which ever one you are more comfortable with.
    
    char c='a';
    char *  d = &c;// d is a char pointer, and it is the address of c
    char ** e ;    // e is a pointer to a memory location containing
                   // a pointer to a char!
    e=&d;          // gets d's address. a pointer to a pointer gets
                   // the address of a pointer.  Messy but gets the job done
    
    **e=5;         // what is pointed to by what is pointed to by e is 5.
    *e=&'f';       // what is pointed to by e (which is a char * itself, and is still d!)
                   // is set to the address of the memory location holding the value 'f'.
                   // does not change c or e, just d! 
    

    我已经10年没有接触过c了,所以其中一些可能有点错误,但它可以帮助我把它大声读出来。

    这是我在学习c时使用的一个技巧(现在仍然使用)

    每当您在代码中看到变量前面的*时,自动将其读取为“what is pointed by”

    因此,您应该能够很容易地看到,将“p”设置为“a”与将“p指向的对象”设置为“a”非常不同

    另外,由于p应该指向一个字符,如果幸运的话,将char p指向一个字符指针(a)的设置(当前内存位置0处的“char”假设为null为0)在编译时可能会失败(取决于编译器和lint设置,它实际上可能会成功)

    from comment:在函数声明中,如f(char c),我通常尝试将变量名与它的其余部分分开——因此它将是f((char)c)。因此c是char*。与变量定义完全相同

    另外&通常读作“的地址”,但这变得更加不确定。一些我自己读东西的例子。可能对你有帮助,也可能没有帮助

    int a[] = {1,2,3}; // I mentally parse this as (int[]) a, so a is an int array.
    int *p;            // p is a pointer to "integers"
    int i;
    p=a;               // p acts exactly as a does now. 
    
    i=*p;          // i is "What is pointed to by" p (1)
    i=p;           // i is some memory address
    i=*a;          // i is what is pointed to by a (1)
    i=p[1];        // Don't forget that * and [] syntax are generally interchangable.
    i=a+1;         // Same as above (2).
    p=&i;          // p is the address of i (it can because it's a pointer)
                   // remember from hs algebra that = generally reads as "is", still works!
    *p=7;          // what is pointed to by p (i) is 7;
    a=*i;          // whoops, can't assign an array.  This is the only difference between
                   // arrays and pointers that you will have to deal with often, so feel
                   // free to use which ever one you are more comfortable with.
    
    char c='a';
    char *  d = &c;// d is a char pointer, and it is the address of c
    char ** e ;    // e is a pointer to a memory location containing
                   // a pointer to a char!
    e=&d;          // gets d's address. a pointer to a pointer gets
                   // the address of a pointer.  Messy but gets the job done
    
    **e=5;         // what is pointed to by what is pointed to by e is 5.
    *e=&'f';       // what is pointed to by e (which is a char * itself, and is still d!)
                   // is set to the address of the memory location holding the value 'f'.
                   // does not change c or e, just d! 
    
    我已经10年没有接触过c了,所以其中一些可能有点错误,但这样可以帮助我大声读出它。

    char a[] = {'a', 'b', 'c'};
    a[0] = 'Z';
    
    int number = 42;
    int pointer = &number;
    printf("%d", *pointer);
    
    char a[8];
    char *p=a;
    
    char a[8];
    char *p=NULL;
    p=a;
    
    char a[8];
    char *p=NULL;
    *p=a;
    
     In the left side of declarations you should read *x as pointer(x) while in
     statements it must be read as value_pointed_by(x). &x on the other hand
     would be pointer_to(x)