Sponsored Content
Full Discussion: Integer array length
Top Forums Programming Integer array length Post 302851285 by yifangt on Sunday 8th of September 2013 02:53:42 AM
Old 09-08-2013
Integer array length

Hello;
When I wrote a function to print out an array,
Code:
void p_array(int arr[])
 {
   int i;
    int size = sizeof(arr) / sizeof(int);
 //    int size = sizeof (arr) / sizeof (arr[0]);
     for (i = 0; i < size; i++)
         printf("%d ", arr[i]);
     printf("\n");
 }

I could only print out the first two elements,( sometimes 8 out of 10 elements). What did I miss?
The problem seems to be the part
Code:
size = sizeof arr/sizeof(int);
size = sizeof arr /sizeof arr[0];

My understanding is (copied from stackoverflow):
sizeof returns the size in memory of the passed element. By dividing the size of an array by a single element size, you get the elements count. On the other hand, when counting elements in an array: sizeof(a) / sizeof(a[0]) will still work as well.
But my code does not! I saw this type online quite lot, but mine just does not work (Ubuntu 12.10, 64_bits kernel). Could anyone explain what is wrong with my code? Thanks a lot!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
CURRENT(3)								 1								CURRENT(3)

current - Return the current element in an array

SYNOPSIS
mixed current (array &$array) DESCRIPTION
Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array. PARAMETERS
o $array - The array. RETURN VALUES
The current(3) function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, current(3) returns FALSE. Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. EXAMPLES
Example #1 Example use of current(3) and friends <?php $transport = array('foot', 'bike', 'car', 'plane'); $mode = current($transport); // $mode = 'foot'; $mode = next($transport); // $mode = 'bike'; $mode = current($transport); // $mode = 'bike'; $mode = prev($transport); // $mode = 'foot'; $mode = end($transport); // $mode = 'plane'; $mode = current($transport); // $mode = 'plane'; $arr = array(); var_dump(current($arr)); // bool(false) $arr = array(array()); var_dump(current($arr)); // array(0) { } ?> NOTES
Note You won't be able to distinguish the end of an array from a boolean FALSE element. To properly traverse an array which may contain FALSE elements, see the each(3) function. SEE ALSO
end(3), key(3), each(3), prev(3), reset(3), next(3). PHP Documentation Group CURRENT(3)
All times are GMT -4. The time now is 03:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy