473,474 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer to array of char

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)?
Sep 25 '05 #1
10 3440
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
Sep 25 '05 #2
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
Sep 25 '05 #3
* 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?
Sep 25 '05 #4
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
Why call it array4_t? Why not char_array_25_t (for instance). Confused
naming is often a symptom of confused thinking. In this case I would you
you are confusing what the type declaration declares (an array of 25)
with how you are going to use it (in an array of 4).

I declared a 2 dimensional array of char in BookType:

char m_authors[4][25];
Having gone to the trouble of declaring array4_t why not use it?

array4_t m_authors[4];

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.
Because *authors never equals NULL presumably. Why exactly are you
expecting it to?

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)?


There is no best way, your way looks fine.

The basic problem is that you have a scheme for storing authors but no
scheme for saying how many authors you have stored (or if you do have
such a scheme you haven't told us about it).

Somewhere in the SetAuthors function (which you didn't post) there
should be someway of saving how many authors you have.

In the loop above you seem to be assuming that the one-past-the-end
author will start with the null character. Is that right?

If that is the case then you need an array of *five* authors. Four for
the actual authors and one extra for the one-past-the-end author. You
also need to to ensure that condition holds in the SetAuthors functions.

If this doesn't solve your problem, post all the relevant code (that
means the SetAuthors function) and state clearly how you expecting the
number of authors to be saved.

john
Sep 25 '05 #5

This goes into an infinite loop. Not sure why.

Because *authors never equals NULL presumably. Why exactly are you
expecting it to?


Not presumably, as Alf pointed out *authors will never ever equal NULL.

You confused me and yourself with your C style code.

Unless you have been absolutely forbidden to do so switch to a vector of
strings. Much easier, that is a good thing.

john
Sep 25 '05 #6
"techie" <te****@nospam.biz> 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];
typedef doesn't make new types; it just renames types.
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;
}
That's not very safe, easy, or "practical" as you put it.
"Practical" would mean using a list of strings (or other
safe, easy technique). (See below for a "practial" solution.)

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)?


I could, but I won't. Don't do that! Why make a headache for yourself?
Instead, do something like this:

#include <iostream>
#include <list>
#include <string>

typedef std::list<std::string> LS;
typedef std::list<std::string>::iterator LSI;

class BookType
{
public:

void AddAuthor(std::string const & A)
{
ListOfAuthors.push_back(A);
}

LS GetAuthors ()
{
return ListOfAuthors;
}

private:
std::list<std::string> ListOfAuthors;
};

int main()
{
BookType CookBooks;
CookBooks.AddAuthor("Child, Julia");
CookBooks.AddAuthor("Puck, Wolfgang");
CookBooks.AddAuthor("Pepin, Jacques");
LS CBAuthors = CookBooks.GetAuthors();
LSI i;
for ( i = CBAuthors.begin() ; i != CBAuthors.end() ; ++i )
{
std::cout << (*i) << std::endl;
}
return 0;
}
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Sep 25 '05 #7
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43*****************@news.individual.net...
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).


Correct! That's why I get an infinite loop.

I changed my program to do this and now I don't get an infinite loop:

array25_t* authors = books[i]->GetAuthors();
char* author = NULL;
int x = 0;
while (*(author = authors[x]) != '\0')
{
printf(" %s ", authors);
x++;
}
Using arrays like this you have to remember to put a terminating NULL or
store the number of elements in the array. I think using vector or list is
far more elegant.

Thanks everyone for your help.
Sep 25 '05 #8
Robbie Hatley wrote:
"techie" <te****@nospam.biz> 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];

typedef doesn't make new types; it just renames types.


....creates an alias for the type.

Ben
--
I'm not just a number. To many, I'm known as a String...
Sep 25 '05 #9
* techie:

I changed my program to do this and now I don't get an infinite loop:

array25_t* authors = books[i]->GetAuthors();
char* author = NULL;
int x = 0;
while (*(author = authors[x]) != '\0')
{
printf(" %s ", authors);
x++;
}


Did you notice that this loop prints the _same_ author again and again?

--
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?
Sep 26 '05 #10
I wrote:
typedef doesn't make new types; it just renames types.
"Ben Pope" replied:
...creates an alias for the type.


Hmmm, yes, that is a better way to phrase it. More clear
than the way I said it.
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Sep 26 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
23
by: Leon Brodskiy | last post by:
Hi, Could please anyone clarify about pointer and array in C? If I have: int arr; The following two commands will be the same: arr and &arr.
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
53
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
10
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.