Re: size of pointer variables





Malcolm wrote:
> <junky_fellow@xxxxxxxxxxx> wrote
> > Can the size of pointer variables of different type may be
> > different on a particular architecture.
> > For eg.
> >
> > Can the sizeof (char *) be different from sizeof(int *) or
> > sizeof (void *) ?
> >
> Let's say we've got a machine that only adresses memory in 32-bit chunks
> (bytes).
> One obvious strategy would be to say that chars are 32 bits. This has some
> problems. For instance an image-processing routine that takes an array of
> unsigned chars as its argument would now gobble four times as much memory as
> necessary, or need to be rewritten.
> So the alternative is to make chars 8 bit, and do a bit of bit twiddling
> behind the scenes to produce the illusion of 8-bit bytes.
> But if our addresses are multiples of 32 bits, we need to tag on an extra
> two bits to tell us where the pointer points to.
> So char pointers become
> {
> address:
> offset:
> }
>
> The whole reason the machine accesses data in 32 bit bytes is for
> efficiency, so we don't want to encumber our int *s and double *s with
> similar tags. That would slow everything down.
>
> so char pointers and int pointers are now a different size.
> >
> >
> > What is the purpose of using a void pointer ? Instead of
> > declaring a pointer variable "void *", can I declare it
> > as "char *" and then later on typcast it to whatever type
> > as needed.
> >
> That was the old-fashioned way of doing things. void * is more for
> documentation, to tell the programmer that "this pointer points to memory of
> unknown type".
> unsigned char * can point to any memory. In practise
> double x;
> (unsigned char *ptr) = &x;
> double *dblptr = (double *) ptr;
>
> will work on pretty much any platform. However it might just be the case
> that the double pointers need to carry around an extra bit for some reason,
> in which case the code will break. void pointers, of course, are guaranteed
> to be able to hold any type.

Thank you to everyone for your help.
What I concluded is that since the size/representation of pointer
variables to
different data types may be different even on the same platform, we
should not
typecast one type pointer to other type except void pointer.

In some of the code pieces, I have seen that there is some pointer to a

structure. To initialize the structure with zero, the structure
pointer is typecasted to (char *) and passed as an argument to bzero().
Is this the right thing to do ?
I will give you the example.
typedef struct node {
long l;
int i ;
short s1;
short s2;
char c;
}node_t;

node_t * pstr_node;
bzero(((char *)pstr_node, sizeof (*pstr_node));

Is this legal ? Can we typecast structure pointer to char pointer ?



Let it be,

.



Relevant Pages

  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: confusion when comparing char * with a string literal
    ... char *, and array. ... when I try to comapare 'str' with "today", ... points it to somewhere in memory, or to malloc memory to it before we ... But what ur program does is it makes a pointer variable, ...
    (comp.lang.c)
  • Re: Simple code leads to segfault.. help
    ... non-const char. ... A pointer variable is just like any other variable -- it consumes memory ... The purpose of a double is to store a floating-point number. ... The purpose of a pointer is to store a memory address. ...
    (comp.lang.c)
  • Re: Simple code leads to segfault.. help
    ... They are a non-const pointer to a non-const char. ... A pointer variable is just like any other variable -- it consumes memory and stores data by arranging bits in a particular pattern. ... The purpose of a double is to store a floating-point number. ...
    (comp.lang.c)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)