Thread: pointers with arrays, (*(pntr + 1) vs. *pntr[1])

  1. #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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    *(pntr+1) is equivalent to pntr[1], not *pntr[1].

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    ooooooh. So *pntr[1] is equivalent to *(*pntr + 1).

  4. #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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by RaisinToe View Post
    ooooooh. So *pntr[1] is equivalent to *(*pntr + 1).
    No. *pntr[1] is equivalent to **(pntr + 1)

  6. #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.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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;
    }

  8. #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;
    }

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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

  10. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread