![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 31
| 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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| *(pntr+1) is equivalent to pntr[1], not *pntr[1]. |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jan 2009
Posts: 31
| ooooooh. So *pntr[1] is equivalent to *(*pntr + 1). |
| RaisinToe is offline | |
| | #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 | |
| | #5 |
| Registered User Join Date: Oct 2008
Posts: 452
| |
| EVOEx is offline | |
| | #6 |
| Registered User Join Date: Jan 2009
Posts: 31
| 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
![]() |
| Tags |
| pointers, question |
| Thread Tools | |
| Display Modes | |
|
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 |