Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-11-04 05:10:18
Point:500 Replies:23 POST_ID:828776USER_ID:11059
Topic:
C++ Programming Language;C Programming Language;Linux
I just tried one simple example of C/C++ program on 64-bit Linux in which just
want to find out of number of cells in one array.
But I get different result from main() and fn() function. Why ?
Could you help this ?
As I know, one double is 8-byte and my array cells are 5 so
sizeof(array) should be 40 . So the result,5, from main() is correct
but the result from fn() is 1 , Why ?
40/8-->5 in main() but 8/8-->1 in fn(), Why ?
Is it * str_numbers in fn() incorrect declaration ?
Please advise
Duncan
want to find out of number of cells in one array.
But I get different result from main() and fn() function. Why ?
Could you help this ?
As I know, one double is 8-byte and my array cells are 5 so
sizeof(array) should be 40 . So the result,5, from main() is correct
but the result from fn() is 1 , Why ?
40/8-->5 in main() but 8/8-->1 in fn(), Why ?
Is it * str_numbers in fn() incorrect declaration ?
Please advise
Duncan
#include <stdio.h>#include<float.h>int fn(double * str_numbers){ int total = sizeof(str_numbers)/sizeof(str_numbers[0]);// printf("In f: %d", total); printf("size of whole:%d",sizeof(str_numbers)); printf("size of one cell:%d",sizeof(str_numbers[0])); return total;}int main (){ double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456}; int total = sizeof(str_numbers)/sizeof(str_numbers[0]); printf("size of whole:%d",sizeof(str_numbers)); printf("size of one cell:%d",sizeof(str_numbers[0])); printf ("No of cell In main: %d", total);printf("%s","===========================================");printf("No of cell in fn:%d", fn(str_numbers)); return 0;} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:
Author: duncanb7 replied at 2024-11-04 07:48:18
Thanks Kdo,
after add back stdio.h the warning is gone.
This thread is trying practise more C code before making decision on
replacing some huge some mathematical PHP code by C/C++ or
node.js on my server.
C and node.js in speed is much faster than PHP ,and PHP and Javascript code(node.js) is
much easier to use than C/C++.
Anyway, this might be next topic
Hava a nice day
Duncan
after add back stdio.h the warning is gone.
This thread is trying practise more C code before making decision on
replacing some huge some mathematical PHP code by C/C++ or
node.js on my server.
C and node.js in speed is much faster than PHP ,and PHP and Javascript code(node.js) is
much easier to use than C/C++.
Anyway, this might be next topic
Hava a nice day
Duncan
Expert: Zoppo replied at 2024-11-04 07:38:46
Hm - I guess a type-or signed/unsigned mismatch warning could occur at
> data.Size = sizeof(str_numbers)/sizeof(str_numbers[0]);
since ADD_t::Size is of type int (which most probably is a 32-bit signed type) while sizeof returns a size_t (in your case I guess it's an unsinged, 64-bit type).
So you should change ADD_t to look like this:
typedef struct ArrayDescriptorDouble
{
double *Address;
size_t Size;
} ADD_t;
If this doesn't help please post the exact warning ...
ZOPPO
> data.Size = sizeof(str_numbers)/sizeof(str_numbers[0]);
since ADD_t::Size is of type int (which most probably is a 32-bit signed type) while sizeof returns a size_t (in your case I guess it's an unsinged, 64-bit type).
So you should change ADD_t to look like this:
typedef struct ArrayDescriptorDouble
{
double *Address;
size_t Size;
} ADD_t;
If this doesn't help please post the exact warning ...
ZOPPO
Expert: Kdo replied at 2024-11-04 07:33:54
Make sure that the program includes:
#include <stdio.h>
at the top of the source code.
Kent
#include <stdio.h>
at the top of the source code.
Kent
Author: duncanb7 replied at 2024-11-04 07:29:08
Thanks for your suggestion and final solution
If possible, please also answer the warning issue after complier zoppo's code
Duncan
If possible, please also answer the warning issue after complier zoppo's code
Duncan
Author: duncanb7 replied at 2024-11-04 07:27:35
dear zoppo,
your code is working and result are all 5 and only one array arg pass into fn()
that is what I want. I don't want to pass two argv into function() as Kda did but
his idea suggestion also help to finish this thread.
Last question, since I am not really fimilar with C,
when I complier the following code, that will give me
warning such as
COuld I avoid this warning ?
your code is working and result are all 5 and only one array arg pass into fn()
that is what I want. I don't want to pass two argv into function() as Kda did but
his idea suggestion also help to finish this thread.
Last question, since I am not really fimilar with C,
when I complier the following code, that will give me
warning such as
10:warning:incompatible implict declaration of bulit-in function "printf" in
18:warning:incompatible implict declaration of bulit-in function "printf" in
18:warning:incompatible implict declaration of bulit-in function "printf" in
COuld I avoid this warning ?
typedef struct ArrayDescriptorDouble{ double *Address; int Size;} ADD_t;int fn(ADD_t* pData){ int total = pData->Size; printf("size of whole:%d",total); return total;}int main (){ double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456}; int total = sizeof(str_numbers)/sizeof(str_numbers[0]); printf("size of whole:%d",sizeof(str_numbers)); printf("size of one cell:%d",sizeof(str_numbers[0])); printf ("No of cell In main: %d", total); printf("%s","==========================================="); ADD_t data; data.Address = str_numbers; data.Size = sizeof(str_numbers)/sizeof(str_numbers[0]); printf("No of cell in fn:%d", fn( &data )); return 0;} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:
Expert: Kdo replied at 2024-11-04 07:17:46
Adding a structure definition doesn't really help unless you use it in the program. :)
In this case, you probably just want to modify the function *fn* and pass the length.
In this case, you probably just want to modify the function *fn* and pass the length.
#include <stdio.h>#include<float.h>int fn(double str_numbers[], int size){ int total = size * sizeof(str_numbers[0]); printf("In f: %d", total); printf("size of whole:%d", size); printf("size of one cell:%d",sizeof(str_numbers[0])); return total;}int main (){ double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456};// int total = sizeof(str_numbers)/sizeof(str_numbers[0]); printf("size of whole:%d",sizeof(str_numbers)); printf("size of one cell:%d",sizeof(str_numbers[0])); printf ("No of cell In main: %d", total);printf("%s","===========================================");printf("No of cell in fn:%d", fn (str_numbers, sizeof (str_numbers)));return 0;} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:
Accepted Solution
Expert: Zoppo replied at 2024-11-04 07:16:40
250 points EXCELLENT
Well, if you get more familiar with C/C++ you'll see sizeof in a function leads to expected results too ;o)
BTW, to avoid confusion I would suggest to avoid [] in function declarations, it is absoluteley the same as passing a pointer like in void fn ( double* a );.
And: You can't really compare C/C++ with PHP or Javascript. These are scripting languages with dynamic type system which internally use a lot of functionality to hide such details form the user, in C/C++ the user has to implement such things on a quite low-level base.
To do it like Kdo suggested you need to even use the declared structure, i.e.:
typedef struct ArrayDescriptorDouble
{
double *Address;
int Size;
} ADD_t;
int fn(ADD_t* pData)
{
int total = pData->Size;
printf("size of whole:%d",total);
return total;
}
int main ()
{
double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456};
int total = sizeof(str_numbers)/sizeof(str_numbers[0]);
printf("size of whole:%d",sizeof(str_numbers));
printf("size of one cell:%d",sizeof(str_numbers[0]));
printf ("No of cell In main: %d", total);
printf("%s","===========================================");
ADD_t data;
data.Address = str_numbers;
data.Size = sizeof(str_numbers)/sizeof(str_numbers[0]);
printf("No of cell in fn:%d", fn( &data ));
return 0;
}
ZOPPO
BTW, to avoid confusion I would suggest to avoid [] in function declarations, it is absoluteley the same as passing a pointer like in void fn ( double* a );.
And: You can't really compare C/C++ with PHP or Javascript. These are scripting languages with dynamic type system which internally use a lot of functionality to hide such details form the user, in C/C++ the user has to implement such things on a quite low-level base.
To do it like Kdo suggested you need to even use the declared structure, i.e.:
typedef struct ArrayDescriptorDouble
{
double *Address;
int Size;
} ADD_t;
int fn(ADD_t* pData)
{
int total = pData->Size;
printf("size of whole:%d",total);
return total;
}
int main ()
{
double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456};
int total = sizeof(str_numbers)/sizeof(str_numbers[0]);
printf("size of whole:%d",sizeof(str_numbers));
printf("size of one cell:%d",sizeof(str_numbers[0]));
printf ("No of cell In main: %d", total);
printf("%s","===========================================");
ADD_t data;
data.Address = str_numbers;
data.Size = sizeof(str_numbers)/sizeof(str_numbers[0]);
printf("No of cell in fn:%d", fn( &data ));
return 0;
}
ZOPPO
Author: duncanb7 replied at 2024-11-04 07:10:59
I tried yours but the result is same as before, Could you send me you complete code
for us ?
for us ?
#include <stdio.h>#include<float.h>typedef struct ArrayDescriptorDouble{ double *Address; int Size;} ADD_t;int fn(double str_numbers[]){ int total = sizeof(str_numbers)/sizeof(str_numbers[0]);// printf("In f: %d", total); printf("size of whole:%d",sizeof(str_numbers)); printf("size of one cell:%d",sizeof(str_numbers[0])); return total;}int main (){ double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456}; int total = sizeof(str_numbers)/sizeof(str_numbers[0]); printf("size of whole:%d",sizeof(str_numbers)); printf("size of one cell:%d",sizeof(str_numbers[0])); printf ("No of cell In main: %d", total);printf("%s","===========================================");printf("No of cell in fn:%d", fn(str_numbers)); return 0;} 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:
Expert: Kdo replied at 2024-11-04 07:07:54
No. If it's critical that the function knows the number of items in the array, you'll need to pass that as a separate parameter or use something like the structure container that I showed above.
Kent
Kent
Author: duncanb7 replied at 2024-11-04 07:06:30
Any now I know why like PHP and node.js at server-side more than C/C++
Author: duncanb7 replied at 2024-11-04 07:04:29
yes it can sizeof() in function but with the result we not expect so
we refer can't sizeof () in function if array is passed into it
Anyway, any other methods, we can
get the number of cells in array in function () when array is passed into function(0 ?
we refer can't sizeof () in function if array is passed into it
Anyway, any other methods, we can
get the number of cells in array in function () when array is passed into function(0 ?
Expert: Kdo replied at 2024-11-04 06:58:48
>> In other words, can't sizeof array in function
You CAN use sizeof() within a function. It returns the size of the object passed. What is passed is the address of the array and sizeof() dutifully returns 1, the number of addresses passed.
You CAN use sizeof() within a function. It returns the size of the object passed. What is passed is the address of the array and sizeof() dutifully returns 1, the number of addresses passed.
Author: duncanb7 replied at 2024-11-04 06:54:08
<link deleted because it violates site guidelines for off site content COBOLdinosaur, Topic Advisor>
So it is impossible to pass array into function for sizeof , I found a lot in google there
are a lot people experienced the same issue but not solved and just do sizeof and pass
result of sizeof into function
In other words, can't sizeof array in function ,
Arrays decay to pointers in function calls. It's not possible to compute the size of an array which is only represented as a pointer in any way, including using sizeof.
So it is impossible to pass array into function for sizeof , I found a lot in google there
are a lot people experienced the same issue but not solved and just do sizeof and pass
result of sizeof into function
In other words, can't sizeof array in function ,
Expert: Zoppo replied at 2024-11-04 06:49:17
Ok, maybe you should first think about the sizeof operator. You can read something about it (including samples similar to your problem here) i.e. at http://en.wikipedia.org/wiki/Sizeof
There you'll fine in most cases, sizeof is a compile-time operator, and that's exacly what's the cause for the difference between instantiating an array like
double a[] = { 1.0, 2.0, 3.0 };
int s = sizeof( a ); // will be 3*sizeof(double)
and passing an array like
void fn( double a[] )
{
int s = sizeof( a ); // will be sizeof( double* )
This is because only in the first case the compiler can determine the size of the array at compile time, for the second case there's no way to determine the size of the array, neither ar compile time nor at runtime.
ZOPPO
There you'll fine in most cases, sizeof is a compile-time operator, and that's exacly what's the cause for the difference between instantiating an array like
double a[] = { 1.0, 2.0, 3.0 };
int s = sizeof( a ); // will be 3*sizeof(double)
and passing an array like
void fn( double a[] )
{
int s = sizeof( a ); // will be sizeof( double* )
This is because only in the first case the compiler can determine the size of the array at compile time, for the second case there's no way to determine the size of the array, neither ar compile time nor at runtime.
ZOPPO
Expert: Kdo replied at 2024-11-04 06:45:27
Hi Duncan,
The short answer is because the called function doesn't know the size of the array. The C convention is that because the called function doesn't know, 1 is assumed. Both Zoppo and I have been trying to explain why that is instead of giving a 1 sentence answer that amounts to "because that's the way it is".
Kent
The short answer is because the called function doesn't know the size of the array. The C convention is that because the called function doesn't know, 1 is assumed. Both Zoppo and I have been trying to explain why that is instead of giving a 1 sentence answer that amounts to "because that's the way it is".
Kent
Author: duncanb7 replied at 2024-11-04 06:34:04
dear zoppo,
Please review my thread question. I want to know why
the result is different between the code at main() and fn() even
the coding is same beside pass array into function.
The target I want to the number of array cells can be found in function fn()
instead of main();
Why such simple task can cost a lot of time ?
Please review my thread question. I want to know why
the result is different between the code at main() and fn() even
the coding is same beside pass array into function.
The target I want to the number of array cells can be found in function fn()
instead of main();
Why such simple task can cost a lot of time ?
Assisted Solution
Expert: Kdo replied at 2024-11-04 06:22:00
250 points EXCELLENT
As noted above, all that is passed is the starting address of the array. There is no mechanism in C to pass an address and have the called function know anything more than the address, and by implication, the nature of the structure defined in the function header. Length simply isn't passed.
You can "simulate" it with your own structure, but that's not the same thing.
typedef struct ArrayDescriptorDouble
{
double *Address;
int Size;
} ADD_t;
Then populate those two items and pass it (or it's address) to the function. But passing a double* or double[], or double[100] passes only the starting address. There is no guarantee or even implication of the length of the array.
Kent
You can "simulate" it with your own structure, but that's not the same thing.
typedef struct ArrayDescriptorDouble
{
double *Address;
int Size;
} ADD_t;
Then populate those two items and pass it (or it's address) to the function. But passing a double* or double[], or double[100] passes only the starting address. There is no guarantee or even implication of the length of the array.
Kent
Expert: Zoppo replied at 2024-11-04 06:19:45
Hi duncanb7,
as Kdo mentioned above regardless whether you pass a 'double*' or a 'double[]' just a pointer to the first element of the array is passed to the function so within the function there's no chance to determine the size of the array.
So the only way to achieve what you want is to add an additional parameter to pass the size, i.e.:
int fn(double * str_numbers, int total)
{
printf("size of whole:%d",sizeof(double)*total);
printf("size of one cell:%d",sizeof(str_numbers[0]));
return total;
}
int main ()
{
double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456};
int total = sizeof(str_numbers)/sizeof(str_numbers[0]);
printf("size of whole:%d",sizeof(str_numbers));
printf("size of one cell:%d",sizeof(str_numbers[0]));
printf ("No of cell In main: %d", total);
printf("%s","===========================================");
printf("No of cell in fn:%d", fn(str_numbers,total));
return 0;
}
ZOPPO
as Kdo mentioned above regardless whether you pass a 'double*' or a 'double[]' just a pointer to the first element of the array is passed to the function so within the function there's no chance to determine the size of the array.
So the only way to achieve what you want is to add an additional parameter to pass the size, i.e.:
int fn(double * str_numbers, int total)
{
printf("size of whole:%d",sizeof(double)*total);
printf("size of one cell:%d",sizeof(str_numbers[0]));
return total;
}
int main ()
{
double str_numbers[5] = {23000.678,23010.22,23800.345,22320.12,20345.456};
int total = sizeof(str_numbers)/sizeof(str_numbers[0]);
printf("size of whole:%d",sizeof(str_numbers));
printf("size of one cell:%d",sizeof(str_numbers[0]));
printf ("No of cell In main: %d", total);
printf("%s","===========================================");
printf("No of cell in fn:%d", fn(str_numbers,total));
return 0;
}
ZOPPO
Author: duncanb7 replied at 2024-11-04 06:14:29
int fn(double str_numbers[])
{
}
there is no complier error but the result of fn() is still 1 not 5, Why ?
{
}
there is no complier error but the result of fn() is still 1 not 5, Why ?
Author: duncanb7 replied at 2024-11-04 06:11:02
So how can I pass the array from str_number into the function that do the same
thing as in main ();
thing as in main ();
Expert: Kdo replied at 2024-11-04 05:46:16
Parameters are passed by the C putting something on the stack. (The stack is just a place in memory used to pass parameters and put the temporary variables used by the function.) In your last example, function fn expects a double to be passed to it. A single variable. When the parameter is defined as a double*, the address of the variable will be passed. When you think about passing a single variable, the difference is pretty subtle. But when you think about passing an array it makes more sense. You wouldn't want to put the entire array on the stack as the array could literally hold millions of items that would have to be copied to the stack and then back to the calling program. Passing the address allows the function to access exactly the elements of the array that it needs.
When you pass a single item, you can pass by address or by value (by using the * or not), but the function must also be coded with the correct syntax to match how the variable is passed.
Kent
When you pass a single item, you can pass by address or by value (by using the * or not), but the function must also be coded with the correct syntax to match how the variable is passed.
Kent
Author: duncanb7 replied at 2024-11-04 05:38:49
int fn(double str_numbers)
{
}
I tried it without * pointer but gave me the complier error, Why ?
{
}
I tried it without * pointer but gave me the complier error, Why ?
Expert: Kdo replied at 2024-11-04 05:34:05
Hi duncan,
All that's passed to fn is the address of the array. You can pass double*, double[], double[1000], etc from the calling function, but there's nothing in the address (or the passed object) that indicates the number of items in the array. All that's passed is the address..
Good Luck,
Kent
All that's passed to fn is the address of the array. You can pass double*, double[], double[1000], etc from the calling function, but there's nothing in the address (or the passed object) that indicates the number of items in the array. All that's passed is the address..
Good Luck,
Kent