C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-04-2009, 03:53 PM   #1
Registered User
 
Join Date: Jan 2009
Posts: 31
Question pointers with arrays, (*(pntr + 1) vs. *pntr[1])

I am just wondering what the difference is between using '*(pntr + 1)' or '*pntr[1]'. When using '*pntr[1]', to point to an array of my variable 'char loc', I get just the character that I pointed to. But when using '*(pntr + 1)', I get the character that I pointed to, and any other characters that followed after it in that string.

In my code, I placed comments at the bottom that explains my guess of what is going on.


Code:
// Having fun with pointers.

#include <iostream>
using namespace std;


int main()
{
	char loc[80];	// Define variable array.
	char *pntr[4] = 
               {&loc[4], 
                 &loc[9], 
                 &loc[14], 
                 &loc[19]};	// Define pointer array to index locations 4, 9, 14, and 19 of variable loc.

	cin >> loc;               // Input string to loc
	cout << *pntr[1] << "\n";	// output one of the characters of loc that were assigned to the pointer (*pntr). (the character in loc[9] is displayed.)
	system( "PAUSE" );

return 0;
}

/************************************************************************************************************************
In the case 

	cin >> loc;
	cout << *pntr[1];

only the specified character is displayed.
But if

	cin >> loc;
	cout << *(pntr + 1);

Or

	cin >> loc;
	cout << *pntr;		(is the same as 'cout << *(pntr + 0)')

the specified character is displayed, but every character after it is also displayed.
The reason for this behavior I will explain.

Remember that the pointer is indexing within its own array first, instead of pointing directly to a 'loc' array location.
So when outputing '*pntr;' or '*(pntr + 0)', I think that the problem is caused from double indirect addressing.
'*(pntr + 1);' differs from '*pntr[1]' as '*(pntr + 1) is an indirect address to *pntr[1], (I think).
*************************************************************************************************************************/

I hope I made sense.

Last edited by RaisinToe; 03-04-2009 at 03:59 PM.
RaisinToe is offline   Reply With Quote
Old 03-04-2009, 04:04 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
*(pntr+1) is equivalent to pntr[1], not *pntr[1].
tabstop is offline   Reply With Quote
Old 03-04-2009, 04:10 PM   #3
Registered User
 
Join Date: Jan 2009
Posts: 31
ooooooh. So *pntr[1] is equivalent to *(*pntr + 1).
RaisinToe is offline   Reply With Quote
Old 03-04-2009, 04:14 PM   #4
Registered User
 
Join Date: Jan 2009
Posts: 31
So how is it that the character form of the specified pntr array is not displayed on the screen instead of displaying the string of loc? How exactly do pointers work when using *(pntr + 1).

Last edited by RaisinToe; 03-04-2009 at 04:16 PM.
RaisinToe is offline   Reply With Quote
Old 03-04-2009, 04:23 PM   #5
Registered User
 
Join Date: Oct 2008
Posts: 452
Quote:
Originally Posted by RaisinToe View Post
ooooooh. So *pntr[1] is equivalent to *(*pntr + 1).
No. *pntr[1] is equivalent to **(pntr + 1)
EVOEx is offline   Reply With Quote
Old 03-04-2009, 04:27 PM   #6
Registered User
 
Join Date: Jan 2009
Posts: 31
Quote:
Originally Posted by EVOEx View Post
No. *pntr[1] is equivalent to **(pntr + 1)
Thank you for pointing that out.
But I also just tested out '*(*pntr + 1)', and it seemed to work just fine.

* has higher precedence than + or -, so I think I see why It should always be done the way you said.

Last edited by RaisinToe; 03-04-2009 at 04:31 PM.
RaisinToe is offline   Reply With Quote
Old 03-04-2009, 04:50 PM   #7
Registered User
 
Join Date: Oct 2008
Posts: 452
Try this:

Code:
#include <iostream>
using namespace std;

int main()
{
        int c = 3;
        int a = 1, b = 2;
        int d = 4;
        int *p[] = { &a, &b };
        cout << *(*p + 1) << endl;
        cout << **(p + 1) << endl;
}
EVOEx is offline   Reply With Quote
Old 03-05-2009, 05:32 PM   #8
Registered User
 
Join Date: Jan 2009
Posts: 31
Thank you,
I am still getting used to this array buisiness.
I just came accross another problem.

Is there a way that I can compair a 1-dementional array, with a 3-dementional array?

here is my problem. I don't know why the two strings (ans[80] and Answer[5][6][80]) will not compair properly. (Look at line 28).
Code:
#include <iostream>
using namespace std;

void Com_ans();
                char ans[80];
				char cat = 0;	// choose from catagory 0.
				char qst = 0;	// choose question 0 within catagory 0.
int main()
{
                cout << "What is the answer in colomb 0 question 0? ( type  \" answer1\")";
                cin >> ans;
                Com_ans();		// call the Com_ans function to compair answers.
	system("PAUSE");

	return 0;
}

 void Com_ans()
{
	char Answers[5][6][80]=
	{	"answer1", "a2", "a3", "a4", "a5", "a6",	//Table of answers.
		"1", "2", "3", "4", "5", "6",
		"1", "2", "3", "4", "5", "6",
		"1", "2", "3", "4", "5", "6",
		"1", "2", "3", "4", "5", "6"
	};

	if(ans == Answers[cat][qst])			// Here is my problem, how can I compair the to strings?
		cout << "!!Correct!!" << Answers[cat][qst];
	else cout << "Rong Answer, Try again";

	return;
}
RaisinToe is offline   Reply With Quote
Old 03-05-2009, 07:38 PM   #9
Registered User
 
Join Date: Nov 2007
Posts: 171
ans is a pointer to the first element in ans[].
ans == Answers[cat][qst] is a comparison of 2 pointers.
To compare C-strings, you need strcmp() which is found in <cstring>
__________________
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
NeonBlack is offline   Reply With Quote
Old 03-06-2009, 12:40 PM   #10
Registered User
 
Join Date: Jan 2009
Posts: 31
Awesome, thanks
So if I try to do this same thing without using the 'strcmp()' function,
I guess I would have to get the values given by my pointers.
Put the values into variables, and then compair the variables.
RaisinToe is offline   Reply With Quote
Reply

Tags
pointers, question

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with returning arrays using pointers cuba06 C Programming 9 11-23-2007 10:40 AM
Array of Pointers to Arrays Biozero C Programming 2 04-19-2007 02:31 PM
Pointers and multi dimensional arrays andrea72 C++ Programming 5 01-23-2007 04:49 PM
pointers and arrays.. ahming C Programming 1 04-24-2004 03:12 AM
Stack functions as arrays instead of node pointers sballew C Programming 8 12-04-2001 11:13 AM


All times are GMT -6. The time now is 01:56 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22