指向char数组的指针 [英] Pointer to array of char

查看:95
本文介绍了指向char数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个类BookType,它将存储有关书籍的信息。每个

书籍类型最多可以有4位作者。


我为char数组定义了一个新类型:


typedef char array4_t [25];


这将包含25个字符


我在BookType中声明了一个二维char数组:


char m_authors [4] [25];


我本可以使用矢量但这只是一个实际练习。

我有一个SetAuthors函数,它设置接受指针

的作者指向to char(char **)的指针。它运行正常。


我还有一个函数Ge​​tAuthors,它返回一个指向array4_t的指针:


array4_t * BookType :: GetAuthors()

{

返回m_authors;

}

在我的测试程序中,我想循环遍历字符串数组打印

作者。我做到了这一点,但效果并不好:


array4_t * authors = books [i] - > GetAuthors();

while(* authors!= NULL)

{

printf("%s",authors);

authors ++;

}


进入无限循环。不知道原因。

有人可以告诉我返回指向

char数组的指针的最佳方法以及如何在不使用的情况下迭代数组vector(或另一个

STL容器)?

解决方案

techie写道:

我'我正在创建一个类BookType,它将存储有关书籍的信息。每种书籍类型最多可包含4位作者。

我为char数组定义了一种新类型:

typedef char array4_t [25];

这将包含25个字符

我在BookType中声明了一个二维char数组:

char m_authors [4] [25];

我本可以使用vector但这只是一个实际的练习。

我有一个SetAuthors函数,它设置接受指针的作者指向toi char(char * *)。它工作正常。

我还有一个函数Ge​​tAuthors,它返回一个指向array4_t的指针:

array4_t * BookType :: GetAuthors()
{
返回m_authors;
}

在我的测试程序中,我想循环遍历字符串数组并打印
作者。我已经完成了这个但是效果不好:

array4_t * authors = books [i] - > GetAuthors();
while(* authors!= NULL)
{
printf("%s",authors);
authors ++;
}


你设置了''作者在某个地方的一个接一个的价值是什么?

这进入了一个无限循环。不知道为什么。

有人可以告诉我返回指向一个
字符数组的指针的最佳方法,以及如何在不使用向量的情况下迭代数组(或其他
STL容器)?




Mike


techie写道:

我正在创建一个类BookType,它将存储有关书籍的信息。
每种书籍类型最多可包含4位作者。

我为数组定义了一种新类型char:

typedef char array4_t [25];

这将包含25个字符

我在BookType中声明了一个二维char数组:

char m_authors [4] [25];

我本可以使用矢量,但这只是一个实际练习。


是的!使用向量。

我有一个SetAuthors函数,它将接受
指针的作者设置为指向toi char(char **)的指针。它工作正常。

我还有一个函数Ge​​tAuthors,它返回一个指向array4_t的指针:

array4_t * BookType :: GetAuthors()
{
返回m_authors;
}


您正在为班级中的数据提供原始访问权限。任何客户都可以改变价值而不通过SetAuthors(),因为返回类型

这里不是arra4_t const。


In我的测试程序我想循环遍历字符串数组并打印
作者。我已经完成了这个但是效果不好:

array4_t * authors = books [i] - > GetAuthors();
while(* authors!= NULL)


要使此循环终止,您需要在

数组的末尾添加一个0指针。编译器不会自动执行此操作。非常喜欢使用0终止字符串处理

,你必须完成所有的工作。


现在,我打赌你没有放0数组末尾的-pointer;

只是因为我根本没有看到这样做的方法:你的

数组的类型是char [4] [ 25]而不是实际上(char *)[4]。注意以下搞笑

的事情:


typedef char array4_t [25];


int main(void) {

array4_t xxx;

if(xxx == NULL){}; //没有错误

xxx = NULL; //编译错误

}


{
printf("%s",authors);


你真的想用C-way做,不是吗?

作者++;
}



向量和数组的主要区别之一:数组不知道它们的长度是多少。如果你分配一个数组


T * t_arr = new T [n];


你碰巧忘记了n的值,那里将无法从t_arr中检索它

。您必须自己跟踪阵列大小。这可以通过几种方式完成,其中没有一个会被编译器自动实现为




a)保持长度并从t_arr [0]迭代到t_arr [length]

b)作为变体:存储一个过去的结束指针。

c)将一个特殊的终止值放入数组的最后一个插槽。

[0终止的C字符串使用此]

d)其他一些方案。


有人可以告诉我返回指向一个
字符数组的指针以及如何在不使用向量(或其他
STL容器)的情况下遍历数组的最佳方法吗?



最好的方法是使用矢量。

最好


Kai-Uwe Bux


* techie:

我正在创建一个BookType类,用于存储有关书籍的信息。每种书籍类型最多可包含4位作者。

我为char数组定义了一种新类型:

typedef char array4_t [25];

这将包含25个字符


而是使用std :: string。


我声明了一个二维数组char在BookType中:

char m_authors [4] [25];

我本可以使用矢量,但这只是一个实际练习。


使用std :: vector。


我有一个SetAuthors函数,用于设置接受指针指针的作者
toi char(char **)。它工作正常。

我还有一个函数Ge​​tAuthors,它返回一个指向array4_t的指针:

array4_t * BookType :: GetAuthors()
{
返回m_authors;
}

在我的测试程序中,我想循环遍历字符串数组并打印
作者。我已经完成了这个但是效果不好:

array4_t * authors = books [i] - > GetAuthors();
while(* authors!= NULL)


''作者''的类型为''array4_t *''。


''* authors''的类型为'' array4_t''。


在与NULL的比较中,数组被转换为指向其第一个

元素的指针,类型为''char *''。


指向某个数组的第一个元素的指针永远不会为NULL,除非在调用未定义的行为后某处出现

意外。

因此,你得到一个无限循环,你很快就会得到未定义的行为

第三次递增你的''作者'指针(此时你是

访问一个不存在的数组。)

{
printf("%s",authors);


作为初学者,使用更安全的''std :: cout''是个好主意。


authors ++;


养成优先使用前缀

增量而不是后缀的习惯也是一个好主意。


}

这进入了一个无限循环。不知道为什么。


见上文。


有人可以告诉我返回指向数组的指针的最佳方法
char


使用std :: string和std :: vector。

以及如何在不使用向量的情况下迭代数组(或其他
STL容器??




为什么?


-

A:因为它搞砸了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

Q :usenet和电子邮件中最烦人的事情是什么?


I''m creating a class BookType that will store information about books. Each
book type can have up to 4 authors.

I defined a new type for an array of char:

typedef char array4_t[25];

This will hold 25 characaters

I declared a 2 dimensional array of char in BookType:

char m_authors[4][25];

I could have used vector but this just a practical exercise.
I have a SetAuthors function which sets the authors which accepts a pointer
to a pointer toi char (char**). It works fine.

I also have a function GetAuthors that returns a pointer to array4_t:

array4_t* BookType::GetAuthors()
{
return m_authors;
}
In my test program I want to loop through the array of strings and print the
authors. I''ve done this but it doesn''t work well:

array4_t* authors = books[i]->GetAuthors();
while (*authors != NULL)
{
printf(" %s ", authors);
authors++;
}

This goes into an infinite loop. Not sure why.

Can someone please tell me the best way to return a pointer to an array of
char and how to iterate through the array without using vector (or another
STL container)?

解决方案

techie wrote:

I''m creating a class BookType that will store information about books. Each
book type can have up to 4 authors.

I defined a new type for an array of char:

typedef char array4_t[25];

This will hold 25 characaters

I declared a 2 dimensional array of char in BookType:

char m_authors[4][25];

I could have used vector but this just a practical exercise.
I have a SetAuthors function which sets the authors which accepts a pointer
to a pointer toi char (char**). It works fine.

I also have a function GetAuthors that returns a pointer to array4_t:

array4_t* BookType::GetAuthors()
{
return m_authors;
}
In my test program I want to loop through the array of strings and print the
authors. I''ve done this but it doesn''t work well:

array4_t* authors = books[i]->GetAuthors();
while (*authors != NULL)
{
printf(" %s ", authors);
authors++;
}
Have you set the ''one-past-the-end'' value of authors to NULL somewhere??

This goes into an infinite loop. Not sure why.

Can someone please tell me the best way to return a pointer to an array of
char and how to iterate through the array without using vector (or another
STL container)?



Mike


techie wrote:

I''m creating a class BookType that will store information about books.
Each book type can have up to 4 authors.

I defined a new type for an array of char:

typedef char array4_t[25];

This will hold 25 characaters

I declared a 2 dimensional array of char in BookType:

char m_authors[4][25];

I could have used vector but this just a practical exercise.
Yes! Use vectors.
I have a SetAuthors function which sets the authors which accepts a
pointer
to a pointer toi char (char**). It works fine.

I also have a function GetAuthors that returns a pointer to array4_t:

array4_t* BookType::GetAuthors()
{
return m_authors;
}
You are providing raw access to the data in your class. Any client can
change the value without going through SetAuthors() since the returntype
here is not arra4_t const.

In my test program I want to loop through the array of strings and print
the
authors. I''ve done this but it doesn''t work well:

array4_t* authors = books[i]->GetAuthors();
while (*authors != NULL)
For this loop to terminate, you need to put a 0-pointer at the end of your
array. The compiler will not do that automagically. Very much like dealing
with 0-terminated strings, you have to do all the work.

Now, I bet you that you did not put a 0-pointer at the end of the array;
simply because I do not see a way of doing this at all: the type of your
array is char[4][25] and not actually (char*)[4]. Note the following funny
thing:

typedef char array4_t[25];

int main ( void ) {
array4_t xxx;
if ( xxx == NULL ) {}; // no error
xxx = NULL; // compiler error
}

{
printf(" %s ", authors);
You really want to do it the C-way, don''t you?
authors++;
}

This goes into an infinite loop. Not sure why.

One of the key differences of vectors and arrays: arrays have no idea about
their length. If you allocate an array

T* t_arr = new T [n];

and you happen to forget the value of n, there will be no way to retrieve it
from t_arr. You have to keep track of the array size yourself. That can be
done in several ways, none of which will be implemented automagically for
you by the compiler:

a) keep the length and iterate from t_arr[0] to t_arr[length]
b) as a variant: store a past-end pointer.
c) put a special terminating value in the last slot of the array.
[The 0-terminated C-strings use this]
d) some other scheme.

Can someone please tell me the best way to return a pointer to an array of
char and how to iterate through the array without using vector (or another
STL container)?



The best way is to use a vector.
Best

Kai-Uwe Bux


* techie:

I''m creating a class BookType that will store information about books. Each
book type can have up to 4 authors.

I defined a new type for an array of char:

typedef char array4_t[25];

This will hold 25 characaters
Instead use std::string.

I declared a 2 dimensional array of char in BookType:

char m_authors[4][25];

I could have used vector but this just a practical exercise.
Use std::vector.

I have a SetAuthors function which sets the authors which accepts a pointer
to a pointer toi char (char**). It works fine.

I also have a function GetAuthors that returns a pointer to array4_t:

array4_t* BookType::GetAuthors()
{
return m_authors;
}
In my test program I want to loop through the array of strings and print the
authors. I''ve done this but it doesn''t work well:

array4_t* authors = books[i]->GetAuthors();
while (*authors != NULL)
''authors'' is of type ''array4_t*''.

''*authors'' is of type ''array4_t''.

In the comparision with NULL that array is converted to a pointer to its first
element, of type ''char*''.

That pointer to the first element of some array will never be NULL, except by
accident somewhere after invoking Undefined Behavior.

Hence you get an infinite loop, with Undefined Behavior as soon you have
incremented your ''authors'' pointer for the third time (at which time you''re
accessing a non-existent array).
{
printf(" %s ", authors);
As a beginner it''s a good idea to use more type safe ''std::cout''.

authors++;
It''s also a good idea to get into the habit of preferentially using prefix
increment, not postfix.

}

This goes into an infinite loop. Not sure why.
See above.

Can someone please tell me the best way to return a pointer to an array of
char
Use std::string and std::vector.

and how to iterate through the array without using vector (or another
STL container)?



Why?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于指向char数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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