473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Find the size of a datatype

Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.

May 22 '06 #1
36 3786
Rahul K wrote:
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Who cares? Use `sizeof`, that's what it's /for/.

--
Chris "electrick hedgehog" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

May 22 '06 #2

Rahul K wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Why "of course", and why without `sizeof`. That's what it's for, after
all.

One recourse remaining to you seems to be grepping/eyeballing for the
type definition in the code (or looking into the C Standard for
built-in types) and figuring it "manually".

May 22 '06 #3
Rahul K wrote:

Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


You can do it with lucky guess.

--
pete
May 22 '06 #4
Ico
Rahul K <ra**************@gmail.com> wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Such hypotethical questions can only come from homework. Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}

--
:wq
^X^Cy^K^X^C^C^C^C
May 22 '06 #5

Ico wrote:
Rahul K <ra**************@gmail.com> wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Such hypotethical questions can only come from homework. Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}

--
:wq
^X^Cy^K^X^C^C^C^C


Thank you.
It's very nice of you.

Regards
Sunil

May 22 '06 #6
"Vladimir Oka" <no****@btopenworld.com> writes:
Rahul K wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Why "of course", and why without `sizeof`. That's what it's for, after
all.

One recourse remaining to you seems to be grepping/eyeballing for the
type definition in the code (or looking into the C Standard for
built-in types) and figuring it "manually".


That's not possible in general. No amount of looking into the C
standard will tell you how big an int is.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 22 '06 #7
Sunil Varma wrote:
Ico wrote:
Rahul K <ra**************@gmail.com> wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Such hypotethical questions can only come from homework. Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}

--
:wq
^X^Cy^K^X^C^C^C^C


Thank you.
It's very nice of you.


If you care for clearing your subject, you'll try to understand the
above code before blindly submitting it for your homework.

May 22 '06 #8
Keith Thompson opined:
"Vladimir Oka" <no****@btopenworld.com> writes:
Rahul K wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and
of course without using sizeof operator.


Why "of course", and why without `sizeof`. That's what it's for,
after all.

One recourse remaining to you seems to be grepping/eyeballing for
the type definition in the code (or looking into the C Standard for
built-in types) and figuring it "manually".


That's not possible in general. No amount of looking into the C
standard will tell you how big an int is.


You're right, of course. I should have said "your implementation's
documentation".

--
But what can you do with it? -- ubiquitous cry from Linux-user
partner.
(Submitted by Andy Pearce, aj*@hpopd.pwd.hp.com)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 22 '06 #9
Keith Thompson said:
No amount of looking into the C standard will tell you how big an int is.


You can deduce from the C standard that an int is sizeof(int) bytes in size.

The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 22 '06 #10
Richard Heathfield <in*****@invalid.invalid> wrote:
Keith Thompson said:
No amount of looking into the C standard will tell you how big an int is.

You can deduce from the C standard that an int is sizeof(int) bytes in size.
The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.


My guess is that the source of this inquire is a tricky interview
question, with the expected answer being something along the lines of
this:

#include <stdio.h>
#include <stdlib.h>

int how_not_to_get_the_sizeof_int()
{
int array[2];
char *i1 = (char *) array;
char *i2 = (char *) (array+1);
return i2 - i1;
}

int main()
{
printf("The size of int seems to be: %d\n",
how_not_to_get_the_sizeof_int());
return EXIT_SUCCESS;
}

i.e., something that shows some understanding of C and machine
architecture, but would not be used in production code.
May 22 '06 #11
On Mon, 22 May 2006 17:02:17 -0400, Roberto Waltman
<us****@rwaltman.net> wrote:
Richard Heathfield <in*****@invalid.invalid> wrote:
Keith Thompson said:
No amount of looking into the C standard will tell you how big an int is.

You can deduce from the C standard that an int is sizeof(int) bytes in size.
The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.


My guess is that the source of this inquire is a tricky interview
question, with the expected answer being something along the lines of
this:

#include <stdio.h>
#include <stdlib.h>

int how_not_to_get_the_sizeof_int()
{
int array[2];
char *i1 = (char *) array;
char *i2 = (char *) (array+1);
return i2 - i1;
}

int main()
{
printf("The size of int seems to be: %d\n",
how_not_to_get_the_sizeof_int());
return EXIT_SUCCESS;
}

i.e., something that shows some understanding of C and machine
architecture, but would not be used in production code.


Oops! I missed the part about "not declaring a variable of the
type..." No valid answer then, as far as I know.
May 22 '06 #12
Richard Heathfield <in*****@invalid.invalid> writes:
Keith Thompson said:
No amount of looking into the C standard will tell you how big an int is.
You can deduce from the C standard that an int is sizeof(int) bytes in size.


True.
The OP's obsession with avoiding sizeof should be ignored; frankly, it's
ridiculous.


Of course it's ridiculous, but the OP needs to understand that it's
ridiculous. Simply ignoring it doesn't accomplish that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 22 '06 #13
Ico wrote:

Rahul K <ra**************@gmail.com> wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type,
and of
course without using sizeof operator.


Such hypotethical questions can only come from homework.
Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}


I don't think that the result of adding one
to a null pointer constant, is defined.

--
pete
May 23 '06 #14
pete wrote:

Ico wrote:

Rahul K <ra**************@gmail.com> wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type,
and of
course without using sizeof operator.


Such hypotethical questions can only come from homework.
Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}


I don't think that the result of adding one
to a null pointer constant, is defined.


That should be "null pointer"
instead of "null pointer constant".

--
pete
May 23 '06 #15
jjf

Roberto Waltman wrote:
On Mon, 22 May 2006 17:02:17 -0400, Roberto Waltman

My guess is that the source of this inquire is a tricky interview
question, with the expected answer being something along the lines of
this:

#include <stdio.h>
#include <stdlib.h>

int how_not_to_get_the_sizeof_int()
{
int array[2];
char *i1 = (char *) array;
char *i2 = (char *) (array+1);
return i2 - i1;
}

int main()
{
printf("The size of int seems to be: %d\n",
how_not_to_get_the_sizeof_int());
return EXIT_SUCCESS;
}

i.e., something that shows some understanding of C and machine
architecture, but would not be used in production code.
Oops! I missed the part about "not declaring a variable of the
type..." No valid answer then, as far as I know.


.... where the OP asked
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Your approach is fine - you don't have any variables of the forbidden
types. You're using a variable of type <array of X>.

May 23 '06 #16
Ico
pete <pf*****@mindspring.com> wrote:
Ico wrote:

Rahul K <ra**************@gmail.com> wrote:
> Suppose I am given a datatype say X in C. How can i find its size
> without declaring a variable or pointer variable of that type, and
> of course without using sizeof operator.
here, I'm glad to help you doing your assignments. Try this :


[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.


I'm not sure about this either, but 0 is not NULL, so this might be
valid. But this stuff is what you get if you insist on not using
sizeof(), ofcourse.

--
:wq
^X^Cy^K^X^C^C^C^C
May 23 '06 #17
you can see U as (int)((X *)0+1);
0 is converted to a pointer point to a X.
and the value of that pointer is 0(not NULL,it was just point to the
address 0).
then we add 1 to that pointer.so it point to the next X. the address
the pointer point to now is 0+sizeof(X);
so we get an address if we use (int)((X*)0+1)
maybe you can try these:
(X*)1+1;
(X*)1+2;
(X*)2+1;

Ico wrote:
Rahul K <ra**************@gmail.com> wrote:
Hello all
Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and of
course without using sizeof operator.


Such hypotethical questions can only come from homework. Like everybody
here, I'm glad to help you doing your assignments. Try this :

----------------------------------------------------------

#include <stdio.h>

/* Place your type in 'X' */

#define X char

/* Some constants */

#define Q (Y-Y)
#define R (Y/Y)
#define S (V Q)
#define T (S+R)
#define U (Z T)
#define V (X W)
#define W (*)
#define Y (0xffff-1)
#define Z (int)

int main(void)
{
printf("size of X is %d\n", U);

return 0;
}

--
:wq
^X^Cy^K^X^C^C^C^C


May 23 '06 #18
Ico wrote:
pete <pf*****@mindspring.com> wrote:
Ico wrote:
Rahul K <ra**************@gmail.com> wrote:

Suppose I am given a datatype say X in C. How can i find its size
without declaring a variable or pointer variable of that type, and
of course without using sizeof operator.
here, I'm glad to help you doing your assignments. Try this :

[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.


I'm not sure about this either, but 0 is not NULL, so this might be


NULL is a macro. If 0 is used in a pointer context then it give you a
null pointer. Adding to a null pointer or a null pointer constant is
undefined. All this has been discussed on the group several times.
valid. But this stuff is what you get if you insist on not using
sizeof(), ofcourse.


Which is why sizeof and offsetof exist. They can't be implemented portably.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 23 '06 #19
vire wrote:

Please do not top post. Your reply belongs under the text you are
replying to.
you can see U as (int)((X *)0+1);
0 is converted to a pointer point to a X.
and the value of that pointer is 0(not NULL,it was just point to the
address 0).
No, it does *not* point to address 0. (X *)0 is a null pointer (NULL is
a macro not a value). Pointer arithmetic is only defined for pointers to
objects or 1 past the end of an object (both initial value and result
having to fit). The null pointer explicitly does *not* point at an object.
then we add 1 to that pointer.so it point to the next X. the address
the pointer point to now is 0+sizeof(X);
so we get an address if we use (int)((X*)0+1)
maybe you can try these:
(X*)1+1;
(X*)1+2;
(X*)2+1;


None of those are defined by the C language.

<snip text which should have been above>
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 23 '06 #20
Ico said:
I'm not sure about this either, but 0 is not NULL,


Actually, NULL can be 0. See 3.2.2.3 in C89 (or 6.3.2.3(3) if you prefer
C99).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 23 '06 #21
Ico wrote:

pete <pf*****@mindspring.com> wrote:
Ico wrote:

Rahul K <ra**************@gmail.com> wrote:

> Suppose I am given a datatype say X in C.
> How can i find its size
> without declaring a variable or pointer variable of that type,
> and of course without using sizeof operator.

here, I'm glad to help you doing your assignments. Try this :

[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.


I'm not sure about this either, but 0 is not NULL, so this might be
valid.


((char *)0) is a null pointer.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.
But this stuff is what you get if you insist on not using
sizeof(), ofcourse.


--
pete
May 23 '06 #22

Flash Gordon wrote:
vire wrote:

Please do not top post. Your reply belongs under the text you are
replying to.
you can see U as (int)((X *)0+1);
0 is converted to a pointer point to a X.
and the value of that pointer is 0(not NULL,it was just point to the
address 0).


No, it does *not* point to address 0. (X *)0 is a null pointer (NULL is
a macro not a value). Pointer arithmetic is only defined for pointers to
objects or 1 past the end of an object (both initial value and result
having to fit). The null pointer explicitly does *not* point at an object.

in <stdio.h> i found:
#define NULL 0
#else
#define NULL ((void *)0)

yes ,it is true that NULL is a macro . but what is a macro ?
and how can you explain these codes:

int *p=NULL;
printf("%p",p);
then we add 1 to that pointer.so it point to the next X. the address
the pointer point to now is 0+sizeof(X);
so we get an address if we use (int)((X*)0+1)
maybe you can try these:
(X*)1+1;
(X*)1+2;
(X*)2+1;


None of those are defined by the C language.

i am so sorry that i have not study the c standard.but those codes do
make some effect.
then how do they come about?

May 23 '06 #23

vire wrote:
Flash Gordon wrote:
vire wrote:

Please do not top post. Your reply belongs under the text you are
replying to.
you can see U as (int)((X *)0+1);
0 is converted to a pointer point to a X.
and the value of that pointer is 0(not NULL,it was just point to the
address 0).
No, it does *not* point to address 0. (X *)0 is a null pointer (NULL is
a macro not a value). Pointer arithmetic is only defined for pointers to
objects or 1 past the end of an object (both initial value and result
having to fit). The null pointer explicitly does *not* point at an object.

in <stdio.h> i found:
#define NULL 0
#else
#define NULL ((void *)0)

yes ,it is true that NULL is a macro . but what is a macro ?


Macro is a text substitution construct.
NULL macro expands to a null pointer constant.
and how can you explain these codes:

int *p=NULL;
Variable `p` is assigned a null pointer value.
printf("%p",p);
This is then output to `stdout` in the implementation defined way.
then we add 1 to that pointer.so it point to the next X. the address
the pointer point to now is 0+sizeof(X);
so we get an address if we use (int)((X*)0+1)
maybe you can try these:
(X*)1+1;
(X*)1+2;
(X*)2+1;


None of those are defined by the C language.

i am so sorry that i have not study the c standard.but those codes do
make some effect.


And their effect is to invoke Undefined Behaviour (them not being
defined by the C Standard). That means that they're allowed to do
*anything*, including what you expect them to do. However, tomorrow
they may do something different.
then how do they come about?


They come about by poor programming skills.

May 23 '06 #24
vire wrote:
in <stdio.h> i found:
#define NULL 0
#else
#define NULL ((void *)0)

yes ,it is true that NULL is a macro . but what is a macro ?
A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?
and how can you explain these codes:

int *p=NULL;
printf("%p",p);
`p` is assigned the null pointer. Then its implementation-specific representation
is printed out. I don't see a problem here.
> then we add 1 to that pointer.so it point to the next X. the address
> the pointer point to now is 0+sizeof(X);
> so we get an address if we use (int)((X*)0+1)
> maybe you can try these:
> (X*)1+1;
> (X*)1+2;
> (X*)2+1;


None of those are defined by the C language.

i am so sorry that i have not study the c standard.but those codes do
make some effect.


And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.
then how do they come about?


Someone typed them.

--
Chris "a monkey trying desperately for Shakespear?" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

May 23 '06 #25
Ico
pete <pf*****@mindspring.com> wrote:
Ico wrote:

pete <pf*****@mindspring.com> wrote:
> Ico wrote:
>>
>> Rahul K <ra**************@gmail.com> wrote:
>>
>> > Suppose I am given a datatype say X in C.
>> > How can i find its size
>> > without declaring a variable or pointer variable of that type,
>> > and of course without using sizeof operator.
>>
>> here, I'm glad to help you doing your assignments. Try this :
>>


[ ..snip questionable code.. ]
> I don't think that the result of adding one to a null pointer
> constant, is defined.


I'm not sure about this either, but 0 is not NULL, so this might be
valid.


((char *)0) is a null pointer.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.


True, I was wrong, you are right.

--
:wq
^X^Cy^K^X^C^C^C^C
May 23 '06 #26
Ico wrote:

pete <pf*****@mindspring.com> wrote:
Ico wrote:

pete <pf*****@mindspring.com> wrote:
> Ico wrote:
>>
>> Rahul K <ra**************@gmail.com> wrote:
>>
>> > Suppose I am given a datatype say X in C.
>> > How can i find its size
>> > without declaring a variable or pointer variable of that type,
>> > and of course without using sizeof operator.
>>
>> here, I'm glad to help you doing your assignments. Try this :
>>

[ ..snip questionable code.. ]

> I don't think that the result of adding one to a null pointer
> constant, is defined.

I'm not sure about this either, but 0 is not NULL, so this might be
valid.


((char *)0) is a null pointer.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.


True, I was wrong, you are right.


His instructor probably doesn't even know.

--
pete
May 23 '06 #27

Chris Dollin wrote:
yes ,it is true that NULL is a macro . but what is a macro ?


A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?

so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

i think the null pointer is indeed point to some place(the address
0).but we just call it null.
and we can not read or write on address 0.is that right?
and how can you explain these codes:

int *p=NULL;
printf("%p",p);


`p` is assigned the null pointer. Then its implementation-specific representation
is printed out. I don't see a problem here.

what i want to say is , the value of p is 0
so i think a pointer point to address 0 is so called null pointer ,is
that right?
> (X*)1+1;
> (X*)1+2;
> (X*)2+1;


And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.


int a;
int* p=&a;
p+1;
is p+1 defined by c language ? plz tell me.i really don't have any idea
about it.

May 23 '06 #28
vire wrote:
Chris Dollin wrote:
> yes ,it is true that NULL is a macro . but what is a macro ?
A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?

so you get a constant 0 if you use NULL. right?


That depends on the expansion of NULL. So long as it expands to a
null pointer constant, that's OK. 0 is a null pointer constant,
but there are null pointer constants that are not (literally) 0.
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;
No, `int *p=(int*)0x13ff7c;` is either undefined or implementation-defined
(I forget which, but it doesn't matter here). How an implementation
chooses to convert an integer to a pointer isn't specified by C. It's
expected to be useful (or at least not deliberately useless) on your
platform, but that's not part of the definition, that's part of not being
a stupid implementation - not all uses of C need be portable.
i think the null pointer is indeed point to some place(the address
0).but we just call it null.
You can think that if you like, but that doesn't make it true. All that's
required is that assigning a null pointer constant to a pointer gives that
pointer a value that doesn't compare equal to the address of any (C) object.
In particular, there is /no requirement/ that the null pointer /value/ be
some all-bits-zero value. A sufficiently determined implementor could
represent it with the bit-pattern 0x11, or 0x2a, or 0xdeadbeef. And
whatever bit-pattern gets chosen, it can be arranged that there is nothing
at that address, with the co-operation of the operating system.

Using all-bits-zero is frequently possible and convenient and used; but it's
not /required/.
and we can not read or write on address 0.is that right?
C doesn't say. It doesn't say /anything/ about "address 0". If an implementation
has an "address 0", it may (or may not) allow you to read it, and it may (or
may not) allow you to write to it.
> and how can you explain these codes:
>
> int *p=NULL;
> printf("%p",p);


`p` is assigned the null pointer. Then its implementation-specific representation
is printed out. I don't see a problem here.

what i want to say is , the value of p is 0
so i think a pointer point to address 0 is so called null pointer ,is
that right?


The value of `p` is the null pointer.
>> > (X*)1+1;
>> > (X*)1+2;
>> > (X*)2+1;


And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.


int a;
int* p=&a;
p+1;
is p+1 defined by c language ?


Yes. It is legal to point one past the end of an object. (Otherwise a whole
slew of looping idioms would be illegal.)
plz tell me.i really don't have any idea about it.


Books. And the standard.

--
Chris "electrick hedgehog" Dollin
"A rock is not a fact. A rock is a rock."

May 23 '06 #29
"vire" <Av*****@gmail.com> wrote:
Chris Dollin wrote:
yes ,it is true that NULL is a macro . but what is a macro ?
A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?

so you get a constant 0 if you use NULL. right?


Wrong. You get _a_ null pointer constant. This may be any integer
constant with the value 0, or any such constant cast to void *. 0 is one
of them, but not the only one.
then in
int *p=NULL;
p is a null pointer
Yes, because the Standard explicitly demands that a null pointer
constant, when evaluated in a pointer context, is translated to a null
pointer. No other constants get this special treatment; neither do
integer _objects_ which happen to have the value 0.
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;
Wrong. There is nothing above which should lead you to this conclusion;
and in fact it is not one you can rely on. It _may_ happen that way on
some systems; and on another system, it may make p point to address
0x0013ff70; on yet another, it may make p point to address S:13-O:ff7c;
and on a third, it may cause your program to crash, burn, and scream
"Bus error! Bus error! Oh, the ignominy!".
i think the null pointer is indeed point to some place
What you think is completely immaterial if it flies in the face of what
the ISO C Standard says.
(the address 0).but we just call it null.
and we can not read or write on address 0.is that right?
No. That is wrong. Or rather, that is an unwarranted extrapolation from
limited desk-top experience.
so i think a pointer point to address 0 is so called null pointer ,is
that right?
Ditto.
> > (X*)1+1;
> > (X*)1+2;
> > (X*)2+1;


And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.


int a;
int* p=&a;
p+1;
is p+1 defined by c language ?


Yes. It points one past _the object_ a. Note that in this case you did
start with an actual object, and the address one past any real object
must exist, and a pointer to it must be valid. You still cannot read
from or write through it, but you _can_ calculate it. (The reason for
this, by the way, is that it's useful in loops.)
plz


Don't use such imbecilic school-boy abbreviations if you want to be
taken for an adult.

Richard
May 23 '06 #30
vire wrote:
Chris Dollin wrote:
yes ,it is true that NULL is a macro . but what is a macro ? A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?

so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;


It could be anything. If you are working on an implementation with 16
bit pointers (which is legal) it is unlikely to be the address you
suggest! The mapping is implementation defined, and the implementation
does not have to define a mapping for every value.
i think the null pointer is indeed point to some place(the address
0).but we just call it null.
Or you might get ((void*)0)
Or (5-5)
Or anything else that satisfies the standard.

There is absolutely no guarantee though that this corresponds to address
0. The null pointer could have an internal representation of all bits
set, or anything else that the autor(s) of the implementation decide on.
and we can not read or write on address 0.is that right?
That may or may not be true. Regardless, the standard does not define
what happens if you dereference a null pointer.
and how can you explain these codes:

int *p=NULL;
printf("%p",p);

`p` is assigned the null pointer. Then its implementation-specific representation
is printed out. I don't see a problem here.

what i want to say is , the value of p is 0


No, the value of p is a null pointer.
so i think a pointer point to address 0 is so called null pointer ,is
that right?


No. A null pointer is called a null pointer. Address 0 might be a valid
address. It is just that C allows you to use an integer constant 0 in
the *source* as a null pointer constant.
> (X*)1+1;
> (X*)1+2;
> (X*)2+1;

And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.


int a;
int* p=&a;
p+1;
is p+1 defined by c language ? plz tell me.i really don't have any idea
about it.


You are allowed to point one passed the end of an object/array, so p+1
is defined in the above. However, you are not allowed to dereference it. So
*(p+1);
would be "illegal". I.e., it invokes undefined behaviour so the program
is allowed to do *anything*. This is despite the value not being used.

You should read the comp.lang.c FAQ (Google will point you at it) in
particular section 6.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 23 '06 #31
"vire" <Av*****@gmail.com> writes:
Chris Dollin wrote:
> yes ,it is true that NULL is a macro . but what is a macro ?


A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?

so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

i think the null pointer is indeed point to some place(the address
0).but we just call it null.
and we can not read or write on address 0.is that right?


No.

Section 5 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has a good
explanation of this. After you've absorbed that, read section 4, then
the rest of the FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 23 '06 #32
pete <pf*****@mindspring.com> writes:
[...]
((char *)0) is a null pointer.


More precisely, it's a null pointer constant; it evaluates to a null
pointer value at run time.

(If I wanted to be really pedantic, I'd mention that the standard
doesn't actually say that a parenthesized null pointer constant is a
null pointer constant; the intent is sufficiently clear, so I won't
mention it.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 23 '06 #33
Keith Thompson wrote:
pete <pf*****@mindspring.com> writes:
[...]
((char *)0) is a null pointer.


More precisely, it's a null pointer constant; it evaluates to a
null pointer value at run time.

(If I wanted to be really pedantic, I'd mention that the standard
doesn't actually say that a parenthesized null pointer constant
is a null pointer constant; the intent is sufficiently clear, so
I won't mention it.)


I am grateful that you didn't waste anybodies time mentioning it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 23 '06 #34
In article <e4**********@malatesta.hpl.hp.com> Chris Dollin <ch**********@hp.com> writes:
vire wrote: ....
i think the null pointer is indeed point to some place(the address
0).but we just call it null. .... Using all-bits-zero is frequently possible and convenient and used; but it's
not /required/.


The Data General MV 4000 was a machine where a null pointer did *not* have all
bits zero.
and we can not read or write on address 0.is that right?


C doesn't say. It doesn't say /anything/ about "address 0". If an
implementation has an "address 0", it may (or may not) allow you to
read it, and it may (or may not) allow you to write to it.


I do not know how C was implemented on the Transputer (if it was implemented)
but on that machine addresses were done as signed integers and address 0
was in the middle of the space that could be used. So I think that on that
machine a null pointer would also not have all bits 0.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
May 24 '06 #35

Keith Thompson wrote:
"vire" <Av*****@gmail.com> writes:
Chris Dollin wrote:
> yes ,it is true that NULL is a macro . but what is a macro ?

A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?

so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

i think the null pointer is indeed point to some place(the address
0).but we just call it null.
and we can not read or write on address 0.is that right?


No.

Section 5 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has a good
explanation of this. After you've absorbed that, read section 4, then
the rest of the FAQ.

many thaks to
Chris Dollin, Richard Bos, Flash Gordon, and Keith Thompson.
i think i should have read through the faq and the standard before i
post a new message.
now i am reading them.thank you all guys for your helpful advices.

May 24 '06 #36

Dik T. Winter wrote:
I do not know how C was implemented on the Transputer (if it was implemented)
but on that machine addresses were done as signed integers and address 0
was in the middle of the space that could be used. So I think that on that
machine a null pointer would also not have all bits 0.
--


The underlying implementation of null can be anything as the address
does not necessary have to be 0. The compiler, however, must treat it
as a constant. In the transputer C compiler, the physical address of
null is MINT (minimum integer), but the C compiler allows you to code:

if (p!= NULL)

which is (char *) 0 in the compiler, but gets translated to if (p !=
MINT) in assembly. The spec doesnt say what the PHYSICAL ADDRESS of
NULL has to be, just how the compiler treats it...
Ram

May 24 '06 #37

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
1
by: Raj | last post by:
Hi all, I am using crypt on PHP to encrypt a word and am in the process of setting up the mysql databse to store the encypted word. For the mysql database: 1) What datatype should I use?...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Bruno BAGUETTE | last post by:
Hello, How many memory does take numeric(3,0) ? Is it smaller than INT2 that takes 2 octets ? Thanks in advance :-) --------------------------------------- Bruno BAGUETTE -...
79
by: syntax | last post by:
what is the size of a pointer? suppose i am writing, datatype *ptr; sizeof(ptr); now what does this sizeof(ptr) will give? will it give the size of the
6
by: ~Maheshkumar.R | last post by:
The error i'm getting is as: > Server Error in '/' Application. > -------------------------------------------------------------------------- -- > String or binary data would be truncated. The...
0
by: SMH | last post by:
Hi All, I am currently learning .Net 2, studying for 70-528. I've hit a bit of a brick wall with DataColumn.Expression. As I understand it, this can be used to (For example) concatenate two...
3
by: Karthik01 | last post by:
Hi, i have written a VB script to find a node name in an XML.Now i need to search for that node name in another XML. How can i do it. My XML looks like this - <xml...
0
by: zephyrus360 | last post by:
This is about a technique to find the mod of a very large integer with a normal small integer. I recently encountered this problem when I needed to compute the modulus of a very large number with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.