Sponsored Content
Top Forums Programming Passing multiple values from a function in C Post 302510345 by shoaibjameel123 on Sunday 3rd of April 2011 05:38:24 AM
Old 04-03-2011
Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this:
Code:
char **read_file ( char * , unsigned long int * );//this is the function prototype

Code:
unsigned long int number_of_words = 0;//variable defined in main() and initialized to 0

Code:
words_from_dictionary = read_file ( "dictionary.dit" , &number_of_words );//function call in main()

The function definition that I write later on, outside of main().

Code:
char **read_file ( char *path , unsigned long int *number_of_words )
{
    char ch;
    char *line = NULL;
    
    unsigned long int len = 0;
    unsigned long int read;
    
    ( * number_of_words ) = 0;//the updated value of this will be returned
    unsigned long int i = 0;
    unsigned long int j = 0;

    FILE *pointer = NULL;
    char **word_array = NULL;

    pointer = fopen ( path , "r" );//read some file from a path
    if ( pointer == NULL )
    {
        perror ( "File read error: read file" );
    }

    while ( !feof ( pointer ) )
    {
        ch = fgetc ( pointer );
        if ( ch == '\n' && ch != EOF )
        {
            ( * number_of_words ) ++;//counting the number of words in the file, this value will be returned to the main() along with another string array.
        }
    }

    rewind ( pointer );

    word_array = malloc ( ( * number_of_words ) * sizeof ( char * )
);
    if ( word_array == NULL )
    {
        perror ( "malloc() memory allocation failure" );
    }

    for ( i = 0 ; i < ( * number_of_words ) ; i ++ )
    {
        word_array [ i ] = malloc ( 100 * sizeof ( char ) );
        if ( word_array [ i ] == NULL )
        {
            perror ( "malloc() memory allocation failure" );
        }
    }

    i = 0;
    j = 0;

    while ( !feof ( pointer ) )
    {
        while ( ( read = getline ( &line, &len , pointer ) ) != - 1 )
        {
            strcpy ( word_array [ i ] , line );
            if ( i <= ( * number_of_words ) )
            {
                i ++;
            }
        }
    }

    fclose ( pointer );
    return ( word_array );//Returning this array of string
}

I can see that the calling function in main() can read both the number of words and the string array easily.

Now my question is:
If I now have something like this. These variables are defined in main()
Code:
 unsigned long int *word_position = NULL;
    unsigned int *document_id = NULL;
    long double *score = NULL;

The calling function in main is like this:

Code:
read_score_file ( word_position , document_id , score );

Now later I have a function like this:

Code:
void read_score_file ( unsigned long int *word_position , unsigned int *document_id , long double *score )
{
    FILE *score_file = NULL;

    unsigned int i = 0;

    score_file = fopen ( "scores.sce" , "r" );
    if ( score_file == NULL )
    {
        fprintf ( stderr , "file open error: score:file\n" );
    }

    word_position = ( unsigned long int * ) malloc ( TOTAL_WORD_IN_DICS * sizeof ( unsigned long int ) );
    if ( word_position == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure" );
    }

    document_id = ( unsigned int * ) malloc ( TOTAL_WORD_IN_DICS * sizeof ( unsigned int ) );
    if ( document_id == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure" );
    }

    score = ( long double * ) malloc ( TOTAL_WORD_IN_DICS * sizeof ( long double ) );
    if ( score == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure" );
    }

    while ( !feof ( score_file ) )
    {
        int32_t return_value = 0;

        return_value = fscanf ( score_file , "%ld %u %Lg\n" , ( word_position + i ) , ( document_id + i ) , ( score + i ) );
        if ( return_value == -1 )
        {
            break;
        }
        i ++;
    }
}

How can I return all the three variables (address of the first value) word_position, document_id and score to main() so that the parameters in
Code:
    read_score_file ( word_position , document_id , score );

in main() have the starting addresses of all the three variables? As of now they are getting NULL from read_score_file() function as return value. This simply means that I want read_score_file() to return multiple values. I am doing my programming in Linux and using gcc-4.1.2.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing string from function with '*'

Hi I have a shell function which returns string(ksh). The string is an sql statement. This statement can have '*' in its content (i.e. select 100 / 2 *100 from dual). When this happens ret_str will have contents of current directry I run the script from build in sql. Is there any way to fix it... (2 Replies)
Discussion started by: zam
2 Replies

2. UNIX for Dummies Questions & Answers

scripting: multiple values from file passing to command

one of my colleagues has this question. he has a command, C_CMD which accepts 4 variables, $1 $2 $3 $4 he wants to load up a file with multiple rows, one row per set of variables and then iteratively execute the command based on the content of the file. example: at the command line you'd... (5 Replies)
Discussion started by: LisaS
5 Replies

3. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

4. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

5. Shell Programming and Scripting

passing values to function in Ksh

Hi, I'm trying to work on the script given below #!/bin/ksh -x pfile() { echo "$1" } touch smp19 echo "Hi" > smp19 result=$(pfile $smp19) echo $result As highlighted , when i pass $smp19 as parameter ,it does not display the output.However when i try giving "Hi" instead... (2 Replies)
Discussion started by: Sheema
2 Replies

6. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

7. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

8. Shell Programming and Scripting

Passing multiple column values to UNIX variable

sqlplus -s $USER_ID@$SID/$PWD<<EOF>sql_1.txt set feedback off set heading off select 114032 as c_1 from dual ; EOF for i in `cat sql_1.txt` do sh script_1.sh $i Currently i am passing one column value to the single unix variable. How can i pass the values from 2... (2 Replies)
Discussion started by: rafa_fed2
2 Replies

9. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

10. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies
All times are GMT -4. The time now is 01:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy