473,609 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tricky use of sizeof [explanation wanted]

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

Sep 13 '07 #1
10 1556
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 :)

Sep 13 '07 #2
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.

Sep 13 '07 #3
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
Sep 13 '07 #4
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(express ion)" and is
probably also part of the OPs confusion.
--
Flash Gordon
Sep 13 '07 #5
On Sep 13, 2:00 pm, Zara <me_z...@dea.sp amcon.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. :)

Sep 13 '07 #6
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

Sep 14 '07 #7
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
Sep 14 '07 #8
On Fri, 14 Sep 2007 12:35:41 +0200, Army1987 <ar******@NOSPA M.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
Sep 14 '07 #9
On Fri, 14 Sep 2007 14:18:21 -0700, Barry Schwarz wrote:
On Fri, 14 Sep 2007 12:35:41 +0200, Army1987 <ar******@NOSPA M.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

Sep 15 '07 #10

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

Similar topics

7
4630
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 known. Hence when passing arrays to functions the number of elements in the array must always be passed as an argument. If STL is available then people can just use vectors of course. Anyways, I guess this stuff is pretty standard. Well, have a...
15
4937
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 bytes\n", sizeof(a)); }
15
3266
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
4
1311
by: shaanxxx | last post by:
I have following programme: #include<stdio.h> int main() { int i=1; char *ptr=(char *)&i; if (*ptr)
28
13586
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)
1
1636
tekninja
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> ) / sizeof( <array type> ) However I am running into an annoying problem when trying to pass an array reference to a function that is suppossed to return the same value as the above would. my function reads: int numElements( int array ){ ...
72
3008
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
20
2160
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 suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
16
1656
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 worked for strings. Thanks in advance, Damien
0
8067
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6053
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5509
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4015
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2529
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1380
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.