LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-12-2006, 04:26 AM   #1
RHLinuxGUY
Member
 
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:

Code:
// 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;
}
Code:
// 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:
}
Code:
// 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:
Quote:
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, 05:40 AM   #2
addy86
Member
 
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).
 
  


Reply



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 11:43 AM
C++ Return Char Arrays (not with pointers) sadarax Programming 4 02-03-2006 04:49 PM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 05:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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