home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2182
Discussions: 188,392, Posts: 2,243,567, Members: 232,621
Old February 14th, 2004, 08:32 PM   Digg it!   #1 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,163
Send a message via ICQ to Creatures
C++ *pointer...

after a long time i started to learn this language again, but i stuck at the same part as last time

so maybe this time someone can explain me these pointers, how they work and how i can use them

and maybe explain me this little program

Code:
// more pointers
#include <iostream.h>

int main ()
{
  int numbers[5];
  int * p;
  p = numbers;  *p = 10;
  p++;  *p = 20;
  p = &numbers[2];  *p = 30;
  p = numbers + 3;  *p = 40;
  p = numbers;  *(p+4) = 50;
  for (int n=0; n<5; n++)
    cout << numbers[n] << ", ";
  return 0;
}
thx
Creatures
__________________
Canon EOS 450D | Canon EF-S 18-55mm 1:3.5-5.6 IS | Canon EF-S 55-250mm 1:4-5.6 IS | Canon Speedlite 430EX II

Creatures is offline   Reply With Quote
Old February 14th, 2004, 10:59 PM     #2 (permalink)
Ultimate Member
 
elmers's Avatar
 
Join Date: Sep 2003
Location: Philadelphia
Posts: 1,462
// more pointers
#include <iostream.h>
int main ()
1 {
2 int numbers[5];
3 int * p;
4 p = numbers; *p = 10;
5 p++; *p = 20;
6 p = &numbers[2]; *p = 30;
7 p = numbers + 3; *p = 40;
8 p = numbers; *(p+4) = 50;
9 for (int n=0; n<5; n++)
10 cout << numbers[n] << ", ";
11 return 0;

Ok first off some general things.
In C the int consists of 16 bits or 2 bytes.
A pointer declared in line 3 is 32 bits or 4 bytes.
A variable declared an array (like in line 2) is actually a pointer to the first element of that array. Because of this it behaves like a pointer. In other words numbers is actually a pointer to the start of a 10 consecutive bytes in memory.

All this means that line 4 actually means that p is now pointing at the location numbers is pointing to. The star in line 4 (*p = 10) means you want to access the location the pointer p is pointing to not the actual pointer. Thus p being numbers and *p being 10 means that numbers[0] is 10.

numbers is (10, nd , nd , nd , nd) where nd is not defined.

Since pointers are basicly unsigned double ints you can increment. Since the p points to the first element of numbers by incrementing it in line 5 p points to the second element of numbers or numbers[1] (remember that arrays always start with 0 in c). Then *p = 20 means that the second element of numbers is 20.

numbers is (10, 20 , nd , nd , nd)

Line 6 is a bit tricky. The & means you want the location of a variable. And numbers[2] is a variable. The tricky part is that numbers is a pointer while numbers[2] is a int. So

&numbers[2]

is actually a pointer to the third element of numbers. This is then assigned 30 (*p=30)

Now numbers is (10, 20, 30, nd, nd)

Since pointers are numbers you can add 3 to the location of the first element of numbers in line 7 to get p to point to the 4th element of numbers. After line 7 numbers is

(10, 20, 30, 40, nd)

Line 8 is pretty much the same reasoning as in line 7 except you add 4 to p which was pointing to numbers[0] and place 50 in that location giving you

(10, 20, 30, 40, 50)

Lines 9, 10 print out the numbers array. No {} are needed for oneliners in c after a for.

Line 11 returns 0 the default for no errors in c.

To sum it up:

Array variables like "numbers" are pointers to the first element of an array.
numbers is a pointer variable while numbers[n] is an int variable
*p denotes the actual data at location p.
&numbers[n] is the address to the n element in an array. &n would give you the address at wich the variable n was stored.
__________________
Buy the ticket, take the ride.
- Hunter S. Thompson


Last edited by elmers : February 18th, 2004 at 12:23 PM.
elmers is offline   Reply With Quote
Old February 15th, 2004, 07:24 AM     #3 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,163
Send a message via ICQ to Creatures
thank you very much elmers!

i think i got it now!

Thx
Creatures

Creatures is offline   Reply With Quote
Old February 18th, 2004, 12:24 PM     #4 (permalink)
Ultimate Member
 
elmers's Avatar
 
Join Date: Sep 2003
Location: Philadelphia
Posts: 1,462
Weak typing makes C hard to understand sometimes. A nice book is "The C Programming Language" by Kerninghan and Ritchie, a true classic on programming.
elmers is offline   Reply With Quote
Old February 18th, 2004, 06:38 PM     #5 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,163
Send a message via ICQ to Creatures
thx! will have a look in our local book store!

Creatures
Creatures is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may 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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2904)
Building a gaming computer advice (5)
New Build ( Finally ) (6)
CPU wont boot (7)
Looks like Burris will get his Sena.. (12)
3-days in and no threads about Gaza (160)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (444)
Upgrading RAM (6)
Recent Discussions
nVidia GTX 295 now available (1)
Error ~ BAD BLOCK (19)
I think my PSU is dieing (0)
no power ! (8)
Need help removing my front usb.. (3)
Help Plz!!! (0)
CS/COND ZERO AGAIN (3)
Please help! multiple problems! (2)
Folderchat Weekday thread (444)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 01:24 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28