如何声明指针在C字符数组? [英] How to declare a pointer to a character array in C?

查看:109
本文介绍了如何声明指针在C字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何声明一个指向一个字符数组用C?

How do I declare a pointer to a character array in C?

推荐答案

我猜我给在部分这样的回答:

I guess I'll give this answer in parts:


  1. 下面是一个指向字符的数组S(我以为有10个元素的数组):

  1. Here's a pointer to an array of chars (I assumed a 10-element array):

char (*x)[10];

让我们来打破它,从最基础的:

Let's break it down from the basics:

x

是一个指针:

*x

到一个数组:

(*x)[10]

字符取值:

char (*x)[10]


  • 然而,大多数的时候,你真的不想要一个指向数组的指针,你想要一个指向数组的第一个元素。在这种情况下:

  • However, most of the time you don't really want a pointer to an array, you want a pointer to the first element of an array. In that case:

    char a[10];
    char *x = a;
    char *y = &a[0];
    

    X 是你在寻找什么,是等价的。

    Either x or y are what you're looking for, and are equivalent.

    提示:了解 CDECL 做出对​​自己更容易这些问题。

    Tip: Learn about cdecl to make these problems easier on yourself.

    这篇关于如何声明指针在C字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆