使用字符串数组:指针数组 - 他们喜欢多维数组? [英] Using arrays of character strings: arrays of pointers - Are they like multidimensional arrays?

查看:155
本文介绍了使用字符串数组:指针数组 - 他们喜欢多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读C ++傻瓜最近,要么标题是用词不当或者他们没有指望我。在一个关于利用指针数组与字符的字符串节他们表现上,我已经完全难倒,不知道要找谁功能。

I've been reading C++ for dummies lately and either the title is a misnomer or they didn't count on me. On a section about utilizing arrays of pointers with characters strings they show a function on which I've been completely stumped and don't know where to turn.

char* int2month(int nMonth)
{
//check to see if value is in rang
if ((nMonth < 0) || (nMonth > 12))
    return "invalid";

//nMonth is valid - return the name of the month
char* pszMonths[] = {"invalid", "January", "February", "March", "April", "May", "June", 
                     "July", "August", "September", "October", "November", "December"};

return pszMonths[nMonth];
} 

第一(但不是主要问题),我不明白为什么返回类型是一个指针,你怎么能没有它走出去的范围返回pszMonths。我在这本书和网上阅读,但我并不在这个例子中得到它。

First of (but not the main question), I don't understand why the return type is a pointer and how you can return pszMonths without it going out of scope. I've read about it in this book and online but I don't get it in this example.

主要的问题我已经是如何做这项工作?!?!。我不明白如何创建一个指针数组,实际上初始化。如果我没有记错你不能用数字数据类型做到这一点。是指针数组像一个阵列本身,包含单个字符从而弥补了词语的每个指针?这整个事情只是博格尔斯我的脑海里。

The main question I have is "how does this work?!?!". I don't understand how you can create an array of pointers and actually initialize them. If I remember correctly you can't do this with numeric data types. Is each pointer in the "array of pointers" like an array itself, containing the individual characters which make up the words? This whole thing just boggles my mind.

8月20日 -
因为似乎我的人试图帮助我在为我的地方其实混乱源于我会尽力解释更好一些混乱。 code的特别,我关心的部分如下:

August 20 - Since there seems to me some confusion by the people trying to help me at as to where my confusion actually stems from I'll try to explain it better. The section of code in particular I am concerned with is the following:

//nMonth is valid - return the name of the month
char* pszMonths[] = {"invalid", "January", "February", "March", "April", "May", "June", 
                 "July", "August", "September", "October", "November", "December"};

我认为,当你犯了一个指针,你只能把它分配给另一个predetermined值。我很困惑,似乎什么是指针(这里的书会)的数组初始化月份名称。我没想到球居然可以初始化值。是数组动态分配的内存?是无效基本上等同于一个新的字符;声明或类似的东西?

I thought that when you made a pointer you could only assign it to another predetermined value. I'm confused that what seems to be an array of pointers (going by the book here) initializes the month names. I did not think pointers could actually initialize values. Is the array dynamically allocating memory? Is "invalid" essentially equivalent to a "new char;" statement or something similar?

我会尝试重新阅读的情况下,他们回答我的问题的职位,但我只是不明白周围的第一次。

I'll try re-reading the posts in case they answered my questions but I just didn't understand the first time around.

推荐答案

好吧,让我们一行在同一时间。

&NBSP;

ok, let's take one line at a time.
 

char* int2month(int nMonth)

这行是最有可能的错误,因为它说,该函数返回一个指针修改字符(根据约定,这将是第一个字符数组元素)。相反,它应该说字符常量* 为const char * 作为结果的类型。这两种规格的意思是完全一样的,即指向一个字符,你不能修改。

&NBSP;

This line is most probably WRONG, because it says the function returns a pointer to a modifiable char (by convention this will be the first char element of an array). Instead it should say char const* or const char* as the result type. These two specifications mean exactly the same, namely a pointer to a char that you cannot modify.
 

{

这仅仅是函数体的左括号。函数体在相应的右括号结束。

&NBSP;

This is just the opening brace of the function body. The function body ends at corresponding closing brace.
 

//check to see if value is in rang

这是一条评论。它是由编译器忽略。

&NBSP;

This is a comment. It is ignored by the compiler.
 

if ((nMonth < 0) || (nMonth > 12))
    return "invalid";

当且仅当如果在的条件成立时执行

下面的收益语句。其目的是处理与不正确的参数值predictable方式。但是,检查是可能的,因为它允许这两个值0和12是有效的,这给总共13有效值,而一个日历年只有12个月。

Here the return statement is executed if and only if the condition in the if holds. The purpose is to deal in a predictable way with incorrect argument value. However, the checking is probably WRONG because it allows both values 0 and 12 as valid, which gives a total of 13 valid values, whereas a calendar year has only 12 months.

顺便说一句,在技术上,对于收益语句指定的返回值是8 字符元素的数组,即7个字符加在最后一个nullbyte。这个数组的的转换为一个指向它的第一要素,这就是所谓的一个类型的衰减。这种特殊的衰减,从字符串的指针的非const 字符,特别是在C ++ 98和C ++ 03,以便与旧的C兼容支持,但在即将到来的C ++ 0x标准无效。

By the way, technically, for the return statement the specified return value is an array of 8 char elements, namely the 7 characters plus a nullbyte at the end. This array is implicitly converted to a pointer to its first element, which is called a type decay. This particular decay, from string literal to pointer to non-const char, is specially supported in C++98 and C++03 in order to be compatible with old C, but is invalid in the upcoming C++0x standard.

这本书不应该教这么丑的东西;使用常量的结果类型。

The book should not teach such ugly things; use const for the result type.

&搜索NBSP;

//nMonth is valid - return the name of the month
char* pszMonths[] = {"invalid", "January", "February", "March", "April", "May", "June", 
                     "July", "August", "September", "October", "November", "December"};

这个数组初始化再次涉及到腐烂。它的指针数组。每个指针初始化为字符串,哪种类型的明智是一个数组,并衰减到指针。

This array initialization again involves that decay. It's an array of pointers. Each pointer is initialized with a string literal, which type-wise is an array, and decays to pointer.

顺便说一句,在PSZpreFIX是一个名为怪物的匈牙利表示法。有人发明了C语言编程,支持微软的程序员工作台帮助系统。在现代编程它没有用处,但实际上只是碟刹最简单的code读像乱码。你真的不希望采用这一点。
结果&NBSP;

By the way, the "psz" prefix is a monstrosity called Hungarian Notation. It was invented for C programming, supporting the help system in Microsoft's Programmer's Workbench. In modern programming it serves no useful purpose but instead just akes the simplest code read like gibberish. You really don't want to adopt that.
 

return pszMonths[nMonth];

此索引有正式的未定义行为,也亲切地称为只是UB,如果 nMonth 是值12,因为有一个在指数12没有数组元素在实际应用中,你会得到一些莫名其妙的结果。

This indexing has formal Undefined Behavior, also known affectionately as just "UB", if nMonth is the value 12, since there is no array element at index 12. In practice you'll get some gibberish result.

修改的:哦,我没有注意到,提交人在前面,这使得13数组元素放在月份名称​​无效。如何掩盖code ...,因为它是非常糟糕和意外,我没有注意到它;为无效的检查做了在功能高。

EDIT: oh I didn't notice that the author has placed the month name "invalid" at the front, which makes for 13 array elements. how to obscure code... i didn't notice it because it's very bad and unexpected; the checking for "invalid" is done higher up in the function.

&搜索NBSP;

} 

这是函数体的右括号。

And this is the closing brace of the function body.

&干杯放大器;心连心,

Cheers & hth.,

这篇关于使用字符串数组:指针数组 - 他们喜欢多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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