LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Global Declaration (https://www.linuxquestions.org/questions/programming-9/c-global-declaration-367691/)

Aju 09-28-2005 12:33 AM

C Global Declaration
 
I have this small problem which i couldnt figure out.

i have a c program in which i have a function,say function1 which i call from the main thread.

From this function which i mentioned above i pass some value to some other function,say function2 and do some operation there,say display the value.

Note:i m not passing any value from main to function1.The value passed from function1 to function2 is declared in function1.

i found this strange,maybe due to my lack of expertise, when i pass a single variable say int i, and display it in function2 it works fine but if the same is done with an array of int's it displays a junk value.

But the above problem is solved if i declare the array of int's as global.

I cant figure out why this happens, do give me some suggestions in this regard.

addy86 09-28-2005 04:02 AM

Maybe a little code might help us :)

jtshaw 09-28-2005 08:49 AM

ok, so I'm sick, can't sleep, and bored, so I decided to write you a dumb little example of how things work.

Code:

#include <stdio.h>
#include <stdlib.h>


/* Prototypes */

void function1 ();
void function2 (int i, int *j, int **k);
void printiarray (const char * header, int *i, int size);


/* Implementation */
void function1 ()
{
        int index;
        int i = 5;
        int *j = malloc (sizeof(int) * 10);
        int *k = malloc (sizeof(int) * 5);

        for (index = 0; index < 10; index++) {
                j[index] = index+1;
        }
        for (index = 5; index > 0; index--) {
                k[5-index] = index;
        }

        printf("i from function 1 = %d\n",i);
        printiarray ("j from function 1 = ",j,10);
        printiarray ("k from function 1 = ",k,5);

        function2 (i,j,&k);

        printiarray ("k from function 1 after function2 was called = ",k,5);
        free(j);
        free(k);
}

void function2 (int i, int *j, int **k)
{
        int index;
        printf("i from function 2 = %d\n",i);
        printiarray ("j from function 2 = ",j,10);

        for (index = 0;index < 5; index++) {
                (*k)[index]+=20;
        }
}

void printiarray (const char * header, int *i, int size)
{
        int index;
        printf("%s ", header);
        for (index = 0; index < size; index++) {
                printf(" %d ",i[index]);
        }
        printf("\n");

/* Main Function */
int main ()
{
        function1();
        return 0;
}

Ok, what we have here is a call to function 1, which loads up an int, and two int arrays. The first int array is passed to function 2 so that it can access the data. The 2nd array is passed in so the data can be accessed and modified so that function 1 can see the modifications. The output of said stupid little program is:

Code:

johnshaw@Quaqmire-OSX ~/test $ gcc -Wall -o test2 test2.c
johnshaw@Quaqmire-OSX ~/test $ ./test2
i from function 1 = 5
j from function 1 =  1  2  3  4  5  6  7  8  9  10
k from function 1 =  5  4  3  2  1
i from function 2 = 5
j from function 2 =  1  2  3  4  5  6  7  8  9  10
k from function 1 after function2 was called =  25  24  23  22  21

I hope that helps a little to clear up how things are passed and how you can access/modify them once they are passed.... if you have any questions fire away.

Aju 09-29-2005 04:21 AM

Thanks jtshaw,i think that has cleared my doubt

Thank u addy,sorry for not posting the code snippet anyways the problem is solved


All times are GMT -5. The time now is 02:42 AM.