- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章简单分析C语言中指针数组与数组指针的区别由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
首先来分别看一下,指针数组的一个小例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <stdio.h>
#include <string.h>
int
lookup_keyword(
const
char
*key,
const
char
* table[],
const
int
size)
{
int
ret = -1;
int
i = 0;
for
(i=0; i<size; i++)
{
if
(
strcmp
(key, table[i]) == 0)
{
ret = i;
break
;
}
}
return
ret;
}
#define DIM(array) (sizeof(array)/sizeof(*array))
int
main()
{
const
char
* keyword[] = {
"do"
,
"for"
,
"if"
,
"register"
,
"switch"
,
"while"
,
"case"
,
"static"
,
};
printf
(
"%d\n"
, lookup_keyword(
"static"
, keyword, DIM(keyword)));
return
0;
}
|
数组指针:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
int
main()
{
int
i;
int
* pI = &i;
//普通类型
typedef
int
(AINT5)[5];
AINT5* p1;
int
array[5];
p1 = &array;
//数组指针1
int
(*p2)[5] = &array;
//数组指针2(不建议这样写)
int
(*p3)[4] = &array;
// X 数组指针3(不建议这样写)
return
0;
}
|
。
这两个名字不同当然所代表的意思也就不同。我刚开始看到这就吓到了,主要是中文太博大精深了,整这样的简称太专业了,把人都绕晕了。从英文解释或中文全称看就比较容易理解.
指针数组:array of pointers,即用于存储指针的数组,也就是数组元素都是指针 。
数组指针:a pointer to an array,即指向数组的指针 。
还要注意的是他们用法的区别,下面举例说明.
int* a[4] 指针数组 。
表示:数组a中的元素都为int型指针 。
元素表示:*a[i] *(a[i])是一样的,因为[]优先级高于* 。
int (*a)[4] 数组指针 。
表示:指向数组a的指针 。
元素表示:(*a)[i] 。
注意:在实际应用中,对于指针数组,我们经常这样使用:
1
2
|
typedef
int
* pInt;
pInt a[4];
|
这跟上面指针数组定义所表达的意思是一样的,只不过采取了类型变换.
代码演示如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <iostream>
using
namespace
std;
int
main()
{
int
c[4]={1,2,3,4};
int
*a[4];
//指针数组
int
(*b)[4];
//数组指针
b=&c;
//将数组c中元素赋给数组a
for
(
int
i=0;i<4;i++)
{
a[i]=&c[i];
}
//输出看下结果
cout<<*a[1]<<endl;
//输出2就对
cout<<(*b)[2]<<endl;
//输出3就对
return
0;
}
|
注意:定义了数组指针,该指针指向这个数组的首地址,必须给指针指定一个地址,容易犯的错得就是,不给b地址,直接用(*b)[i]=c[i]给数组b中元素赋值,这时数组指针不知道指向哪里,调试时可能没错,但运行时肯定出现问题,使用指针时要注意这个问题。但为什么a就不用给他地址呢,a的元素是指针,实际上for循环内已经给数组a中元素指定地址了。但若在for循环内写*a[i]=c[i],这同样会出问题。总之一句话,定义了指针一定要知道指针指向哪里,不然要悲剧.
最后此篇关于简单分析C语言中指针数组与数组指针的区别的文章就讲到这里了,如果你想了解更多关于简单分析C语言中指针数组与数组指针的区别的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我在64位linux下使用c++,编译器(g++)也是64位的。当我打印某个变量的地址时,例如一个整数,它应该打印一个 64 位整数,但实际上它打印了一个 48 位整数。 int i; cout <<
我是一名优秀的程序员,十分优秀!