如何初始化一个字符数组的指针数组? [英] How to initialize an array of pointers to an array of characters?

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

问题描述

您好,


我正在尝试初始化一个字符数组的指针数组,我可以在3行中完成,但我真的想在一行中完成它同时保持#define。


3行初始化(可以编译)

====================== br />
#define A 1

#define B 2

char row1 [] = {A | B,B,A};

char row2 [] = {B,A};

char * test [] = {row1,row2};


1行初始化(失败)

===============================

char * test [] = {{A | B,B,A},{B,A}}; //< - 我该怎么办?


我不想要这个,因为它浪费了ROM空间

======== =====================================

char test [] [ 3] = {{A | B,B,A},{B,A}};


非常感谢任何人都可以给我一些指示,thnx - paul 。

Hello,

I am trying to initialize an array of pointers to an array of characters, I can do it in 3 lines but I really want to do it in one line at the same time keeping the #define.

3 lines initialization (can compile)
======================
#define A 1
#define B 2
char row1[] = {A|B, B, A};
char row2[] = {B, A};
char *test[]= {row1, row2};

1 line initialization (failed)
===============================
char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??

I do not want this because it waste ROM space
=============================================
char test[][3] = { {A|B, B, A}, {B, A} };


Much appreciate if anyone can give me some pointers, thnx - paul.

推荐答案

第1行有三个元素,但第2行只有两个元素。您打算如何跟踪每行中有多少元素?您基本上有两个选项:明确指定行大小(添加一个字段以指定它)或隐式(硬编码到您的软件逻辑中。
Row 1 has three elements, but row 2 only has two elements. How do you plan to keep track of how many elements are in each row? You basically have two options: specify the row size explicitly (add a field to specify it) or implicitly (hard-coded into the logic of your software.


展开 < span class =codeDivider> | 选择 | Wrap | Line数字


您好donbock,


感谢您的回复。是的,我打算添加一个终止字符来标记结束除此之外,您是否知道如何在一行中完成这项工作?
Hi donbock,

thanks for your reply. yes, i intend to add a termination character to mark the end of the row. That aside, are you aware of how this could be done in one line?


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

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