ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.