Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 04-12-2006, 05:26 AM   #1
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Question about outputing arrays with pointers, then just arrays...

I got this question... after a brief (hour) time skimming through C++ for dummies (I highly don't recommend, for extreme newbies I mean, even after the year or so of off and on programming, it is still a bit difficult from time to tim e (used to be all the time, but it has gradualy got better) to understand Stephen Randy Davis [if he wrote it or not, I have no proof] explanations of certain topics.), I started reading what it had to say in the chapter for pointers. I came across using poitners to access arrays. Well, I got it to work, but Stephen writes that it is more common to see pointers to display arrays then just displaying arrays... example:

// DisplayArray example 1:

void DisplayArray(int intArray[], int nSize)
	int * pArray = intArray;

	cout << "\n\tThe value of the array is:\n";

	for (int i = 0; i < nSize; i++, pArray++)
		cout << i << " : " << *pArray << "\n";
cout << endl;
// DisplayArray example 2:

void DisplayArray(int intArray[], int nSize)

	cout << "\n\tThe value of the array is:\n";

	for (int i = 0; i < nSize; i++)
		cout << i << " : " << intArray[n] << "\n";
cout << endl:
// This block code of main goes for both codes shown above...

int main(int nNumberofArgs, char* pszArgs[])
    int array[] = {4, 3, 2, 1};
    DisplayArray(array, 4);

return 0;
I can see here that it just does the same thing... but Davis says/writes this:
You may think this pointer conversion is silly; however, the pointer version of displayArray() is actually more common among C++ programmers in the know than the array version. For some reason, C++ programmers don't seem to like arrays.

The use of pointers to access arrays is nowhere more common than in accessing of character arrays.
m'kay. Telling me it's more common to see the pointer display function then just then using the array function example, doesn't help me much (if not at all). Davis then goes on to talk about pointer operations to a string... I fail when it comes to trying to figure situations in c++(not all c++ code was like this to me, I picked up some operators/usefull methods of operating code almost immediatly) where an operator or piece of code is usefull to me. I guess it would be ignorant of me to say that he should have stayed on topic and discussed this a little more, I probably wasn't ready for this if I didn't already see what he was trying to explain to the reader, something along the lines of "other ways you can use this pointer array method to your advantage" would have worked to help the reader a bit more on the topic at hand IMO. He may have said this somewhere else in the book (more then likely not before this chapter, for I (re-)read the first good portion of the book several times to grasp the concept of c++ programming. If he does, well, to me it seems as though it would cause confusion to someone (no proof other then me) who gets frustrated trying to think of a situation where it could come in handy. I know how to work pointers (* &), I'm not completely ignorant when it comes to that, but like many things, I may not fully understand it's power, and maybe Davis is assuming I know what he is talking about. Anyways, I don't know, so, can someone give me a good situation where this could be extremely usefull, maybe a piece of code that might head me in the direction of Davis? Thank you, and I'll be back later on this morning to maybe clear up my question some more, since I am not exactly in the state of having foggless thoughts.
Old 04-12-2006, 06:40 AM   #2
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
I don't see any reason why anybody would favor example 1 over example 2. Using arrays is much clearer and less error-prone. It is true that a lot of C++ programmers despise arrays; however they don't replace them with pointers (which would be even worse), but with std::vector (or std::list).

In my opinion, if you need pointers, you will know it (because there are either no other possibilities or only inadequate ones).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Double pointers and two dimensional arrays in C kponenation Programming 23 03-16-2011 12:43 PM
C++ Return Char Arrays (not with pointers) sadarax Programming 4 02-03-2006 05:49 PM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 09:24 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 06:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 04:38 AM

All times are GMT -5. The time now is 04:48 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration