[ C ] multidemensional array pass to functions


 
Thread Tools Search this Thread
Top Forums Programming [ C ] multidemensional array pass to functions
# 1  
Old 12-05-2008
[ C ] multidemensional array pass to functions

Please excuse my ineptitude for a bit as I've been spoiled for the past few months with only writing perl code instead of C.

So ok, I've been thinking about some code to change the crc32 values that are held within central directory headers of zip files.

Because I'm lazy I decided to just hack up some archive repair tool to write the desired crc32.

What I figured I could do was make a multidimensional array of ints to pass to the function that will do all the messy work.


Here is the relevant code:

Code:
int crcSpoof( char* file, char* fileOut, char* fileOutTmp, ulong* nRecovered, ulong* bytesRecovered, int** CRC_Array )

...

int main( int argc, char** argv )
{
    int i = 3; // start with the 3rd value of argv
    int** crc_s; 
    
    // Some argument testing here

    while( argv[ i ] )
       crc_s = argv[ i ];
    
    crcSpoof( argv[1], argv[2], "fileOutTmp", NULL, NULL, crc_s );

I feel that this is probably ok but I really can't test it until I baseline some units around here.


Thanks!
# 2  
Old 12-06-2008
Most of the source code looks alright with the exception of one statement. Dual-level pointer crc_s cannot be equated to uni-level pointer argv[i] as they are incompatible in terms of the pointer levels. crc_s is of type char** while argv[i] is of type char*.
Quote:
Originally Posted by VRoemer
Code:
int crcSpoof( char* file, char* fileOut, char* fileOutTmp, ulong* nRecovered, ulong* bytesRecovered, int** CRC_Array )

...

int main( int argc, char** argv )
{
    int i = 3; // start with the 3rd value of argv
    int** crc_s; 
    
    // Some argument testing here

    while( argv[ i ] )
       crc_s = argv[ i ]; /* the compiler will complain about this line */ 
    
    crcSpoof( argv[1], argv[2], "fileOutTmp", NULL, NULL, crc_s );

# 3  
Old 12-06-2008
Ow yes that does look like an issue

Alright so here are my updates, I was also thinking that I need to change my command line ascii codes to actual integers like this.

Now I'm coming to think that this could perhaps lead to some memory issues, so I might also be obligated to throw in a *alloc.

Code:
int main( int argc, char** argv )
{
    int i = 3; // start with the 3rd value of argv
    int** crc_s; 
    
    // Some argument testing here

    while( argv[ i ] ) {
       crc_s[ i ] = malloc( sizeof(argv[i]) );
       if( crc_s[ i ] == NULL )
           exit( -1 );
       crc_s[ i ] = atoi( argv[ i ] ); // Perhaps this is a better way of doing it
    }

    crcSpoof( argv[1], argv[2], "fileOutTmp", NULL, NULL, crc_s );

Hopefully thats the proper way to allocate memory ( I have never really done much with allocating memory )

Last edited by VRoemer; 12-06-2008 at 06:19 PM..
# 4  
Old 12-08-2008
Well there is one major point in your code that I completely missed before. I mistakenly thought crc_s was of type char** when it actually is of type int**. Casting char** to int** won't work (most likely crash the program with a SIGBUS) as char** addresses are not guaranted to be aligned on a word boundary.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Code review: recursion in circular array, reduce two functions to one?

Hello, I think there's an easier way to do this but can't seem to recall but given an array of animals and an initial value is a random index in the array, here it's 3. 3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0... inifinite repeat a quick brute force solution i came up with was two functions, i... (6 Replies)
Discussion started by: f77hack
6 Replies

2. UNIX for Dummies Questions & Answers

How to pass first array value?

Hi, I am creating filesystem for block device, but I want to pass array value one by one acording to block device count. $tmp1 = block device count 3 $blockdevice = So I want to first pass sdb1 alone in loop, how to take only block device seprately from $blockdevice array. (1 Reply)
Discussion started by: stew
1 Replies

3. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

4. Shell Programming and Scripting

Need help on Assigning a Array variable from Background Functions

I have a question on how can I assign a output of a function to a variable which is executed in background. Here is my example $ cat sample_program.sh #!/bin/ksh exec_func () { sleep 1 v=`expr $1 + 100` print $v } export OUT_ARR date for i in 1 2 do OUT_ARR=`exec_func $i` &... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

5. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

6. Shell Programming and Scripting

Passing array to functions in ksh script

Let me know if there is a way to pass array to a funtion in ksh script. function isPresent { typeset member member=$1 dbList=$2 echo '$1:' $1 echo '$2' $dbList The array will be at the second position....something like this isPresent 12 <array> if then echo... (3 Replies)
Discussion started by: prasperl
3 Replies

7. UNIX for Dummies Questions & Answers

How to pass array as an arguement ?

Hi experts, I am here again with another Issue. I need to pass the array as parameter / argument to another script. I tried it as follows . ( I got this idea from the link ) $ cat test1.sh #! /usr/bin/ksh set -a arr1 echo "...In Test1...." arr1="APPS_DEV" arr1="TEST_DEV" echo... (16 Replies)
Discussion started by: rajavu
16 Replies

8. Shell Programming and Scripting

Tricky array substitution in functions

Hello, Please tell me if there is a better way to get the number of elements from an array that is passed to a function. This is what works on Solaris 8 (ksh) but it looks odd: loop_array() { array_name=$2 b1='\${\#' b2='}' nr_elements=`eval echo... (6 Replies)
Discussion started by: majormark
6 Replies

9. UNIX for Dummies Questions & Answers

How To Pass an Array Variable

Hi, I have a master BASH shell script where I define a bunch of variables: $var1=why $var2=is $var3=(this so hard) I would then like to call another shell script and pass these variables to it: $script2 $var1 $var2 $var3 This works fine for var1 and var2. However, var3 is an array,... (9 Replies)
Discussion started by: msb65
9 Replies

10. UNIX for Advanced & Expert Users

Pass by reference in shell functions

Hi all, In windows script, passing arguments can be done in both "call by value" and "call by reference". Refer http://www.commandline.co.uk/lib/treeview/index.php Can we have call by reference in unix too? Thanks in adavance, Sonal. (5 Replies)
Discussion started by: sonaluphale
5 Replies
Login or Register to Ask a Question