Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-28-2005, 01:33 AM   #1
LQ Newbie
Registered: Aug 2005
Location: ~
Distribution: Red Hat 9
Posts: 14

Rep: Reputation: 0
Question 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.
Old 09-28-2005, 05:02 AM   #2
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Maybe a little code might help us
Old 09-28-2005, 09:49 AM   #3
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

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

#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);

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++) {

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

/* Main Function */
int main ()
        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:

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.
Old 09-29-2005, 05:21 AM   #4
LQ Newbie
Registered: Aug 2005
Location: ~
Distribution: Red Hat 9
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks jtshaw,i think that has cleared my doubt

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
declaration does not declare anything tristanm Programming 5 10-24-2005 05:00 PM
char * declaration in c; alaios Programming 19 09-14-2005 10:50 PM
the c declaration kapsikum Programming 3 04-06-2005 03:12 AM
How to understand the declaration? Xiangbuilder Programming 8 09-13-2003 05:49 AM
Perl global declaration? mikeshn Programming 1 08-16-2002 07:10 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration