Integer array length


 
Thread Tools Search this Thread
Top Forums Programming Integer array length
# 15  
Old 09-09-2013
Two things still unclear to me:
1) Why the two exact formulas in main() and in showarray_1() get two different number 10 vs 2? That's the part bugs me very much.
2) In your showarray_2(int array[], int len), the local variable len is not calculated. In the main() call, len was called from the main(), that's I want to avoid in the cases when the array length is unknown. I can't provide an example yet.....maybe I am wrong!
It seems I really need read more to get thru this point.
Thank you so much Scott!
# 16  
Old 09-09-2013
Try this:

Code:
int arr[32];
int *parr=arr;

printf("sizeof(arr)=%d\n", sizeof(arr));
printf("sizeof(parr)=%d\n", sizeof(parr));

The only time the compiler knows the exact size of the array is when you use it directly. If you pass it around, it becomes a pointer to something of unknown size.

It only knows when you use it directly, because sizeof() is hardcoded. Its value is fixed in stone before the program even runs. You cannot use it in a dynamic way.
These 2 Users Gave Thanks to Corona688 For This Post:
# 17  
Old 09-09-2013
the compiler knows the exact size of the array is when you use it directly. Now I got it! Thanks a lot, and thank you, too, Scott!
# 18  
Old 09-09-2013
I'll try.

When a function is called the argument( parameters) for the function are pushed onto the stack. When C was first developed, memory space, and therefore also available stack space, were limited.

When a function is called it is more efficient to pass as a parameter the address of an object than the whole object. It is also faster. They decided that since arrays could be arbitrarily large, it was better to pass the address of the array.

In a 32 bit environment addresses are 32 bits long. int datatypes are usually 32 bit as well. You are confusing:

1. the address of your array with the physical memory object of the array itself:
The address is 32 bits, each element of the array is 32 bits because they are the
the int datatype.
2. There is nothing in the address of an array or the pointer to the array to tell the
program how big the array object is.
3. Because pointers are 32 bits you can print them just like an integer, using "%d".
Which is wrong. To print a pointer use "%p".
4. sizeof() gives you the number of bytes of a datatype, all 32 bit pointers are 4 bytes.


Code:
void foo(int *arr)
{
    printf("%d ", arr); //WRONG
    printf("%p\n", arr); // correct
    printf("%u %u %u\n", sizeof(arr), sizeof(int *), sizeof(int)); // ALL are the same size
}

int main()
{
    int arr[]={1,2,3,4,5,6,7,8,9,10};
    foo(arr);
    printf("size of arr = %u\n", sizeof(arr));
    return 0;
}

This User Gave Thanks to jim mcnamara For This Post:
# 19  
Old 09-09-2013
As I haven't done any C programming for about 15 years, I'm glad you guys showed up to add better explanations to the matter.

Thanks Smilie

Last edited by Scott; 09-09-2013 at 02:00 PM.. Reason: Actually it's only 15 years!
# 20  
Old 09-09-2013
No worry! I should have dove into C 15 years ago, and now I realize C is so important for programming!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Array length: ls and sort

Hi there, I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird. files_info="$(find $input_dir -name "*_CHR$i.info" | sort )" printf ${#files_info}"\n" #print length #--loop through... (6 Replies)
Discussion started by: genome
6 Replies

2. Shell Programming and Scripting

How to find length of multidimension array ???

Does anyone know how to find length of multi dimension array of following type A Afor simple array I is to do for (i in A)n++ to find length of array but if it is multi dimension how to find the length ? (2 Replies)
Discussion started by: nex_asp
2 Replies

3. Shell Programming and Scripting

Array Length Reports as Having Length when it is Empty?

Hello All, I have this script that does stuff like "starting, stopping & restarting" a Daemon Process running on my machine... My main question is why in part of my code (which you will see below) does the Array Length (i.e. ${#PIDS} ) return "1" when I know the Array is empty..? Here is... (17 Replies)
Discussion started by: mrm5102
17 Replies

4. Shell Programming and Scripting

Longest length of string in array

I would be grateful if someone could help me. I am trying to write a .sh script in UNIX. I have the following code; User=john User=james User=ian User=martin for x in ${User} do print ${#x} done This produces the following output; 4 5 3 6 (12 Replies)
Discussion started by: mmab
12 Replies

5. Shell Programming and Scripting

MAWK does not support length(array)?

As Brendan O'Conner writes in this blog, mawk is near 8 times faster than gawk, so I am going to give mawk a go, but I got errors when trying to print the length of an array in mawk using length() function, is it not supported in mawk? or there's another way to get the length of an array in mawk? ... (3 Replies)
Discussion started by: kevintse
3 Replies

6. Shell Programming and Scripting

Floating point to integer in variable length lines

Hi ! I'm looking for a way to transform certain floating point numbers in a one-line, variable length file to integers. I can do this in a crude way with sed : sed -e 's/0\.\(\):/\1:/g' -e 's/0\.0\(\):/\1:/g' -e 's/1\.000:/100:/g' myfile ... but this doesn't handle the rounding correctly. ... (3 Replies)
Discussion started by: jossojjos
3 Replies

7. Shell Programming and Scripting

Variable Sized Array Length Question

I need to implement the following logic and need some expert help from UNIX community. These are the steps in my Shell script. 1. Analyze a file. 2. Extract all the ID's in that file. 3. Use the ID's from #2 to run another filter on the file. I've implemented # 1 and 2 using... (3 Replies)
Discussion started by: katwala
3 Replies

8. UNIX for Advanced & Expert Users

converting openssl hex dump or PEM format to integer array

Hello. I'm working on a project that involves creating public/private keys server-side using openssl and using the public key in a Javascript application to encrypt sensitive data in form fields before transmission to the server. Using an SSL https server connection was not an option in this... (1 Reply)
Discussion started by: jhopper
1 Replies

9. Shell Programming and Scripting

Array length in PERL

Hi experts, How to get the length of an Array in PERL. for eg., @Var having 5 elements. regards Anent (5 Replies)
Discussion started by: anent
5 Replies

10. Programming

Function to return an array of integer

Hi all, I am trying to create a function that return an array of integer based on the char parameter pass into the function. I.e. func_a(char * str) { example str is equal to "1,2,3,4" return an array of integers of 1,2,3,4 } Please advise regards dwgi32 (2 Replies)
Discussion started by: dwgi32
2 Replies
Login or Register to Ask a Question