Advertisement

pointers

Started by April 22, 2002 06:43 PM
1 comment, last by Timbermar 22 years, 6 months ago
alright, I've been beating my head against a wall for what seems like years. how do I use pointers/structures all that jazz. I'm really frustrated now, because last semester I took a class in begining c programming, I did good with that, and then over summer break, it all left me, and now I can't remember a lick of it. we just moved into structures, and I'm still struggling with pointers. I'm not worried about the class, i'll pass it, so time isn't an issue, but I just wish I could understand those things. please, email me or post here any good methods of learning or tuts. thanks, a ton. oh, yeah, my next big hurtle(well, after I learn c) is figuring out why when you put ++ after C, you have to relearn it all. "I cannot tell a lie... it was those guys over there." [edited by - timbermar on April 22, 2002 7:46:08 PM]
"I cannot tell a lie... it was those guys over there."
Is there something specific you do not understand or is it the whole concept of pointers that confuse you ?

A pointer is just a variable (like any other), the value of which is a memory address (say, 0x001A40B7C0). Which means that, through a pointer int* foo, you can treat the int data at that address (*foo) as if it was just another variable (*foo = 25).

However, you have to make sure that the address your pointer holds is a valid memory address. To do that, you can either assign it with the actual address of another variable ( the address of variable a is &a ), or ask the operating system to allocate some memory for you ( with new or malloc() ) in which case you have to remember to release it at some point (with delete or free() ).

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Ok, here''s a quick a dirty explanation of pointers.

int x; //allocates 4 bytes (an int) of memory in your executable.

int *x_ptr; //allocates 4 bytes of memory in your executable to store a memory address.

x_ptr = &x; //& = derefrence.
basically, set our pointer to the memory location that x is at.

*x_ptr = 10;
/*Sets the memory location pointed to by x_ptr to 10, since it points to the memory location of x, it actually modifies the value of x.
*/

you can have a pointer of any type...

char *ptr;

and it still takes 4 bytes because it holds a memory address (32-bit memory addresses).

Now, you can use this to your advantage when working with things like character arrays...

char *Test="Hello!"; //Creates a pointer that points to the word Hello! in our .exe file.
char *ptr = Test; //Sets our pointer to Test!

we can now printf() or cout << or ptr instead of the actual variable.. well, that''s pretty useless on it''s own, but...

what if we wanted to print out everything, but the first letter?

*ptr = (Test+1); Now our memory pointer points to the ello! in our .exe file.. so if we call something like printf, it starts at ello! and displays that.

Now, this may all seem pretty useless so far, but until you understand what they are/do they will be useless :D.

Lets say we want to find the length of a NULL terminated string...

first we''ll do it with our string array..

int SLen(char str[])
{
int pos=0;
while (str[pos]!=0) //Check if we still have something
++pos;//increment our position
return pos;
}

then using a pointer...

int SLen(char *str)
{
int len=0;
while (*str)
{
++len; //increment our counter
++str; //increment our pointer to the next char
}
return len;
}


Pointers are also necessary for dynamic memory.. say we don''t know how much memory we''ll need... you can''t create an array, as it has to be created at compile-time, and not run-time.

int *ptr, *sptr;
int amount, ctr;

printf("How many do you want?";
scanf("%d",amount);

ptr = (unsigned int*)malloc(amount*sizeof(int));
//Allocate our array of "amount" integers.

//We can do it this way, or this way:
for (ctr=0;ctr!=amount;++ctr)
ptr[ctr]=ctr;

//Or this way..
sptr = ptr;
for (ctr=0;ctr!=amount;++ctr)
{
*sptr=ctr; //set the memory address of the pointer to ctr!
++sptr; //increment our pointer by 4 bytes (size of an int).
}

free(ptr); //Free our memory

Billy - BillyB@mrsnj.com

This topic is closed to new replies.

Advertisement