What is the type of a pointer? 
Author Message
 What is the type of a pointer?

I know how to use pointers  very well I've been writng in C for three  
years.

My question is what is the type of a C pointer??

I know it returns the memory location or lvalue when derefernced.

Would it be somthing like this in pesudoC

typedef union {

all c types;
size of all  c types; /*how are structures handeld ??*/

Quote:
}*p

It seams to me a pointer must be able to store bothe the memory adress and  
be big enough to hold both the adress and the type ??

It look to me like its a union. I looked through the Source for both Small  
C and gcc and could gean enough info to determin exactly how a pointer is  
defined.

Also how does the & and * operators work this of couse is dependent on how  
a pointer is defined. And on this note smething like p[1] or p++. How do  
they get incremented??

IT's hard to understand from looking at a compiler.

In short if I was going to write a function that behaved like a pointer  
w/o using pointers how would one do it.

Thanks Mike



Wed, 15 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:

>My question is what is the type of a C pointer??

There are an infinite number of pointer types, because there are an
infinite number of types. For instance, here are three different
types:

struct T1 { int i; };
struct T2 { int i; };
struct T3 { int i; };

With a bit of imagination, you can come up with some more distinct
structure types. Now, since a pointer to a `struct T' has type
`pointer to struct T', we have just created the opporunity to make
three obviously-different pointer types, i.e., `struct T1 *',
`struct T2 *', and `struct T3 *'.

Of course, we also have `struct T1 **', which is different from
`struct T1 *', and `struct T2 **', which is different from all the
others, and so on.

Quote:
>It seams to me a pointer must be able to store bothe the memory adress and
>be big enough to hold both the adress and the type 10 11 12 13 14 15

An object pointer must be able to store the address of an object
of the type to which the pointer can point. That is about all you
can say for certain, except that `char *' and `void *' can hold
any object pointer and that the result of converting any pointer
value `p' (of any pointer-to-object type `T') to `void *', then
converting it back to type `T', must compare equal to the original
value `p'.

A pointer *might* also hold a type code, but there is no guarantee,
and no requirement; the C language forces the programmer to supply
the correct type. If the programmer fails to supply the correct
type, all bets are off. Some systems carry around an internal
notion of the type, working hard to be helpful, and produce a
compile- or run-time error; others may abort at runtime, misbehave
unpredictably, or hang entirely.

Quote:
>Also how does the & and 1 10 11 12 13 14 15 2 3 4 5 6 7 8 9 operators work this of couse is dependent on how
>a pointer is defined. And on this note smething like p[1] or p++. How do
>they get incremented??

Again, all you can say in C is that `&lvalue' produces the address
of the given lvalue (object), and `*pointer' produces the object
to which the pointer points. The expression p[1] is defined as
being equivalent to *((p) + (1)), and pointer addition is defined
in terms of moving forward or backward by some integral number of
objects in an array of objects into which the pointer must point.

Quote:
>IT's hard to understand from looking at a compiler.

More precisely, it is *impossible* to understand from looking at a
compiler---all you will learn this way is one (or several) specific
*implementations*. (You may be able to generalize from this, but
you are probably better off consulting the C Standard or a good book.)

Quote:
>In short if I was going to write a function that behaved like a pointer
>w/o using pointers how would one do it.

This question makes no sense, unless you are referring to a specific
implementation or implementations (in which case you are probably
best off finding assembly-language information on the machine(s)
in question).
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc



Thu, 16 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:

>I know how to use pointers very well I've been writng in C for three
>years.

>My question is what is the type of a C pointer??

>I know it returns the memory location or lvalue when derefernced.

>Would it be somthing like this in pesudoC

>typedef union {

>all c types;
>size of all c types; /*how are structures handeld ??*/
>}*p

>It seams to me a pointer must be able to store bothe the memory adress and
>be big enough to hold both the adress and the type 10 11 12 13 14 15 16

>It look to me like its a union. I looked through the Source for both Small
>C and gcc and could gean enough info to determin exactly how a pointer is
>defined.

>Also how does the & and 1 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 operators work this of couse is dependent on how
>a pointer is defined. And on this note smething like p[1] or p++. How do
>they get incremented??

The type of the pointer is "pointer-to-x" where the
"x" is the declared type to which the pointer points,
but your question seems to be: how does one handle pointers
generically?

In the old country, a pointer could be reasonably assumed
to fit into an `unsigned long' object.

These days, what with segmentation and virtual memory and
thirty-one flavors equalling ten flavors plus fro-yo and
"sorbet," such an expectation would be rash.

Now, according to the standard, any pointer may be safely
stored in a special object designated to have the type
"pointer-to-void," or `void *'.

You can create arrays of these `void *' objects, to hold a
skein of pointers, or place them in structures as members,
or pass them to functions. The one thing you can't do is
dereference them without casting them to a type other than
`void *'. They have to be a "pointer-to-something."

Which brings us to the crux of your question: How does
one figure out what a `void *' points to?

Unfortunately, one has to build it in. Type information
does not appear at runtime, unless you create the
association yourself in your object definition (as you have
done rather clearly in your structure, above).

This is a common practice. In the X window system, for
instance, almost all of the standard structures begin with
a "type" member. This tells the recipient of the data much
more than what a single pointer means, but the principle is
the same. There's even a common name for your structure;
it's called a "bag." It's a generic structure that allows
one to handle objects of arbitrary structure without having
to build the type information into that structure. This
isn't what you'd do if you were defining the structure
yourself, of course, you'd just put the type information
into every structure; but, it's what you need to do if
you're using structures borrowed from existing systems or
standards.

Quote:
>IT's hard to understand from looking at a compiler.

It's just as easy as understanding the thermodynamics of
the internal combustion engine by looking at an automobile
assembly line. Which is to say, not.

Quote:
>In short if I was going to write a function that behaved like a pointer
>w/o using pointers how would one do it.

What?

--Blair
"C isn't C++. It isn't even (C++)--."



Thu, 16 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:

>My question is what is the type of a C pointer??

The type is "pointer to ...".

Quote:
>I know it returns the memory location or lvalue when derefernced.

Nope. It returns the value stored in the memory location when
dereferenced.

Quote:
>It seams to me a pointer must be able to store bothe the memory adress and
>be big enough to hold both the adress and the type 10 11 12 13 14 15 16 17

Not necessarily. The type is known by the compiler, it doesn't have to be
stored in the pointer (and most implementations store only memory addresses
in pointers). However, an implementation is free to store as much
information as it likes in a pointer. For example, a bound-checking
compiler will have to store three memory addresses (or two addresses and a
byte count).

Quote:
>Also how does the & and 1 10 11 12 13 14 15 16 17 2 3 4 5 6 7 8 9 operators work this of couse is dependent on how
>a pointer is defined. And on this note smething like p[1] or p++. How do
>they get incremented??

The compiler knows the size of the object pointed to by 'p', so it is able
to increment it properly. Note that void pointers (which don't point to
any object, but can be converted to pointers to objects) cannot be
dereferenced or incremented (pointer arithmetic is not defined for void
pointers).

Quote:
>IT's hard to understand from looking at a compiler.

This is precisely why the recommended method for learning C is to read a
good book, not to look at a compiler.

Quote:
>In short if I was going to write a function that behaved like a pointer
>w/o using pointers how would one do it.

One can't do that in C. There is no way to substitute pointers by anything
else in C.

Dan
--
Dan Pop
CERN, CN Division

Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Thu, 16 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?


Quote:
> My question is what is the type of a C pointer??

-<snip>-

Quote:
> It seams to me a pointer must be able to store bothe the memory adress and
> be big enough to hold both the adress and the type ??

It is a place in memory that stores a memory adress (the place it is
pointing to) but it is the compiler that "knows" about it's type and
generates code (or warnings) according to this information.

Quote:
> It look to me like its a union. I looked through the Source for both Small
> C and gcc and could gean enough info to determin exactly how a pointer is
> defined.

The definition depends on your a system. If it was based on a 68k (like
mine) a pointer would look very much like an unsigned long integer and be
32 bits wide. It's not a union though!

Quote:
> Also how does the & and 1 2 3 4 operators work this of couse is dependent on how
> a pointer is defined. And on this note smething like p[1] or p++. How do
> they get incremented??

This depends on how *p is declared. If p is a pointer to char, then p++
would add '1' to p (and p would point to the neighbouring memorycell.)
The case p[1] would also add '1' to p, but only temporarely and without
destroying the original value of p

If p had been a pointer to a 32 bit integer, then p++ would have added
'4' to the value of p (p is then pointing to the next memoryarea big
enough to hold an integer)

Quote:
> IT's hard to understand from looking at a compiler.

Hope this helps // Jens M Andreasen


Thu, 16 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:

>It seams to me a pointer must be able to store bothe the memory adress and  
>be big enough to hold both the adress and the type ??

IMHO I think you're wrong.  A pointer is an address to a memory location.
By typecasting it, the compiler will theat it differently.
        struct a {
                int a;
        } alpha;
        struct b {
                int a, b;
        } bravo;
        struct a *ptr  = &alpha;
        printf( "%d\n", ( ( struct b * )ptr )->b );

will print garbage.

The compiler does not store any info regarding the size of what the
pointer is looking at.

Quote:
>Also how does the & and * operators work this of couse is dependent on how  
>a pointer is defined. And on this note smething like p[1] or p++. How do  
>they get incremented??

If you mean X is a pointer to a struct, and you insert X++, well the compiler
will add the size of the struct to X (or if X points to a thingy, the size
of thingy).


Sat, 18 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:

> I know how to use pointers  very well I've been writng in C for three
> years.

> My question is what is the type of a C pointer??

> I know it returns the memory location or lvalue when derefernced.

> Would it be somthing like this in pesudoC

> typedef union {

> all c types;
> size of all  c types; /*how are structures handeld ??*/
> }*p

> It seams to me a pointer must be able to store bothe the memory adress and
> be big enough to hold both the adress and the type ??
> [snip]

A pointer is the address of something. What the pointer is pointing to is not
recorded anywhere, except at compilation time The compiler keeps an internal
record of the pointers and the types of the values they point to. This way,
during compilation, the compiler can increment the pointers by the size of the
type of value they're pointing to. Consider the following code:

        void    func(void *ptr)
        {
                char    c;

                ptr++;
                c = *ptr;
        }

This will lead to a compilation error, since a generic pointer (a pointer to
anything) is used and the compiler thus cannot know how much the pointer should
be incremented in order to get to the next element of the type the pointer is
pointing to, and it also cannot make the assignment for the same reason
(undefined pointee type). In such constructs, you would have to cast the pointer
to some type first, for example:

        void    func(void *ptr)
        {
                char    c;

                ((char *)ptr)++;
                c = *((char *)ptr);
        }

Also, if you'd have the pointee type stored somewhere, you could do something
like this:

        typedef struct {
                char    name[16];
                int     num;
        } TYPE_S;

        int     func(TYPE_S *ptr)
        {
                return sizeof(*ptr);
        }

        int     main(void)
        {
                TYPE_S  t[2];

                printf("%d\n", func(t));
        }

This, however, returns correctly the sizeof(TYPE_S), but it does not take into
account the fact that t actually contains two elements of TYPE_S. There is no
way, AFAIK, you could make func return the size of the stuff actually used when
the function is being called. In other words, *ptr does not contain enough
information.

Later,
AriL
--
All my opinions are mine and mine alone.



Sat, 18 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Thanks for the replies from people that understood my question and didn't  
tell me I needed to learn basic C. ; )

Basically

the size of a pointer is implementation dependent. With ANSI the type

void * should be portable?? The trick is not the size of a pointer or its  
type. It's the fact that the type information doesn't effeect the pointer  
it  gives info to the compiler to determine how the ++ operator & operator  
etc are to be implemented. These operators are overloaded and when you  
type a pointer say

int *ptr

The compiler now knows how to resolve

p++;
or for that matter

*p++;

I'm sure I can dig out how the overloaded operators are resolved for  
pointers if I want but there's no need really. Its generic stuff from  
compiler design books. I just wasn't thinking "right" : (

In short the type of a pointer is provided to determine how to resolve the  
overloaded C operators much in the same way that type is used for the /  
operator wich is overloaded to perform both integer and floating point  
arithmetic.

The type of a pointer is only important when its dereferenced or  
incremented. Thus this info is not "stored with a pointer" its used by the  
compiler for handling pointer arithmetic and dereferncing.

A little different from the handling of generic C data types.

What brought this about is that the postscript language allows one to form  
arrays that contain any data type. There basically a hybrid between C  
arrays and structures. In this case I'm pretty sure that each member has  
both its value and  size stored as a linked list along I assume with some  
pointer casting to move the postscript array increment.  I want to write a  
C function that will do the same thing. That is move through a structure  
using a integer counter to move a "pointer" to each data.

Thanks

Mike



Sat, 18 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

<snip>
   (undefined pointee type). In such constructs, you would have to cast the pointer
   to some type first, for example:
<snip>
                   ((char *)ptr)++;
                   c = *((char *)ptr);

The first of these is incorrect! Casting tells it to change the ptr to
a (char*) type, with the ++ you probably wanted to increment it, and
store it back. But, wait! Store where? ptr is not a char* variable: it
is a void* variable. So, you have to say `ptr = ((char*)ptr + 1);'
(where you did not need a cast again only because you were converting
to (void*) which is cast free in C. ((char*)ptr)++ is a syntax error
in C (i.e. a compiler must spot it!) which would have been the
equivalent of (char*)ptr = (char*)ptr + 1 : an equally meaningless
statement.

<snip>
           int  func(TYPE_S *ptr)
<snip>
   account the fact that t actually contains two elements of TYPE_S. There is no
   way, AFAIK, you could make func return the size of the stuff actually used when
   the function is being called. In other words, *ptr does not contain enough
   information.

*ptr is unlikely to contain the information anyway, you probably meant
ptr. ptr _need not_ contain the size information, but bounds checking
compilers are, in fact, _not_ disallowed.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sun, 19 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:
(Michael Emmel) writes:

<snip>
   In short the type of a pointer is provided to determine how to resolve the  
   overloaded C operators much in the same way that type is used for the /  
   operator wich is overloaded to perform both integer and floating point  
   arithmetic.

   The type of a pointer is only important when its dereferenced or  
   incremented. Thus this info is not "stored with a pointer" its used by the  
   compiler for handling pointer arithmetic and dereferncing.

Or more correctly, it _need_ not be stored with the pointer.

   A little different from the handling of generic C data types.

Sorry, I am lost: What is a generic C data type?

   What brought this about is that the postscript language allows one to form  
   arrays that contain any data type. There basically a hybrid between C  
   arrays and structures. In this case I'm pretty sure that each member has  
   both its value and  size stored as a linked list along I assume with some  
   pointer casting to move the postscript array increment.  I want to write a  
   C function that will do the same thing. That is move through a structure  
   using a integer counter to move a "pointer" to each data.

Actually, in postscript the simplest implementation requires that all
composite objects (i.e. strings, packedarrays, arrays, dicts etc.) be
stored by reference (i.e. through `pointer') when part of other
composite objects. Having done this, there may not be much point to
storing the other entities (i.e. ints, reals, names, null, mark
etc.) in a different way.

In C, you can mimic this arrangement by an array of pointers to union
of structs.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sun, 19 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:
Bhattacharya) writes:

> (Michael Emmel) writes:
> <snip>
<munch>
> In C, you can mimic this arrangement by an array of pointers to union
> of structs.

> Cheers
> Tanmoy
> --

Thanks ! I was getting there slowly Hmm array yes struct yes Union Hmmm  
how hmmm ??

Thanks a lot, saved me a little time.

Shoudn't it be and array of of pointers to a union of pointers to struct's  
??.

I check and see if all structs have the same size pointers??

Which makes you wonder about the size/type of a pointer : )

Of which I've had some good answers via email. Esp concerning portable  
answers.

Thanks for all the response and help from this group I've learned a lot!

Mike



Mon, 20 Jul 1998 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. type casting to / from pointers to incomplete data types

2. type casting to / from pointers to incomplete data types

3. cast pointer to struct to pointer to type of first member of struct

4. pointer to an array OF pointers to integer data types

5. Type Casting Const Pointers to non const pointers

6. HELP, I am having problems with pointer...

7. Am I being paranoid about vector pointers?

8. I am new to programming and am lost

9. Type Casting between Base and Derived Type (Serialization of objects with Type members)

10. Variables and types of type "Type"

11. Dereferencing pointer to incomplete type

12. problem about pointer used as function return type

 

 
Powered by phpBB® Forum Software