Reg : Pointers

S

Satish Kumar

Can any one give me indetail about,

what are pointers?
How are pointers useful in C prgramming?
How do they work?

i had gone through one book, i am a bit confused.

Regards,
Satish...
 
A

Anthony Borla

Satish Kumar said:
Can any one give me indetail about,

what are pointers?
How are pointers useful in C prgramming?
How do they work?

I had gone through one book, i am a bit confused.

I *do* have a pointer for you:

Google

using the phrase:

Pointers C Tutorial

Cheers,

Anthony Borla
 
J

Jens.Toerring

Satish Kumar said:
Can any one give me indetail about,
what are pointers?

Pointers are variables that can hold addresses. Normally, they get
assigned a value that is the address of another object (a normal
variable, a function or another pointer). Once that has happened
you can access what the pointer points to by dereferencing it,
which is done by putting the (unary) '*' operator in front of
the pointer variables name.

So when you have e.g.

int i = 5;
int *ip = &i; /* pointer, pointing to i */

then you can e.g. do

printf( "i is %d\n", *ip );

or

*ip = 7; /* change i indirectly via the pointer */

printf( "i is now %d\n", i )

and this will print out

i is now 7
How are pointers useful in C prgramming?

First of all, they allow you to pass addresses of variables to
functions, and by doing so it is possible to change the content
of the variable pointed to from within the function (remember:
in C variables are passed by value, so a function only receives
the value of an argument variable and can't change the variable
itself).

Second, pointers are indispensable when you need amounts of
memory you can only determine while the program is already
running. Functions like malloc() return you a pointer to a
memory buffer with a size you can tell the function. If the
function succeeds you can use the returned value to access
that memory, e.g.

int *a = malloc( 100 * sizeof *int );
if ( a != NULL ) {
a[ 17 ] = 42;
free( a );
}

But there are hundreds of other uses of pointers and listing
only the small subset which immediately comes to mind would
make this posting much too long. C without pointers would be
a rather useless language.
How do they work?

What do you mean by "work"?
i had gone through one book, i am a bit confused.

We all started that way (more or less). Keep reading and experi-
menting and you will understand soon enough;-) Trying to draw
some boxes representing pointers and variables and arrows de-
picting the relationship between them sometimes helps (especially
if you start playing around with pointers to pointers).

Regards, Jens
 
R

Ramasubramanian XR (AS/EAB)

Can any one give me indetail about,

what are pointers?
How are pointers useful in C prgramming?
How do they work?

i had gone through one book, i am a bit confused.

Regards,
Satish...
you can get good material from net
search google with keyword as ' pointers in c '
you can get so many pdf
 
J

Jonathan Bartlett

Satish said:
Can any one give me indetail about,

what are pointers?
How are pointers useful in C prgramming?
How do they work?

I have found that for people to really understand pointers, they need to
understand assembly language and how it works. I wrote a book to teach
assembly language programming, not to make people expert assembly
language programmers, but to instead give them a better idea of how
their computer works and how the languages they use work under the hood.
You can find the book at:

http://www.cafeshops.com/bartlettpublish.8640017

Jon
 
J

Joona I Palaste

I have found that for people to really understand pointers, they need to
understand assembly language and how it works. I wrote a book to teach
assembly language programming, not to make people expert assembly
language programmers, but to instead give them a better idea of how
their computer works and how the languages they use work under the hood.
You can find the book at:

I strongly disagree. I have only very limited experience about assembly
language, but I understand the basics about C pointers. In my opinion,
it's enough to know that variables are stored in memory locations, and
pointers store the addresses of those locations.
 
M

Mark L Pappin

I strongly disagree. I have only very limited experience about
assembly language, but I understand the basics about C pointers. In
my opinion, it's enough to know that variables are stored in memory
locations, and pointers store the addresses of those locations.

I'll have to disagree with both of you here. C pointers _may_ (and in
many common implementations do) store just addresses, but at an
abstract level (where one should be thinking, to avoid UB) each
pointer has both value (which may be invalid in certain circumstances)
and type. Thinking of pointers as addresses leads to such brokenness
as expecting

#include <assert.h>
int main(void) {
{
unsigned i = 0;
void* p = i;
assert(p == (void*)0);
}
{
char a[27];
assert((&a)+1 == (&a[0])+1);
}
return 0;
}

to not abort.

mlp
 
K

Keith Thompson

Mark L Pappin said:
I'll have to disagree with both of you here. C pointers _may_ (and in
many common implementations do) store just addresses, but at an
abstract level (where one should be thinking, to avoid UB) each
pointer has both value (which may be invalid in certain circumstances)
and type. Thinking of pointers as addresses leads to such brokenness
as expecting
[snip]

A good point, but I'm going to quibble anyway.

The standard uses the term "address" to mean a pointer value. So if
you follow the terminology of the standard, it's trivially true that
pointers store just addresses. You just need to be aware that they
aren't necessarily "addresses" in the low-level sense used by most
people -- just as C "bytes" aren't necessarily 8 bits.
 
B

BGreene

The standard uses the term "address" to mean a pointer value. So if
you follow the terminology of the standard, it's trivially true that
pointers store just addresses. You just need to be aware that they
aren't necessarily "addresses" in the low-level sense used by most
people.
San Diego Supercomputer Center <*>
We must do something. This is something. Therefore, we must do this.

Could you please clarify this?

Barry
(e-mail address removed)
 
K

Keith Thompson

BGreene said:
Could you please clarify this?

The usual (non-C) sense of the term "address" is a virtual or physical
address recognized by the CPU and/or memory management system.

The C standard uses the term "address" to mean a C pointer value. For
example, the unary "&" (address-of) operator yields an address value.
The C standard defines the semantics, but doesn't say much about the
representation. An "address" in the C sense could be some
higher-level construct than a virtual or physical machine address.
It's an address in the C abstract machine, not necessarily in the
physical machine.

In most implementations, C addresses (pointer values) are machine
addresses, but the C standard is designed to allow a variety of
implementation strategies.

One concrete example is the C implementation on Cray vector machines.
A machine-level address is a 64-bit quantity that points to a 64-bit
machine word, but the C compiler has CHAR_BIT==8, so it needs a
mechanism to point to 8-bit bytes within words. An int* pointer is a
machine address, but a char* or void* pointer has a 3-bit offset
stored in the otherwise unused high-order bits of a word pointer.
This is implemented entirely in code generated by the compiler, not in
hardware. A char* pointer value with a non-zero offset field is a C
address, but it's not a machine address.
 
B

BGreene

Keith Thompson said:
The usual (non-C) sense of the term "address" is a virtual or physical
address recognized by the CPU and/or memory management system.

The C standard uses the term "address" to mean a C pointer value. For
example, the unary "&" (address-of) operator yields an address value.
The C standard defines the semantics, but doesn't say much about the
representation. An "address" in the C sense could be some
higher-level construct than a virtual or physical machine address.
It's an address in the C abstract machine, not necessarily in the
physical machine.

In most implementations, C addresses (pointer values) are machine
addresses, but the C standard is designed to allow a variety of
implementation strategies.

One concrete example is the C implementation on Cray vector machines.
A machine-level address is a 64-bit quantity that points to a 64-bit
machine word, but the C compiler has CHAR_BIT==8, so it needs a
mechanism to point to 8-bit bytes within words. An int* pointer is a
machine address, but a char* or void* pointer has a 3-bit offset
stored in the otherwise unused high-order bits of a word pointer.
This is implemented entirely in code generated by the compiler, not in
hardware. A char* pointer value with a non-zero offset field is a C
address, but it's not a machine address.
San Diego Supercomputer Center <*>
We must do something. This is something. Therefore, we must do this.


Thanks for making this clear. The example you gave was well described. It
is similar to the implemetation of all the compilers that ran on HP-PA,
with the exception HP-PA was originally 32 bit, and used the 2 bit (low
order bits) as a "space register." to allow it to address 4G. I don't know
where I read this but I am pretty sure it is true.
 

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
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top