i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
thanks 10 1544
On Sep 13, 12:34 pm, goacr...@gmail.com wrote:
i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
thanks
I run the program in vc :)
On Sep 13, 9:34 am, goacr...@gmail.com wrote:
i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
thanks
This is becuase,
sizeof('h')[p] will return the size of the expression ('h')[p] which
will always be 1 as the p is pointer to char.
On Wed, 12 Sep 2007 21:34:01 -0700, go******@gmail.com wrote:
>i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
Compare these two expressions:
char n=(sizeof('h'))[p]; (1)
char n=sizeof(('h')[p]); (2)
Your second expression is being interpreted as (2), and what you
wanted was (1)
The problem lies in sizeof beˇing greedy when it refers to an object
and not to a type
Best regrads,
Zara
Zara wrote, On 13/09/07 07:00:
On Wed, 12 Sep 2007 21:34:01 -0700, go******@gmail.com wrote:
>i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
Compare these two expressions:
char n=(sizeof('h'))[p]; (1)
This is the same as
char n = (sizeof 'h')[p];
This will still not do what the OP expected in C although it will in
C++. This is because a character literal like 'h' has type int in C
(probably 4 on the OPs system, although it could be any other number)
whereas in C++ it would have type char.
char n=sizeof(('h')[p]); (2)
Your second expression is being interpreted as (2), and what you
wanted was (1)
The problem lies in sizeof beˇing greedy when it refers to an object
and not to a type
Also note that sizeof is an operator, NOT a function. This is why you
can do "sizeof expression" rather than "sizeof(expression)" and is
probably also part of the OPs confusion.
--
Flash Gordon
On Sep 13, 2:00 pm, Zara <me_z...@dea.spamcon.orgwrote:
On Wed, 12 Sep 2007 21:34:01 -0700, goacr...@gmail.com wrote:
i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
Compare these two expressions:
char n=(sizeof('h'))[p]; (1)
char n=sizeof(('h')[p]); (2)
Your second expression is being interpreted as (2), and what you
wanted was (1)
The problem lies in sizeof beˇing greedy when it refers to an object
and not to a type
Best regrads,
Zara
Thank you all.
It's clear now. :)
On Wed, 12 Sep 2007 21:34:01 -0700, goacross wrote:
i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
sizeof('h') is sizeof(int). If it is four, 4+"abc" will point past
the end of the string, so dereferencing it, even implicitly with
[], will cause UB. If sizeof(int) is 4, even pointer arithmetic
itself will cause UB, even if you don't dereference. If
sizeof(int) < 4, n will be 'b', 'c', or 0.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen
On Wed, 12 Sep 2007 21:34:01 -0700, go******@gmail.com wrote:
>i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
*(1+p) = *(p+1) = p[1] = 1[p].
Remove del for email
On Fri, 14 Sep 2007 12:35:41 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
>On Wed, 12 Sep 2007 21:34:01 -0700, goacross wrote:
>i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
sizeof('h') is sizeof(int). If it is four, 4+"abc" will point past the end of the string, so dereferencing it, even implicitly with [], will cause UB. If sizeof(int) is 4, even pointer arithmetic itself will cause UB, even if you don't dereference. If sizeof(int) < 4, n will be 'b', 'c', or 0.
Except for variable length arrays (not germane to this thread), sizeof
never evaluates its operand. Consequently, nothing gets dereferenced.
The operand is analyzed only to determine its type, not whether it
actually exists.
Remove del for email
On Fri, 14 Sep 2007 14:18:21 -0700, Barry Schwarz wrote:
On Fri, 14 Sep 2007 12:35:41 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
>>On Wed, 12 Sep 2007 21:34:01 -0700, goacross wrote:
>>2. char n=sizeof('h')[p]; // n=1;
sizeof('h') is sizeof(int). If it is four, 4+"abc" will point past the end of the string, so dereferencing it, even implicitly with [], will cause UB. If sizeof(int) is 4, even pointer arithmetic itself will cause UB, even if you don't dereference. If sizeof(int) < 4, n will be 'b', 'c', or 0.
Except for variable length arrays (not germane to this thread), sizeof
never evaluates its operand. Consequently, nothing gets dereferenced.
The operand is analyzed only to determine its type, not whether it
actually exists.
Yeah, damned syntax... sizeof('h')[p] means sizeof(('h')[p]), not
(sizeof('h'))[p] as I was thinking when I wrote that reply...
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen
"Barry Schwarz" <sc******@doezl.neta écrit dans le message de news: md********************************@4ax.com...
On Fri, 14 Sep 2007 12:35:41 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
>>On Wed, 12 Sep 2007 21:34:01 -0700, goacross wrote:
>>i found something tricky this morning.
char *p="abc";
1. char m=1[p];// m='b'
2. char n=sizeof('h')[p]; // n=1;
I guess the reason of 1is,
1+p=p+1; so the same as p[1];
but why the seemed same thing doesn't work on case 2?
sizeof('h') is sizeof(int). If it is four, 4+"abc" will point past the end of the string, so dereferencing it, even implicitly with [], will cause UB. If sizeof(int) is 4, even pointer arithmetic itself will cause UB, even if you don't dereference. If sizeof(int) < 4, n will be 'b', 'c', or 0.
No, you are not on the right track.
Except for variable length arrays (not germane to this thread), sizeof
never evaluates its operand. Consequently, nothing gets dereferenced.
The operand is analyzed only to determine its type, not whether it
actually exists.
sizeof does not evaluate its operand in variable length arrays either, the
evaluation is done at run-time to determine the size of the operand not
known at compile time, but that does not involve evaluating the operand.
The rest is true but unrelated to the previous post (erroneous) answer.
--
Chqrlie. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: nzanella |
last post by:
Hello,
I just thought I would share the following observation with the rest
of the group. The sizeof operator seems to act differently according
to whether the number of elements in the array is...
|
by: signuts |
last post by:
I'm aware of what sizeof(...) does, what I would like to know is if
sizeof(...) is compiled in or a function that's executed at run-time.
Like for example
{
int a;
printf("a is %d...
|
by: junky_fellow |
last post by:
Hi,
Is it possible to implement sizeof as a C function ?
|
by: shaanxxx |
last post by:
I have following programme:
#include<stdio.h>
int main()
{
int i=1;
char *ptr=(char *)&i;
if (*ptr)
|
by: birensubudhi |
last post by:
1) void foo(char *s,char *t)
{
while(*s++=*t++);
}
which C function is equivalent to foo ?
2) #define ROUND(x,n)
| |
by: tekninja |
last post by:
I wanted to write a function to take the tedium out of dynamically determining the total number of elements in an array.
Within the main method this is easy as I simply use:
sizeof( <array name>...
|
by: goacross |
last post by:
char ch='a';
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'?
thanks
|
by: jason |
last post by:
Hello,
I'm a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.
If I pass an array of structures to a function, then...
|
by: DamienS |
last post by:
In the interests of me saving hair, can someone please explain to me
what's going on below? Why doesn't == work in comparing two int's when
cast as objects? They're the same type.
Note that it...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |