Integer array length


 
Thread Tools Search this Thread
Top Forums Programming Integer array length
# 8  
Old 09-09-2013
For the same reason. The two numbers displayed on each call of p_array() stem from the fact that a[] is an 8-byte pointer and sizeof(int) is 4 bytes, and 8/4 = 2.

So, it's displaying the first two elements of the array as you're sorting it in qsort().

Note that you passed len to qsort(). Why can't you pass it to q_array()?
# 9  
Old 09-09-2013
Thanks Scott!
Yes, I did pass len to p_array, but did not make any difference. Here my focus is about the calculation of the array length.
I could not understand why the calculation of array length in the main function is fine (line 10), but not in the p_array function (line 66). Thanks!
# 10  
Old 09-09-2013
In the main() function it's an array. In the p_array() function it's a pointer to the first element of the array.

I didn't see where you passed len to p_array(), or how it was used there, so I can't comment on it.
# 11  
Old 09-09-2013
Sorry I meant I tried passing len to the p_array function like this:
Code:
#include<stdio.h>
void qsort(int[], int, int);
void p_array(int[], int);

int main()
{
    int i;
    int array[] = { 55, 66, 32, 12, 9, 73, 2, 4, 37, 36 };
    int len = sizeof(array) / sizeof(int);
    for (i = 0; i < len; i++)
        printf("%d ", array[i]);
    printf("\n");
    p_array(array, len);
return 0;
}

void p_array(int arr[], int size)
{
    int i;
    size = sizeof(arr) / sizeof(int);
//    int size = sizeof (arr) / sizeof (arr[0]);
    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

output:
Code:
55 66 32 12 9 73 2 4 37 36 
55 66

I did not catch the part you said it is an array in the main() but pointer in the p_array(). Seems that's the part I missed. Thanks a lot!
# 12  
Old 09-09-2013
You still have this, which will override the passed variable:
Code:
    size = sizeof(arr) / sizeof(int);

Also, as you originally passed len - 1 to qsort(), you'll need to modify the for-loop in p_array to:
Code:
    for (i = 0; i <= size; i++)

This User Gave Thanks to Scott For This Post:
# 13  
Old 09-09-2013
Thanks again! That bug only make small difference. Output:
Code:
55 66 32 12 9 73 2 4 37 36 
55 66 32

I feel understand pointer and array in C (not fluent to use, though!). Could you explain why the two calculations differ?
Code:
int len = sizeof(array) / sizeof(int);  //10
int  size = sizeof(arr) / sizeof(int);   //2

Thanks.

Last edited by yifangt; 09-09-2013 at 01:09 PM..
# 14  
Old 09-09-2013
I'm not sure where you get 8 from - you'll have to show the code (in context) that produces that number, but passing the length works perfectly fine:
Code:
# cat main.c
#include <stdio.h>

int main( int argc, char **argv ) {
  int array[10] = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 };
  int i, len = sizeof( array ) / sizeof( int );
  printf( "In main, length of array: %d\n", len );
  printf( "In main, array elements:" );
  for( i = 0; i < len; i++ )
    printf( " %d", array[i] );

  printf( "\n" );
  printf( "\n" );

  showarr_1( array );
  showarr_2( array, 10 );

  return 0;
}
 
int showarr_1( int array[] ) {
  int len = sizeof( array ) / sizeof( int );
  printf( "In showarr_1, length of array: %d (bogus number!)\n", len );
  printf( "\n" );

  return 0;
}
 
int showarr_2( int array[], int len ) {
  int i;
  printf( "In showarr_2, length of array: %d\n", len );
  printf( "In showarr_2, array elements:" );
  for( i = 0; i < len; i++ )
    printf( " %d", array[i] );

  printf( "\n" );

  return 0;
}


# ./main
In main, length of array: 10
In main, array elements: 3 6 9 12 15 18 21 24 27 30

In showarr_1, length of array: 2 (bogus number!)

In showarr_2, length of array: 10
In showarr_2, array elements: 3 6 9 12 15 18 21 24 27 30

edit: you updated your post, to change 8 to 10. It should be clear now why the numbers are different. As previously said: one is an array, the other is a pointer (to the array).
This User Gave Thanks to Scott For This Post:
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