Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将变量赋给指针与将变量赋给指针_Arrays_C_Pointers - Fatal编程技术网

Arrays 将变量赋给指针与将变量赋给指针

Arrays 将变量赋给指针与将变量赋给指针,arrays,c,pointers,Arrays,C,Pointers,我现在正在学习指针。这是一本书中的示例代码: #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_

我现在正在学习指针。这是一本书中的示例代码:

#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;
        }
}

编辑-输入错误。是的,这与上面显示的行相同

A.来自C11标准6.3.2.1p3[增加强调]

3除非它是sizeof运算符的操作数、_Alignof运算符或一元&运算符,或者是用于初始化数组的字符串文字,否则类型为array of type的表达式将转换为类型指针指向类型的表达式,该类型指针指向数组对象的初始元素,而不是左值

因此,在这些声明中

        char_ptr = char_array;
        int_ptr = int_array;

char\u数组被转换为指向char\u数组初始元素的char*类型指针,int\u数组被转换为指向int\u数组初始元素的int*类型指针。

A。是的,这与上面显示的行相同

A.来自C11标准6.3.2.1p3[增加强调]

3除非它是sizeof运算符的操作数、_Alignof运算符或一元&运算符,或者是用于初始化数组的字符串文字,否则类型为array of type的表达式将转换为类型指针指向类型的表达式,该类型指针指向数组对象的初始元素,而不是左值

因此,在这些声明中

        char_ptr = char_array;
        int_ptr = int_array;
char_数组转换为指向char_数组初始元素的char*类型指针,int_数组转换为指向int_数组初始元素的int*类型指针

Q.will char*char\u ptr=char\u数组;更改此代码的工作方式 以任何方式,或者这与上面显示的线条是一样的吗

*char_ptr=char_数组;与上面显示的线条相同

问:本书中的其他示例代码也将&variable赋值给指针,据我所知,这意味着将变量的地址存储到该指针,例如char\u ptr=&char\u array;。此示例仅将变量本身分配给指针,但此程序仍然能够打印指针指向的内存地址。为什么或为什么不使用char\u ptr=&char\u数组;在这种情况下?还是说这没什么区别

> 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$
这里要做的是将变量的地址、数组分配给指针。为了获取数组的地址,只需使用数组名称,因此不需要&运算符来获取数组的地址。有关更多详细信息,请参阅此

如果要使用指针指向变量,则使用&运算符。请参阅此部分以了解有关*和(&O)运算符的更多信息

字符*p1; char*p2; 字符变量; char-arr[5]; p1=&var p2=arr; 编辑:添加另一种初始化指针的方法

字符变量; char-arr[5]; char*p1=&var;//char*p1=var不正确 char*p2=arr; Q.will char*char\u ptr=char\u数组;更改此代码的工作方式 以任何方式,或者这与上面显示的线条是一样的吗

*char_ptr=char_数组;与上面显示的线条相同

问:本书中的其他示例代码也将&variable赋值给指针,据我所知,这意味着将变量的地址存储到该指针,例如char\u ptr=&char\u array;。此示例仅将变量本身分配给指针,但此程序仍然能够打印指针指向的内存地址。为什么或为什么不使用char\u ptr=&char\u数组;在这种情况下?还是说这没什么区别

> 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$
这里要做的是将变量的地址、数组分配给指针。为了获取数组的地址,只需使用数组名称,因此不需要&运算符来获取数组的地址。有关更多详细信息,请参阅此

如果要使用指针指向变量,则使用&运算符。请参阅此部分以了解有关*和(&O)运算符的更多信息

字符*p1; char*p2; 字符变量; char-arr[5]; p1=&var p2=arr; 编辑:添加另一种初始化指针的方法

字符变量; char-arr[5]; char*p1=&var;//char*p1=var不正确 char*p2=arr; 第一季度:

Q.will char*char\u ptr=char\u数组;以任何方式或方式更改此代码的功能 这和上面显示的线是一样的吗

答:两者都一样。 在第一个问题中,char_ptr声明和初始化在同一行中完成,在第二个问题中,它们分别完成

第二季度:

为什么或为什么不使用char\u ptr=&char\u数组;在这种情况下?还是说这没什么区别

> 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$
答:在上面显示的代码中,当您使用其名称分配给指针时,char_数组是一个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打印数组起始元素的地址。 表情

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;
char*char_ptr=&char_数组[0];和char*char\u ptr=&char\u数组;不同,但它们为Q1提供了相同的地址

Q.will char*char\u ptr=char\u数组;更改此代码的功能 无论如何 这和上面显示的线是一样的吗

答:两者都一样。 在第一个问题中,char_ptr声明和初始化在同一行中完成,在第二个问题中,它们分别完成

第二季度:

为什么或为什么不使用char\u ptr=&char\u数组;在这种情况下?还是说这没什么区别

> 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$
答:在上面显示的代码中,当您使用其名称分配给指针时,char_数组是一个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打印数组起始元素的地址。 表情

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;

char*char_ptr=&char_数组[0];和char*char\u ptr=&char\u数组;不同,但它们给出相同的地址

假设此语句中有输入错误int_ptr=int_ptr=1;你的意思是int_ptr=int_ptr+1@H.S.是的,你是对的-编辑假设这句话中有一个拼写错误int_ptr=int_ptr=1;你的意思是int_ptr=int_ptr+1@H.S.是的,您是正确的-编辑以确保我理解:在本上下文中,char\u ptr[2]是否指向char\u array[2]?在本语句之后,char\u ptr=char\u array;,char_ptr将指向char_数组的第一个元素,即[0]索引处的元素。为确保我理解:char_ptr[2]是否在此上下文中指向char_数组[2]?在该语句之后,char_ptr=char_数组;,char_ptr将指向char_数组的第一个元素,即[0]索引处的元素。因此,不适合将变量本身赋值为int*p1=integer1,而integer1是int?所显示的是,本书中的示例代码仅适用于将数组分配给指针时?指针让我的大脑有点混乱lol@raincouver我刚刚更新了我的答案。希望这有助于您将变量本身赋值为int*p1=integer1而integer1是int不合适吗?所显示的是,本书中的示例代码仅适用于将数组分配给指针时?指针让我的大脑有点混乱lol@raincouver我刚刚更新了我的答案。希望这有助于理解更多的含义,因此数组本质上就像指向特定指定元素的指针。。我现在对数组有了更好的理解,这就更有意义了,所以数组的工作原理就像一个指针,指向指定的元素saddress。。我现在有了更好的理解