C语言如何获得数组首地址的指针

C语言如何获得数组首地址的指针,第1张

单维数组名就是首地址,多维数组名就是第1维的首地址,因此取首地址,可以把数组名赋给对应的指针变量,或取索引为0的首个元素地址赋给对应的指针变量。举例:

单维 int arr[10]; int p =arr; 或 int p =&arr[0];

二维 int arr[10][10]; int (p)[10] =arr; 或int (p)[10] = &arr[0];// 取第一维地址

int arr[10][10]; int p= & arr[0][0]; // 取多维中的首个元素

delegate

委托就是函数指针

用delegate就可以了,你可以参考下MSDN中的delegate的用法,

delegate可以定义一个代理,用这个代理可以是对一个函数或方法的代理,实现上就是指针了

比如:

public

delegate

void

someMethod(

int

num,

string

str

);

someMethod

method

=

new

someMethod(

函数名字);

这个method就是函数的指针了

C++

----

C#

传入的char

----string

传出的char

----

StringBuilder(预分配空间)

short

----short

char

----

byte

char[n]

----

fixed

byte[n]

结构指针

----结构指针

函数指针

----

委托

/////////////////////////以下是代码////////////////////

在C#中使用Delegate可以方便的包装Win32非托管dll中的函数指针(回调函数)。

如下面的Win32API:

typedef

void

(PFNCLIENTCONNECTED)(DWORD

clientId,

DWORD

addr);

typedef

void

(PFNCLIENTDISCONNECTED)(DWORD

clientId,

DWORD

addr);

typedef

void

(PFNMESSAGERECEIVED)(DWORD

clientId,

DWORD

addr,

LPCSTR

message);

extern

"C"

__declspec(dllexport)

BOOL

InitializeServer(USHORT

port);

extern

"C"

__declspec(dllexport)

BOOL

UninitializeServer();

extern

"C"

__declspec(dllexport)

void

RegisterClientConnectedCallback(PFNCLIENTCONNECTED

clientConnected);

extern

"C"

__declspec(dllexport)

void

RegisterClientDisconnectedCallback(PFNCLIENTDISCONNECTED

clientDisconnected);

extern

"C"

__declspec(dllexport)

void

RegisterMessageReceivedCallback(PFNMESSAGERECEIVED

messageReceived);

可以使用类似下面的C#代码来进行调用:

class

Win32Wrapper

{

public

delegate

void

ClientConnected(uint

clientID,

uint

address);

public

delegate

void

ClientDisconnected(uint

clientID,

uint

address);

public

delegate

void

MessageReceived(uint

clientID,

uint

address,

StringBuilder

message);

[DllImport("LogCenterdll")]

public

static

extern

bool

InitializeServer(ushort

port);

[DllImport("LogCenterdll")]

public

static

extern

bool

UninitializeServer();

[DllImport("LogCenterdll",

CallingConvention

=

CallingConventionCdecl)]

public

static

extern

int

RegisterClientConnectedCallback(Delegate

callback);

[DllImport("LogCenterdll",

CallingConvention

=

CallingConventionCdecl)]

public

static

extern

int

RegisterClientDisconnectedCallback(Delegate

callback);

[DllImport("LogCenterdll",

CallingConvention

=

CallingConventionCdecl)]

public

static

extern

int

RegisterMessageReceivedCallback(Delegate

callback);

}

就以int类型来说。int a[2][3]={0};

指针数组是int (p)[3]=a; p是一个指针数组,可以放3个元素,去访问指针数组内的值可以用p[0],p[1],p[2],或者a,(a+1),(a+2)去访问

以上就是关于C语言如何获得数组首地址的指针全部的内容,包括:C语言如何获得数组首地址的指针、C# 获取 C++指针函数、如何获取指针数组的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9478486.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存