Simple question about pointer types

L

laredotornado

Hello,
Are all pointer types the same length? My instinct tells me yes,
but I just wanted to confirm with the experts. So if I have

typedef struct {
char* field1;
int field2;
} myStruct;

myStruct *a;
char *b;
int *c;

Are the variables "a", "b", and "c" all 32 bits in length? Thanks, -
Dave
 
S

S.Tobias

Hello,
Are all pointer types the same length?

ITYM "size"
My instinct tells me yes,
but I just wanted to confirm with the experts. So if I have
[snip]

myStruct *a;
char *b;
int *c;
Are the variables "a", "b", and "c" all 32 bits in length? Thanks, -

No, the size is not specified in either case.

There are four groups of pointers that are required to have the
same representation and alignment (hence size, too): pointers to
1) un/qualified compatible types; 2) void and character types; 3)
struct types; 4) union types.

So, for an example, in theory
unsigned int *up;
signed int *sp;
`up' and `sp' might have different sizes (and other properties).
 
D

Daniel Etzold

Hello,
Are all pointer types the same length? My instinct tells me yes,
but I just wanted to confirm with the experts. So if I have

typedef struct {
char* field1;
int field2;
} myStruct;

myStruct *a;
char *b;
int *c;

Are the variables "a", "b", and "c" all 32 bits in length? Thanks, -

yes

you can check this with sizeof( char* ) and so on.

Regards,
Daniel
 
P

pete

Hello,
Are all pointer types the same length?

They don't have to be.
(char *) and (void *) have the same representation.
Pointers to struct types are all the same size as each other.
Other types can be different.
myStruct *a;
char *b;
int *c;

Are the variables "a", "b", and "c" all 32 bits in length?

At your house? Maybe, I don't know. Everywhere else, maybe not

#include <stdio.h>
printf("sizeof (int *) is %lu.\n", (long unsigned)sizeof(int *));
printf("sizeof (char*) is %lu.\n", (long unsigned)sizeof(char*));
 
F

Flash Gordon

Hello,
Are all pointer types the same length? My instinct tells me yes,
but I just wanted to confirm with the experts. So if I have

typedef struct {
char* field1;
int field2;
} myStruct;

myStruct *a;
char *b;
int *c;

Are the variables "a", "b", and "c" all 32 bits in length? Thanks, -

As far as the standard is concerned they could all be different lengths
and using different representations. There is also no guarantee that an
implementation has *any* type which will be 32 bits in length.

The only reasons I can think of for needing to know what you are asking
are somewhat obscure. So I strongly suspect that you are approaching
whatever your real problem is in the wrong way.
 
C

Chris Croughton

Are all pointer types the same length? My instinct tells me yes,
but I just wanted to confirm with the experts. So if I have

They may be, but they needn't be. All that is required is that void*
has the same representation as char*.
typedef struct {
char* field1;
int field2;
} myStruct;

myStruct *a;
char *b;
int *c;

Are the variables "a", "b", and "c" all 32 bits in length?

On what platform? They could be all different lengths, or all 64 bits
(or 16, or 24, or any other number depending on the architecture).

For instance, take a word-addressed machine which addresses in 32 bit
words. If a char is 8 bits on such a machine, a char pointer (and
therefore also a void*) would need extra information to store which char
in the word is addressed. At least one CPU allowed memory to be
addressed as bit fields with a start position and a length, that could
have totally different sizes of pointers for different types.

Chris C
 
P

Prashant Mahajan

Yes all the pointers are of same size . "Internally" they all are
actully of type unsigned int , though u can specify them as what ever
datatype . What actully is a pointer , just a way to access the memory
address directly which obviuosly is of type unsigned int.
struct abc
{
--------
}*ptr1;

char * ptr2;

both ptr1 and ptr2 (though of incompatible types) have the same size
i.e equal to unsigned int, As far as different platforms are
concerned.The size of unsigned int varies from platform to platform ,
correspondingly the size of the pointers change.
Prashant Mahajan
 
P

pete

Prashant said:
Yes all the pointers are of same size . "Internally" they all are
actully of type unsigned int , though u can specify them as what ever
datatype . What actully is a pointer , just a way to access the memory
address directly which obviuosly is of type unsigned int.
struct abc
{
--------
}*ptr1;

char * ptr2;

both ptr1 and ptr2 (though of incompatible types) have the same size
i.e equal to unsigned int, As far as different platforms are
concerned.The size of unsigned int varies from platform to platform ,
correspondingly the size of the pointers change.

Your post is pure fantasy.
 
K

Keith Thompson

Prashant Mahajan said:
Yes all the pointers are of same size .

This is often true on many systems, but it absolutely is not
guaranteed by the standard. There are good reasons why void* and
char* pointers might be bigger than int* pointers on some
architectures; similarly function pointers might be represented
differently than object pointers.
"Internally" they all are
actully of type unsigned int , though u can specify them as what ever
datatype . What actully is a pointer , just a way to access the memory
address directly which obviuosly is of type unsigned int.

No. As far as the language in concerned, pointers and integers are
distinct types, though conversions between them are allowed in some
limited circumstances. It is not at all "obvious" that a memory
address is of type unsigned int.

I've worked on several systems on which unsigned int is 32 bits and
pointers are 64 bits (and I expect such systems to become more common
over time).

C99 defines (in <stdint.h>) types intptr_t and uintptr_t that are big
enough to hold object pointer values. It's important to note that
these types are optional, that they aren't necessarily the same size
as an object pointer, and that they can't necessarily hold function
pointer values.

[snip]

Prashant, may I ask where you got your information?
 
K

Kenny McCormack

Keith Thompson said:
Prashant, may I ask where you got your information?

Answer: From experience.

Hint: Not everybody genuflects at the altar of the standard. In fact,
I would wager that Prashant has never even heard of "the standard".
 
K

Kenny McCormack

both ptr1 and ptr2 (though of incompatible types) have the same size
i.e equal to unsigned int, As far as different platforms are
concerned.The size of unsigned int varies from platform to platform ,
correspondingly the size of the pointers change.

Your post is pure fantasy.[/QUOTE]

No. In fact, his post is quite reasonable. His only error was neglecting
to include an obvious qualification, which I'm sure to him seemed so
obvious that he couldn't imagine people not assuming same.

That qualification goes something like:

On garden variety 32 bit Intel hardware, using normal compilers on
normal OSs. I.e., all the systems I (Prashant) have ever used.

P.S. Yes, I am well aware that there are words in the previous sentence
that clc pissant regulars can (and will) quibble with. Have a ball!
 
K

Keith Thompson

Answer: From experience.

Hint: Not everybody genuflects at the altar of the standard. In fact,
I would wager that Prashant has never even heard of "the standard".

_____________________
/| /| | |
||__|| | Please do not |
/ O O\__ | feed the |
/ \ | Trolls |
/ \ \|_____________________|
/ _ \ \ ||
/ |\____\ \ ||
/ | | | |\____/ ||
/ \|_|_|/ | _||
/ / \ |____| ||
/ | | | --|
| | | |____ --|
* _ | |_|_|_| | \-/
*-- _--\ _ \ | ||
/ _ \\ | / `'
* / \_ /- | | |
* ___ c_c_c_C/ \C_c_c_c____________
 
O

Old Wolf

Prashant said:
Yes all the pointers are of same size . "Internally" they all
are actully of type unsigned int , though u can specify them as
what ever datatype . What actully is a pointer , just a way to
access the memory address directly which obviuosly is of type
unsigned int.

Having programmed on a system where unsigned int is 2 bytes and
char * is 3 bytes, I can only say: "Pooh to you with knobs on!"
 
E

Eric Sosman

Old said:
Having programmed on a system where unsigned int is 2 bytes and
char * is 3 bytes, I can only say: "Pooh to you with knobs on!"

I'll bite: Can you tell us more about this system?
It sounds like it might have been the DS09000 (DeathStation
Octal Oh-Nine-Thousand, with an implementation-dependent
interpretation of the non-octal digit "nine").
 
B

Barry Schwarz

I'll bite: Can you tell us more about this system?
It sounds like it might have been the DS09000 (DeathStation
Octal Oh-Nine-Thousand, with an implementation-dependent
interpretation of the non-octal digit "nine").

I don't know about his machine but I work on big-endian one where all
pointers are 24 bits (with 8 padding bits on the high end) and all
unsigned ints are 32 bits. Originally manufactured by IBM as a
derivative of the commercial S/360 (yes, it is over 30 years old).


<<Remove the del for email>>
 
K

Kenny McCormack

/| /| | |
||__|| | Please do not |
/ O O\__ | feed the |
/ \ | Trolls |

Assuming, for the sake of argument, that you are calling me a troll, then
aren't you in direct violation of your own statement?
 
R

Richard Bos

Your post is pure fantasy.

No. In fact, his post is quite reasonable. His only error was neglecting
to include an obvious qualification, which I'm sure to him seemed so
obvious that he couldn't imagine people not assuming same.

That qualification goes something like:

On garden variety 32 bit Intel hardware, using normal compilers on
normal OSs. I.e., all the systems I (Prashant) have ever used.[/QUOTE]

In other words,

In my limited experience, which doesn't even go back as far as
MS-DOS, and is therefore hardly a guarantee in the real world.
P.S. Yes, I am well aware that there are words in the previous sentence
that clc pissant regulars can (and will) quibble with. Have a ball!

How about the entire sentence?

Richard
 
O

Old Wolf

Eric said:
I'll bite: Can you tell us more about this system?

It was an Intel 8051 CPU (8-bit), with three different address
spaces: 64K of data, 64K of code, and 128 bytes of registers.
Ints were 16 bits (2 bytes). You could declare a code pointer
or a data pointer of 2 bytes, using a compiler extension keyword,
but to have a pointer that could be pointing to any of those
three pages, required 2 extra significant bits (hence the third byte).
 
K

Keith Thompson

Old Wolf said:
It was an Intel 8051 CPU (8-bit), with three different address
spaces: 64K of data, 64K of code, and 128 bytes of registers.
Ints were 16 bits (2 bytes). You could declare a code pointer
or a data pointer of 2 bytes, using a compiler extension keyword,
but to have a pointer that could be pointing to any of those
three pages, required 2 extra significant bits (hence the third byte).

There would be no need for a char* to be able to point into the code
space (unless the compiler supports meaningful conversions between
char* and function pointers as an extension). Pointing into the
register space could be useful, of course (but again it's probably not
required for C semantics).

Out of curiosity, was this extended pointer format implemented in
hardware or in software? I know it doesn't matter as far as the C
language is concerned, but it would be nice to have more ammunition
against the assumption that a C pointer is always a hardware address.
 

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,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top