search
HomeBackend DevelopmentC#.Net TutorialWhat is the difference between pointer array and array pointer

What is the difference between pointer array and array pointer

Jan 06, 2023 pm 04:50 PM
c languagearray pointerarray of pointers

区别:对指针数组来说,首先它是一个数组,数组的元素都是指针,也就是说该数组存储的是指针,数组占多少个字节由数组本身决定;而对数组指针来说,首先它是一个指针,它指向一个数组,也就是说它是指向数组的指针,在 32 位系统下永远占 4 字节,至于它指向的数组占多少字节,这个不能够确定,要看具体情况。

What is the difference between pointer array and array pointer

本教程操作环境:windows7系统、c99版本、Dell G3电脑。

一、数组指针和指针数组的定义

1.指针数组:定义 int *p[n];

指针数组可以说成是”指针的数组”,由于*p没有括号,首先这个变量是一个数组,其次,”指针p”修饰这个数组,意思是说这个数组的所有元素都是指针类型,而指针所占的字节数和其类型无关,只与系统有关,在32位系统下,任何类型的指针占据4个字节,在64位系统下,任何类型的指针占据8个字节。

2.数组指针:定义 int (*p)[n];

由于()的优先级高,首先说明p是一个int类型指针,它是指向一个整型(int)的一维数组,这个一维数组的长度是n,也可以说是总共有n个格子。数组指针也称指向一维数组的指针,亦称行指针。

数组指针也可以称为“数组的指针”,首先这个变量是一个指针,其次,”数组”修饰这个指针,意思是说这个指针存放着一个数组的首地址,或者说这个指针指向一个数组的首地址。

What is the difference between pointer array and array pointer

二、数组指针和指针数组的使用

1.数组指针

首先定义一个数组指针,既然是数组的指针,说明是用指针p去指向一个一维数组,即:

int (*p)[[4]];
 //这里定义int类型,p是一个指针,指向一维数组的首地址,这个一维数组大小为4

也就是说,int类型的指针p指向int [[4]]的数组,而数组的每一个元素是一个int类型的变量。

数组指针本质是一个指针,而指针的作用就是用来指向变量的地址,所以数组指针一般可以这样使用(示例):

void fun(int (*P)[4]); //子函数中的形参,数组指针 

a[3][4] = {0}; //主函数中定义的二维数组
fun(a); //主函数调用子函数的实参,是二维数组的首元素首地址

2.指针数组

对于指针数组,也就是“指针的数组”,说明这个数组的所有元素都是指针类型。不妨定义指针p,例如:

int *p[[4]];
 //这里数组长度为4,所有元素均为int类型的指针

代码如下(示例):

void Fun(char** str);    //子函数中的形参,指针数组

int main()
{
	int arr[3][4];
	char* str = "abc";
	char** q = &str;
}

指针数组和数组指针参数传递问题

传递的参数 匹配需要的形参
int *p[4] int **p(指针数组或者二级指针)
int (*p)[4] int (*p)[4](不变)
int [3][4] int (*p)[4](数组指针)
int **p int **p(不变)

三、实际使用的例子

例子:使用指针数组打印出:“sun wu kong”

What is the difference between pointer array and array pointer

// 代码块  打印“sun wu kong”
char str1[] = { 's', 'u', 'n' , '\0' };
char str2[] = { 'w', 'u' ,'\0' };
char str3[] = { 'k', 'o', 'n', 'g' ,'\0' };
char* p[3] = { str1, str2, str3 };

总结

提示:这里对文章进行总结:

1、数组指针简单理解为“数组的指针”,首先这个变量是一个指针,其次,”数组”修饰这个指针,意思是说这个指针存放着一个数组的首地址,或者说这个指针指向一个数组的首地址。

2、指针数组简单理解为“指针的数组”,首先这个变量是一个数组,其次,”指针p”修饰这个数组,意思是说这个数组的所有元素都是指针类型。

3、关于函数传参的问题:传递的参数:int *p[4] -> 匹配需要的形参:int **p(指针数组或者二级指针);传递的参数:int (*p)[4] -> 匹配需要的形参:int (*p)[4](不变)。

【相关推荐:C语言视频教程编程教学

The above is the detailed content of What is the difference between pointer array and array pointer. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.