Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers

Status
Not open for further replies.

curlyj

Programmer
Apr 8, 2001
10
GB
I am reading up on how to code with Visual C++ 6 and I have understood all the concepts I have read - so far - bar pointers.

I understand that you can set a pointer to a memory location thats holding data but the rest of pointers I just cant understand!

It's a bit cheeky, but could someone tell me about pointers, how and why use them?

I know this is a lot to ask, but any information would help me.

The book I am reading is "Beginning Visual C++ 6" by Ivor Horton.

Thanx!
Chris ;-) QBasic is fun! Go see what I've done!
 
All the program is loaded in memory while executing. Code(means commands) and Data(means variables) use the same memory. Data and code are separated between them in memory, but in one single memory, not in different parts of computer. Also, in memory are loaded system data/code.
Pointers are usualy used to access direct the memory.
For example, if you have an array of some type of data(for example integers), it has a fixed size on the memory. When you try to change it, you may broke the execution of program. You should find a place in the memory not used by code or other data, and access it direct with pouinters. This process is named allocation. Every C/C++ programs know what memory is used, what not, what is used by operating system. The finding of free memory, is difficult, but you can use functions malloc, calloc, realloc. C++ has an operator 'new' what is close to calloc. Also, instead of statically declaring of variables, you can just create them dinamically while executing. Also do not forget about memory leaks. John Fill
1c.bmp


ivfmd@mail.md
 
I'll take a stab at it as I use them constantly but never reflex on what they really are.

A pointer like you probably have read is a variable that holds a memory address of the same type. The examples you have probably seen where they create a pointer to an integer and then change the value by assigning the address of another integer probably seems to have little practical use.

//this is not a good demonstration of the use of pointers
int *i, p;
p = 3;
i = &p;
fprintf("%d\n", *i);

That is because pointers are mainly used to pass around objects. When you create an object of some user defined type its easy to pass a pointer to it. Pointers are also mainly used to dynamically create objects at runtime. Hmm, pointers are hard to explain b/c they're just a variable that holds a memory address plain and simple. The uses of pointers is something you pick up as you program more. Just remember when you see functions that takes a pointer to a object then you have to pass the address of the object (&obj) or a pointer because a pointer is an address. Passing a pointer to an object (its address) usually means that the variable is going to change in the function. It's C++ reference parameter. In the days of C (which are still today mind you) you pass a pointer to change the value of the variable in the calling program. Well, in C++ you pass a reference parameter, which under the hood is just a pointer to the variable. A matter of fact the compiler replaces all the & signs in the function with * and dereferences all uses of the variable in the function - the programmer just never sees it. To pass around pointers to types you:

MYTYPE p1;
MYTYPE *p2;

static void set_p(MYTYPE* val)
{
val->data1 = bla
val->data2 = bla
val->data3 = bla

//alternatively you can dereference and use the dot operator
(*val).data4 = bla;
}

called like this:
set_p(&p1);
or
set_p(p2);

Your knowledge about pointers will increase has you start coding more. Just keep on coding and deal with them as you see them and evidentally you'll get used to them. Once you really get into pointers you can start making function pointers and callback functions - cool stuff.

Bitwise
 
One good use of pointers is to exceed the limitations of functions called within main. As you know, functions can only return one value.
Example of a function definition:

int example (int x)
{
x += 4;
return x;
}

As you can see, you can only return one value. But what if you want to "return" more values? Well you can do this with pointers. Example:

void pointerExample (int *x, int *y, int *z)
{
*x = 3;
*y = 4;
*z = 5;
}

Although your not really returning a value, you can use the pointers to manipulate the data to which it points to. Thus in my example, you could say the function "returns" 3 values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top