473,385 Members | 2,004 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.

About the retrogression problem when passing a array variable into a function which takes a pointer as its argument.

Hi, folks,

As the Subject suggests, array variable will retrogress as the
parameter of the function actually taking pointer as its argument,
like this:

int f(int* i) {
cout << sizeof(i) << endl;

return 0;
}

main() {
int a[100];
int *p = new int(1);

f(a);
f(i);
}

The output will be:
4
4
It's intuitive and straightforward, let's think another problem:

typedef int IntArray[100];
int g(IntArray& ia) {
cout << sizeof(ia) << endl;
}

main() {
IntArray ia;
g(ia);
}

The output is:
400

Can anybody explain what's going on here, a lot of thanks will go to
you.

Jul 26 '07 #1
4 1217
On Jul 26, 10:13 am, <leomayleo...@gmail.comwrote:
typedef int IntArray[100];
int g(IntArray& ia) {
cout << sizeof(ia) << endl;
}

The output is:
400

Can anybody explain what's going on here, a lot of thanks will go to
you.
Isn't it obvious? You're seeing 400 which is 100 * sizeof(int)...?
In other words, the full type information (including array dimension)
is preserved when ia is a reference.

Tony

Jul 26 '07 #2
api
On Jul 26, 9:13 am, <leomayleo...@gmail.comwrote:
Hi, folks,

As the Subject suggests, array variable will retrogress as the
parameter of the function actually taking pointer as its argument,
like this:

int f(int* i) {
cout << sizeof(i) << endl;

return 0;

}

main() {
int a[100];
int *p = new int(1);

f(a);
f(i);

}

The output will be:
4
4
It's intuitive and straightforward, let's think another problem:

typedef int IntArray[100];
int g(IntArray& ia) {
cout << sizeof(ia) << endl;

}

main() {
IntArray ia;
g(ia);

}

The output is:
400

Can anybody explain what's going on here, a lot of thanks will go to
you.
sizeof a pointer always return 4.
Passing reference as parameters, however, contains the full type
information

Jul 26 '07 #3
api wrote:
>
sizeof a pointer always return 4.
Only on a system where the size of a pointer is 4...
Passing reference as parameters, however, contains the full type
information
If the compiler knows the type of a variable, parameter or not, it will
know the size.

--
Ian Collins.
Jul 26 '07 #4
?? wrote:
:: Hi, folks,
::
:: As the Subject suggests, array variable will retrogress as the
:: parameter of the function actually taking pointer as its argument,
:: like this:
::
:: int f(int* i) {
:: cout << sizeof(i) << endl;
::
:: return 0;
:: }
::
:: main() {
:: int a[100];
:: int *p = new int(1);
::
:: f(a);
:: f(i);
:: }
::
:: The output will be:
:: 4
:: 4
:: It's intuitive and straightforward, let's think another problem:
::
:: typedef int IntArray[100];
:: int g(IntArray& ia) {
:: cout << sizeof(ia) << endl;
:: }
::
:: main() {
:: IntArray ia;
:: g(ia);
:: }
::
:: The output is:
:: 400
::
:: Can anybody explain what's going on here, a lot of thanks will go
:: to you.

Sure, a pointer and a reference is different. That's why we have both!
:-)

To pass by reference, you don't even need the typedef, you can do just

void g(int (&ia)[100]);

to get an equivalent function. Are you surprised that this one knows
the size of its parameter?
So, a reference is an alias for the real object bound to it. A pointer
is just an address!
Bo Persson


Jul 26 '07 #5

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
17
by: BlindHorse | last post by:
Help!!! I need someone to tell me why I am getting the err msg error C2440: '=' : cannot convert from 'char *' to 'char' //==================== #include <iostream>
34
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK"...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
17
by: kj | last post by:
How can one test if a pointer has been "freed" (i.e. with free())? My naive assumption was that such a pointer would equal NULL, but not so. Thanks, kj -- NOTE: In my address everything...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
3
by: Ken Varn | last post by:
I have a managed C++ function that accepts an IntPtr argument. I am passing in a variable of type HANDLE for the IntPtr argument. The compiler does not issue any warnings for this, so I am...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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,...

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.