Hi, my problem with calculating the size of an array is when
I pass an array as a parameter to a function which perform the
calculation, the result never comes right, like below:
int SizeOfArray(int a[]) {
return (sizeof(a)/sizeof(a[0]));
}
main() {
int a[3]={1,2,3};
printf("%d\n",S izeOfArray(a));
}
but is right when the array declared in the same scope as the sizeof()
statment,
main() {
int a[3]={1,2,3};
printf("%d\n",s izeof(a)/sizeof(a[0]));
}
why's that ? i suspect that i passed the wrong type of parameter to
the function. (should be &a or *a or something... ??) 5 11942 ru****@sohu.com (sugaray) wrote in
news:ad******** *************** ***@posting.goo gle.com: Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below:
int SizeOfArray(int a[]) { return (sizeof(a)/sizeof(a[0]));
Bang! You have tricked yourself. Although you think you are passing an
array into SizeOfArray() you are actually passing a pointer which on
32-bit platforms is often only 4 bytes in size. You can (1) pass in the
size, (2)package the array inside a struct, or (3) simply USE THE C sizeof
OPERATOR!
#include <stdio.h>
struct ArrayWrap
{
int array[100];
} pkg;
int array[100];
void printPkgArrSize (struct ArrayWrap *pPkg)
{
printf("Size of array is %u bytes\n", sizeof pPkg->array);
}
void printArrSize(si ze_t size)
{
printf("Size of array is %u bytes\n", size);
}
int main(void)
{
/* 1
*/
printPkgArrSize (&pkg);
/* 2
*/
printArrSize(si zeof array);
/* 3
*/
printf("Size of array is %u bytes\n", sizeof array);
return 0;
}
--
- Mark ->
--
"sugaray" <ru****@sohu.co m> wrote in message
news:ad******** *************** ***@posting.goo gle.com... Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below:
int SizeOfArray(int a[]) { return (sizeof(a)/sizeof(a[0])); } main() { int a[3]={1,2,3}; printf("%d\n",S izeOfArray(a)); }
[..]
the function. (should be &a or *a or something... ??)
Actually, SizeOfArray () returns the number of elements
of the array. The above function would not work.
I would write the function as below, though, I would
prefer a macro to function:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
size_t
len_of_array ( int (*a)[MAX] )
{
printf ( "Array size: %lu\n", sizeof ( *a ) );
return sizeof *a / sizeof **a;
}
int
main ( void )
{
int a[MAX];
printf ( "Number of elements: %lu\n", len_of_array ( &a ) );
return EXIT_SUCCESS;
}
--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Vijay Kumar R Zanvar wrote: #include <stdio.h> #include <stdlib.h>
#define MAX 10
size_t len_of_array ( int (*a)[MAX] ) { printf ( "Array size: %lu\n", sizeof ( *a ) ); return sizeof *a / sizeof **a; }
This is equivalent to:
size_t len_of_array(vo id) { return MAX; }
It might be useful to define a macro:
#define NELEMS(x) (sizeof x/sizeof x[0])
and use that instead.
-nrk.
int main ( void ) { int a[MAX]; printf ( "Number of elements: %lu\n", len_of_array ( &a ) ); return EXIT_SUCCESS; }
--
Remove devnull for email
"Mark A. Odell" <no****@embedde dfw.com> wrote in message news:<Xn******* *************** **********@130. 133.1.4>... ru****@sohu.com (sugaray) wrote in news:ad******** *************** ***@posting.goo gle.com:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below:
int SizeOfArray(int a[]) { return (sizeof(a)/sizeof(a[0]));
Bang! You have tricked yourself. Although you think you are passing an array into SizeOfArray() you are actually passing a pointer which on 32-bit platforms is often only 4 bytes in size. You can (1) pass in the size, (2)package the array inside a struct, or (3) simply USE THE C sizeof OPERATOR!
or 4) implement the functionality as a macro. Use of function
macros is always to be done with some hesitation, but this
one seems OK:
#define N_ELTS(a) ((int)(sizeof(a )/sizeof(a[0])))
I put in the cast to emulate his function semantics, as he wants
to use the value as an int, not a size_t. Of course, this macro
only works if the array definition is in scope, otherwise
one of the solutions Mark proposed seems needed. In real life,
packaging the array and its size into a struct keeps all the info
in one place and that is nice... Hmmm, not having a C99 standard
at hand, what happens if this macro is applied to a VLA?
sizeof evaluated at runtime? error?
-David
Groovy hepcat sugaray was jivin' on 22 Jan 2004 05:58:12 -0800 in
comp.lang.c.
Problem with calculating the size of an array's a cool scene! Dig it! Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below:
int SizeOfArray(int a[]) { return (sizeof(a)/sizeof(a[0]));
Why didn't you pay attention when you read the FAQ? I know you did
read it, because it is a requirement before posting to the newsgroup.
Noone wants to be thought of as an idiot, so you must have read the
FAQ. However, you obviously missed question 6.21, because you surely
would have asked for clarification had you read it and misunderstood.
So, the conclusion I'm drawing is that you just weren't paying
attention at all. You simply couldn't be bothered. Well, why should we
bother to help you, then?
If you want our help, you're going to have to prove it. You're going
to have to show us that you are looking for answers and are willing to
learn. Start by going to the FAQ's web site, http://www.eskimo.com/~scs/C-faq/top.html, and reading it through
Then, read at least a month worth of articles in this newsgroup. Make
sure you read the article entitled "Welcome to comp.lang.c!" by James
Hu.
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: kie |
last post by:
hello,
i have a table that creates and deletes rows dynamically using
createElement, appendChild, removeChild.
when i have added the required amount of rows and input my data, i
would like to calculate the totals in each row.
when i try however, i receive the error:
"Error: 'elements' is null or not an object"
|
by: PHil Coveney |
last post by:
Hello,
I am having difficulty marshalling structures when calling DeviceIoControl.
I am importing this Win32 function as
static extern int DeviceIoControl (int hDevice,
int dwIoControlCode,
ref int lpInBuffer,
|
by: dbuser |
last post by:
Hi,
I need help on a problem, as described below. I am reading a file
"input.txt"which has data like this:
abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and writing into a dynamic
array. My problem is that the array shows extra z and probably because
of this further processing gives run time error in borland...
|
by: Peter Afonin |
last post by:
Hello,
I'm struggling with the string conversion to MD5 which I've never user
before.
I have a string that I need to encode which looks approximately like this:
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Email=petera_gudzon.net:lang
=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:shp_Price=2.95:shp_HostPlan=
BU:shp_Term=2"
|
by: Ray Z |
last post by:
I would like to have someone to help me out with my program.
Users are able to add items if they wish for the Monthly Claims Charges System.
I did not manage to do the calculation part of the program.
Could someone help me? :) Here is the program:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
...
| |
by: Wilson |
last post by:
i am learning to program using c++ and was set a task of making a
simple encryption algorithim. I choose to start with one where simply
each letter is replaced with its equivilent in the alphabet when
written backmards, i am hoping to make this more complex soon. Below
is the program i wrote but it does not work, it simply returns the
exact...
|
by: Nepomuk |
last post by:
Hi!
I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but not in others. Here's what I mean:#include <iostream>
int length(int * array){return sizeof(array)/sizeof(array);}
int main()
{
int array1 =...
|
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= |
last post by:
Say I have a class like,
class Sample
{
public decimal first = 10;
public decimal second = 20;
}
I have initialized it
|
by: edsunder |
last post by:
I'm making a "wrapper" for an unmanaged DLL (written, I'm fairly certain in C). I have a c++ "wrapper" that I'm trying to convert to VB.net. I've got most of the program working, but I've hit a brick wall.
I'm trying to convert the following two structures from their c++ versions to their vb.net versions. I cannot be sure which is the problem...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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...
| |
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. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
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...
|
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
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |