简体   繁体   English

将变量分配给指针与将 & 变量分配给指针

[英]Assigning Variable to Pointer vs Assiging &variable to Pointer

I am now learning about pointers.我现在正在学习指针。 This is the sample code from a book:这是一本书的示例代码:

#include <stdio.h>

int main()
{
        int i;

        char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
        int int_array[5] = {1, 2, 3, 4, 5};

        char *char_ptr;
        int *int_ptr;

        char_ptr = char_array;
        int_ptr = int_array;

        for(i=0; i < 5; i++) {
                printf("[integer pointer] points to %p, which contains the integer %d\n", int_ptr, *int_ptr + 1);
                int_ptr = int_ptr + 1;
        }

        for(i=0; i <5; i++) {
                printf("[char pointer] points to %p, which contains the char '%c'\n", char_ptr, *char_ptr);
                char_ptr = char_ptr + 1;
        }
}

Q. Would char *char_ptr = char_array; Q. char *char_ptr = char_array; change how this code functions in any way or is this the same thing as lines shown above?以任何方式更改此代码的功能,还是与上面显示的行相同?

Q. Other sample codes from this book also assigns &variable to a pointer, which from I understand means to store the address of a variable to that pointer, (eg char_ptr = &char_array; ).问:本书中的其他示例代码也将 &variable 分配给了一个指针,据我了解,这意味着将变量的地址存储到该指针(例如char_ptr = &char_array; )。 This sample just assigns the variable itself to the pointer, but this program still manages to print the memory addresses that the pointer points to.此示例仅将变量本身分配给指针,但该程序仍设法打印指针指向的 memory 地址。 Why or why not use char_ptr = &char_array;为什么或为什么不使用char_ptr = &char_array; in this context?在这种情况下? Or does it not make a difference?还是没有区别?

> kingvon@KingVon:~/Desktop/asm$ gcc pointertypes.c
> kingvon@KingVon:~/Desktop/asm$ ./a.out [integer pointer] points to
> 0x7ffc225227a0, which contains the integer 2 [integer pointer] points
> to 0x7ffc225227a4, which contains the integer 3 [integer pointer]
> points to 0x7ffc225227a8, which contains the integer 4 [integer
> pointer] points to 0x7ffc225227ac, which contains the integer 5
> [integer pointer] points to 0x7ffc225227b0, which contains the integer
> 6 [char pointer] points to 0x7ffc225227c3, which contains the char 'a'
> [char pointer] points to 0x7ffc225227c4, which contains the char 'b'
> [char pointer] points to 0x7ffc225227c5, which contains the char 'c'
> [char pointer] points to 0x7ffc225227c6, which contains the char 'd'
> [char pointer] points to 0x7ffc225227c7, which contains the char 'e'
> kingvon@KingVon:~/Desktop/asm$

Edit- typo编辑 - 错字

A. Yes, this is same things as lines shown above. A. 是的,这与上面显示的行相同。

A. From C11 Standards#6.3.2.1p3 [emphasis added] A. 来自 C11 Standards#6.3.2.1p3 [强调添加]

3 Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 3 除非它是 sizeof 运算符、_Alignof 运算符或一元 & 运算符的操作数,或者是用于初始化数组的字符串文字,否则类型为 ''array of type'' 的表达式将转换为表达式类型为“指向类型的指针”,它指向数组 object 的初始元素,并且不是左值。 .... ……

So, in these statements所以,在这些陈述中

        char_ptr = char_array;
        int_ptr = int_array;

char_array is converted to pointer of type char * that points to the initial element of the char_array and int_array is converted to pointer of type int * that points to the initial element of the int_array . char_array被转换为指向 char_array 的初始元素的char *类型的指针,而int_array被转换为指向char_array的初始元素的int *类型的int_array

For Q1:对于第一季度:

Q. Would char *char_ptr = char_array; Q. char *char_ptr = char_array; change how this code functions in any way or is this the same thing as lines shown above?以任何方式更改此代码的功能,还是与上面显示的行相同?

char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
char *char_ptr; // declaration
char_ptr = char_array; // assignment

Ans: Both are same.答:两者是一样的。 In the first one(in question) char_ptr declaration and initialization are done in same line and in second one they are done separately.在第一个(有问题的) char_ptr声明和初始化在同一行中完成,在第二个中它们分别完成。

For Q2:对于第二季度:

Why or why not use char_ptr = &char_array;为什么或为什么不使用 char_ptr = &char_array; in this context?在这种情况下? Or does it not make a difference?还是没有区别?

Ans: In the code above shown char_array is an array of 5 characters, when you use its name to assign to a pointer it basically means you are assigning address of its first element, so the expressions Ans:在上面显示的代码中char_array是一个 5 个字符的数组,当你使用它的名字来分配一个指针时,它基本上意味着你正在分配它的第一个元素的地址,所以表达式

char_ptr = char_array; // 1. decl & init done separately
char *char_ptr = char_array; //2. decl & init done same line
char *char_ptr = &char_array[0]; //3. same as 2
char *char_ptr = &char_array; // 4. this is not the correct way will give you warning about this usage, its needs char (*)[5] ptr;

all print the address of the starting element of the array. all 打印数组起始元素的地址。 The expressions表达式

char *char_ptr = &char_array[0]; and char *char_ptr = &char_array;char *char_ptr = &char_array; are different but they give the same address不同但他们给出相同的地址

Q. Would char *char_ptr = char_array; Q. char *char_ptr = char_array; change how this code functions in any way or is this the same thing as lines shown above?以任何方式更改此代码的功能,还是与上面显示的行相同?

*char_ptr = char_array; is the same thing as the lines shown above.与上面显示的行相同。


Q. Other sample codes from this book also assigns &variable to a pointer, which from I understand means to store the address of a variable to that pointer, (eg char_ptr = &char_array;).问:本书中的其他示例代码也将 &variable 分配给了一个指针,据我了解,这意味着将变量的地址存储到该指针(例如 char_ptr = &char_array;)。 This sample just assigns the variable itself to the pointer, but this program still manages to print the memory addresses that the pointer points to.此示例仅将变量本身分配给指针,但该程序仍设法打印指针指向的 memory 地址。 Why or why not use char_ptr = &char_array;为什么或为什么不使用 char_ptr = &char_array; in this context?在这种情况下? Or does it not make a difference?还是没有区别?

What you want to do here is assigning the address of a variable (an array) to a pointer .您在这里要做的是将变量(数组)的地址分配给指针 In order to get the address of an array, you simply use the array name, so you don't need the & operator to get the address of an array.为了获取数组的地址,您只需使用数组名称,因此您不需要&运算符来获取数组的地址。 Refer to this link for more details.有关更多详细信息,请参阅此链接

The & operator is used in case you want to use a pointer to point to a variable.如果您想使用指针指向变量,则使用&运算符。 Refer to this article to understand more about * and & operators请参阅本文以了解有关*&运算符的更多信息

char *p1;
char *p2;
char var;
char arr[5];

p1 = &var
p2 = arr;

Edit : Add another way to initialize pointer编辑:添加另一种初始化指针的方法

char var;
char arr[5];

char *p1 = &var; // char *p1 = var is not correct
char *p2 = arr;

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

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