473,385 Members | 1,740 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,385 software developers and data experts.

help:question about sizeof?

help:

i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)

"
how can i use pstr to get sizeof string which it point?

thx!
Apr 13 '07 #1
7 2357
"guoliang" <poster....@gmail.comwrote:
help:

i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char str[] = "Hello\n";
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
%d will tell printf to expect an int. You supply a size_t
which is unlikely to be a type that promotes to int.

printf("sizeof = %u\n", (int) sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get
sizeof(str) not sizeof(void *)
I suspect you meant sizeof(char *), although void and character
pointers will have the same size.
"
how can i use pstr to get sizeof string which it point?
You can't.

http://c-faq.com/malloc/sizeof.html

--
Peter

Apr 13 '07 #2
fuction "strlen()"
Apr 13 '07 #3
guoliang wrote:
help:

i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)

"
how can i use pstr to get sizeof string which it point?
Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
get the size of the _array_ to which it points: such information is not
incorporated into the pointer or into the array. In addition to the
obvious, that the sizeof a pointer is just the sizeof a pointer,
remember that sizeof yields a compile-time constant. Since the sizes of
arrays pointed can change during execution, they can hardly be
compile-time constants.
Apr 13 '07 #4
On Apr 13, 10:24 am, Martin Ambuhl <mamb...@earthlink.netwrote:
guoliang wrote:
help:
i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)
"
how can i use pstr to get sizeof string which it point?

Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
get the size of the _array_ to which it points: such information is not
incorporated into the pointer or into the array. In addition to the
obvious, that the sizeof a pointer is just the sizeof a pointer,
remember that sizeof yields a compile-time constant.
above statement hold true for c89. c99 has variable length array and
it needs run sizeof operator for variable length array.

6.5.3.4 The sizeof operator:
EXAMPLE 3 In this example, the size of a variable length array is
computed and returned from a
function:
#include <stddef.h>
size_t fsize3(int n)
{
char b[n+3]; // variable length array
return sizeof b; // execution time sizeof
}

Since the sizes of
arrays pointed can change during execution, they can hardly be
compile-time constants.

Apr 13 '07 #5
On Apr 13, 10:24 am, Martin Ambuhl <mamb...@earthlink.netwrote:
guoliang wrote:
help:
i try the code:
"
char str[]={'h','e','l','l','o','\n','\0'};
char *pstr=str;
printf("sizeof=%d\n",sizeof(str));
printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not
sizeof(void *)
"
how can i use pstr to get sizeof string which it point?

Well, the _string_ has a size of strlen(pstr)+1. You cannot use pstr to
get the size of the _array_ to which it points: such information is not
incorporated into the pointer or into the array. In addition to the
obvious, that the sizeof a pointer is just the sizeof a pointer,
remember that sizeof yields a compile-time constant.
above statement hold true for c89. c99 has variable length array and
it needs runtime sizeof operator for variable length array.

6.5.3.4 The sizeof operator:
EXAMPLE 3 In this example, the size of a variable length array is
computed and returned from a
function:
#include <stddef.h>
size_t fsize3(int n)
{
char b[n+3]; // variable length array
return sizeof b; // execution time sizeof
Since the sizes of
arrays pointed can change during execution, they can hardly be
compile-time constants.

Apr 13 '07 #6
On 12 Apr 2007 20:28:57 -0700, "Peter Nilsson" <ai***@acay.com.au>
wrote:
>%d will tell printf to expect an int. You supply a size_t
which is unlikely to be a type that promotes to int.
Agree.
printf("sizeof = %u\n", (int) sizeof(str));
Are you sure about that? %u is for unsigned int.

Jim
Apr 13 '07 #7
On Apr 13, 4:28 pm, JimS <s...@not.comwrote:
On 12 Apr 2007 20:28:57 -0700, "Peter Nilsson" <a...@acay.com.au>
printf("sizeof = %u\n", (int) sizeof(str));

Are you sure about that? %u is for unsigned int.
True. The cast should have been (unsigned). Thanks.

--
Peter

Apr 15 '07 #8

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

Similar topics

1
by: Brian Wilson | last post by:
Could someone help me with how to center or pocission this document to the center of the page? <body onload="ImageBook()"> <script type="text/javascript"> // 7 variables to control behavior...
2
by: junk | last post by:
I am converting a large website with VBScript include files and JScript also included as <script language="jscript" runat="server"> in other include files. I would like to convert the VBScript to...
3
by: Woody Splawn | last post by:
For reasons I have not yet identified I am having problems with the help system in VS.net 2003. It seems as though it was working all right in the past but it's not now. First of all I can't seem...
1
by: VRWC | last post by:
Hello dear People! For the very first time I have attempted to have BOTH Access 2000 AND Access 2002 installed on the same computer. I started with Access 2002 (XP). After I was sure the A2002...
12
by: uno | last post by:
Hello, I am a beginner with the programming and I have some question. I am reading manuals from Internet and some book from the library. In all those documents the C language operate wint plain...
3
by: JimC | last post by:
I have .NET Frame projects that can be downloaded from the Web in setup.exe files. These files are less than a megabyte in size. They are demos for clients. If the user's computer doesn't have...
3
by: Woody Splawn | last post by:
For reasons I have not yet identified I am having problems with the help system in VS.net 2003. It seems as though it was working all right in the past but it's not now. First of all I can't seem...
4
by: DjShifta | last post by:
Ok this really shouldn't be that hard...but I can not figure out whats' wrong for the life of me. I am brand new to "coding" and have thus far just built basic sites with Dreamweaver, but am going...
1
by: John | last post by:
Can somebody tell me why webform is not WYSIWYG ? In that case, should I use CSS to place the controls? Thanks,
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.