I have this code:
void test(int* array)
{
int pp = sizeof(array);
cout << pp << endl;
}
int main(){
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;
}
In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).
But when I pass nums to test() and call sizeof again I only get 4.
Why do I get 12 in main and 4 in test()? 5 1972
desktop wrote:
I have this code:
void test(int* array)
{
int pp = sizeof(array);
cout << pp << endl;
}
int main(){
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;
}
In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).
But when I pass nums to test() and call sizeof again I only get 4.
Why do I get 12 in main and 4 in test()?
Because the sizeof(int*), which is the function parameter, is 4 on your
system. If you want the keep track of the size, you have to pass it, or
just use std::vector.
--
Ian Collins.
On Tue, 24 Apr 2007 13:20:08 +0200, desktop wrote:
I have this code:
void test(int* array)
{
int pp = sizeof(array);
cout << pp << endl;
}
int main(){
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;
}
In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).
Yes.
But when I pass nums to test() and call sizeof again I only get 4.
Yes, because although you've called your parameter to test() "array",
it's *not* an array - it's a pointer to int; and the size (in bytes) of a
pointer to int on your system is evidently 4.
Why do I get 12 in main and 4 in test()?
Because pointers are not the same as arrays in C++. Note that when you
pass in the array nums as an argument to test(), you are actually passing
in a pointer to the first element of nums (this conversion happens
implicitly); in fact the size of the array nums is simply not available
to the function test(). You would have to pass it in as another parameter.
--
Lionel B
"Ian Collins" <ia******@hotmail.comwrote in message
news:59*************@mid.individual.net...
desktop wrote:
>I have this code:
void test(int* array) { int pp = sizeof(array);
cout << pp << endl;
}
int main(){
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
int pp = sizeof(nums)/4; cout << pp << endl; test(nums); return 0;
}
In main I can take sizeof(nums)/4 which gives me 12 (correct number of elements in the array nums).
But when I pass nums to test() and call sizeof again I only get 4.
Why do I get 12 in main and 4 in test()?
Because the sizeof(int*), which is the function parameter, is 4 on your
system. If you want the keep track of the size, you have to pass it, or
just use std::vector.
Or use a template function:
template<size_t N>
void test(int (&array)[N])
{
cout << N << endl;
}
Of course, this limits the use of test() to only complete array types (no
incomplete array types, e.g., extern int bla[], and no pointers)
- Sylvester
Sylvester Hesp wrote:
>
Or use a template function:
template<size_t N>
void test(int (&array)[N])
{
cout << N << endl;
}
Of course, this limits the use of test() to only complete array types (no
incomplete array types, e.g., extern int bla[], and no pointers)
And it generates a different function for each array size that you call
it on.
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
On Apr 24, 7:20 am, desktop <f...@sss.comwrote:
I have this code:
void test(int* array)
{
int pp = sizeof(array);
cout << pp << endl;
}
int main(){
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
int pp = sizeof(nums)/4;
cout << pp << endl;
test(nums);
return 0;
}
In main I can take sizeof(nums)/4 which gives me 12 (correct number of
elements in the array nums).
But when I pass nums to test() and call sizeof again I only get 4.
Why do I get 12 in main and 4 in test()?
The function test() takes a pointer to a single integer.
Why should it do anything else?
Follow this example and pay attention to the types detected.
#include <iostream>
#include <typeinfo>
void test(int* p_n)
{
std::cout << "type of p_n is ";
std::cout << typeid(p_n).name();
std::cout << std::endl;
}
template< typename T, const size_t Size >
void pass_by_ref(T (& array)[Size])
{
std::cout << "type of array is ";
std::cout << typeid(array).name();
std::cout << std::endl;
}
int main()
{
int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
int sz = sizeof(nums)/sizeof(int);
std::cout << "sz = " << sz;
std::cout << std::endl;
std::cout << "type of sz is ";
std::cout << typeid(sz).name() << std::endl;
std::cout << "type of nums is ";
std::cout << typeid(nums).name();
std::cout << std::endl;
test(nums);
pass_by_ref(nums);
return 0;
}
/*
sz = 12
type of sz is i
type of nums is A12_i
type of p_n is Pi
type of array is A12_i
*/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: news.hku.hk |
last post by:
suppose i have a class abc, and i have an important value stored as
int integer = 888;
when i want to declare an array of objects:
abc obj; // error said non-constant....expect constant...
|
by: henrytcy |
last post by:
Hi,
How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?
Thanks!
|
by: Vijay |
last post by:
With the option strict On set.....
Dim fs As FileStream = File.OpenRead(strFile)
With fs
Dim buffer(fs.Length) As Byte ' <--- Option Strict On disallows implicit
conversions from 'Long' to...
|
by: redefined.horizons |
last post by:
I would like to have an array declaration where the size of the array
is dependent on a variable. Something like this:
/* Store the desired size of the array in a variable named
"array_size". */...
|
by: Frederick Gotham |
last post by:
On modern 32-Bit PC's, the following setup is common:
char: 8-Bit
short: 16-Bit
int: 32-Bit
long: 32-Bit
"char" is commonly used to store text characters.
"short" is commonly used to store...
|
by: kaczmar2 |
last post by:
I have code like so:
FileStream fs = new FileStream(locationTextBox.Text, FileMode.Open,
FileAccess.Write, FileShare.Read);
long size = fs.Length;
byte buffer = new byte;
// Get file to byte...
|
by: desktop |
last post by:
I have a function that takes two pointers to an array. The first point
to the first element while the other points to the last element.
int nums = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int...
|
by: yhlove |
last post by:
Hi
I declared an array as follows:
dim a as integer()
somewhere else in my program I have the following command:
redim preserve a(size)
I want to check the array length like that
if...
|
by: James Harris |
last post by:
Initial issue: read in an arbitrary-length piece of text.
Perceived issue: handle variable-length data
The code below is a suggestion for implementing a variable length
buffer that could be used...
|
by: Adam Chapman |
last post by:
Hi,
Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I've
realised that C can't determine the size of a 2d...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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: 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...
| |