GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2009, 15:42
aligatorfurr aligatorfurr is offline
New Member
 
Join Date: Feb 2009
Posts: 1
aligatorfurr is on a distinguished road
Exclamation

Pointers... I need HELP!


I am a student in C++ and I am having a problem doing the following:
The array is named "values"
1) Use a "for" statement to print the elements of "array values" by subscripting the pointer to the array.

2)Refer to the fifth element of "values" using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation.

3)Assuming that "vPtr" points to "values[ 4 ]", what address is referenced by "vPtr -= 4"? What value is stored at that location?
  #2  
Old 09-Feb-2009, 16:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Pointers... I need HELP!


Here is some info.
  #3  
Old 09-Feb-2009, 20:26
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Pointers... I need HELP!


Something like:

CPP / C++ / C Code:
#include <iostream>

int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int* p = arr;

    // #1    
    for(size_t idx = 0; idx < sizeof(arr)/sizeof(int); idx++)
    {
	std::cout << "arr[" << idx << "] = " << p[idx] << std::endl;
    }

    // #2
    if(sizeof(arr)/sizeof(int) > 5)
    {
	// 2a
	std::cout << arr[5] << std::endl;
	// 2b
	std::cout << *(arr + 5) << std::endl;
	// 2c
	std::cout << p[5] << std::endl;
	// 2d
	std::cout << *(p + 5) << std::endl;
    }

    // #3
    if(sizeof(arr)/sizeof(int) > 4)
    {
	p = &arr[4];
	std::cout << *(p -= 4) << std::endl;
    }

    return 0;
}



Output:

Code:
$ ./intptr arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 arr[5] = 6 arr[6] = 7 arr[7] = 8 arr[8] = 9 arr[9] = 10 6 6 6 6 1


MxB
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 08:46
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.