Pointers

S

Sidhu

Hai,

I like 2 know more abot pointers. Can you please tell me the fields
in which pointers are widely used.

Yours
Sidhu
 
R

Richard Heathfield

Sidhu said:
Hai,

I like 2 know more abot pointers. Can you please tell me the fields
in which pointers are widely used.

When you need to store an address, a pointer is a great place to do it.
 
M

Malcolm

Sidhu said:
I like 2 know more abot pointers. Can you please tell me the fields
in which pointers are widely used.
If I had to describe C in one sentence I would say

"A computer language in which pointers allow direct access to memory".

Pointers are the heart and soul of C. Only contrived programs manage without
them.

OK.

int a;
int *ptr = 7a;

ptr now points to a. By saying *ptr = 3 we can set a to 3.

What is the point of that?

The point is that we can pass pointers to other functions.

C functions cannot return two values, but a cursor has both an x and a y
coordinate
asscoiated with it.

void getcursorxy(int *x, int *y)
{
*x = cursor_xpositon; /* the positon is kept in some variable somewhere by
the OS */
*y = cursor_yposition;
}

will allow us to extract both values with a single call.

Pointers really come into their own when they point to arrays for numbers.
 
P

pete

Sidhu said:
Hai,

I like 2 know more abot pointers. Can you please tell me the fields
in which pointers are widely used.

Most of the standard library functions
have at least one pointer type parameter.
 
L

lovecreatesbeauty

Malcolm said:
C functions cannot return two values, but a cursor has both an x and a y
coordinate
asscoiated with it.
void getcursorxy(int *x, int *y)

I ever was taught that pointer parameters are used to pass large
arguments to the functions, e.g. struct ..., efficiently and return
value is used to pass a value to callers. Now the return value seems no
use in the example code above.

lovecreatesbeauty
 
R

Richard Heathfield

lovecreatesbeauty said:
I ever was taught that pointer parameters are used to pass large
arguments to the functions,

Parameters are *not* used to pass arguments.

Arguments are what you provide to the function call mechanism, which
evaluates them and passes them to the function, which receives them as
parameters.
 
L

lovecreatesbeauty

Richard said:
lovecreatesbeauty said:

Parameters are *not* used to pass arguments.

Arguments are what you provide to the function call mechanism, which
evaluates them and passes them to the function, which receives them as
parameters.

Exactly! Thank you.

It's better for me to avoid using "argument" in my last post, I think.
Just use "value" again instead.

lovecreatesbeauty
 
K

Keith Thompson

Richard Heathfield said:
lovecreatesbeauty said:


Parameters are *not* used to pass arguments.

Arguments are what you provide to the function call mechanism, which
evaluates them and passes them to the function, which receives them as
parameters.

Pointers are used in C to emulate the pass-by-reference facility that
some other languages provide.

C doesn't really have pass-by-reference, but you can emulate it using
pointers (which are, of course, passed by value). The question of
whether this is really pass-by-reference is more about the meaning of
the word "really" than about the C language.

C doesn't really have linked lists, but you can build linked lists
using structures and pointers. If I write code in C that implements a
linked list, is it not really a linked list because the language
doesn't support it directly?

Everything that isn't hardware (and a great deal that is) is
emulation.

It's vital to understand that C function arguments are always passed
by value, and that pointers are the way to emulate pass-by-reference.
 
B

Barry Schwarz

I ever was taught that pointer parameters are used to pass large

Pointers are used wherever the designer of the software decides to use
them.
arguments to the functions, e.g. struct ..., efficiently and return

Efficiency is just one of many design objectives. And not necessarily
the deciding one for every decision.
value is used to pass a value to callers. Now the return value seems no
use in the example code above.

There is no return value in the above code.

There are functions that need to provide more than one piece of
information to the caller. There are several methods used to
accomplish this:

Some functions (e.g., ftell) return an error value and set
errno.

Some functions (e.g. strtol), in addition to returning a
value, dereference a pointer to provide additional information to the
caller.

Some functions (as in the above example), don't return
anything but do dereference pointers to provide data to the caller.

Some functions (e.g., fopen) return an aggregate or a pointer
to an aggregate and let the aggregate contain the multiple data the
caller needs.

Some functions use global variables.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,148
Latest member
ElizbethDa
Top