0

I'm combining two C programs using a header file.

first program

#include<explore.h>

int main()
{
    int a =10;
    explore();
}

explore program

#include<stdio.h> 

int explore()
{
    // I want to use the value of a. Can I use it? How can sue it?
}

I want to use the value of a in explore() in explore program file.

Can I use it? How can use it, is possible?

Denim Datta
  • 3,740
  • 3
  • 27
  • 53
Beginner
  • 286
  • 4
  • 17

5 Answers5

1

Ok, first off. With the code as it stands now, you won't be able to use the int a anywhere, except in the main function, since it's a local variable to that function.
Either pass it as an argument, or declare it as a global (and an extern for that matter). Either way, I'd opt to pass it as an argument, if a will be changed by the function explore, you can do 1 of 2 things:

int explore( int val)
{
    //do stuff
    return new_val;
}
//call:
a = explore(a);//assigns return int to a

Or, if the returned int signals a status of some kind, pass a pointer to a. This may, if you're new to pointers, seem like a sure way to over-complicate things, but it's very common, and very useful (added note on why this is useful to the bottom):

int explore(int *val)
{
    *val += 123;//add 123 to val's value
    //this is NOT the same as val += 123, because that's shifting the pointer!
    return 0;
}
//call:
int return_val = explore(&a);//pass &[address-of]a

Now, linking two source files is easy, but you say you're using a header file, all well and good, but you don't seem to be including it anywhere, nor are you showing what it looks like. I suspect you're having trouble compiling your code... if so, show us what you've tried (how you're compiling the code), and what errors you're getting.
If you need a basic example of how to manually link 2 source files: Here's a previous answer of mine that shows, step by step, how to link 2 files

Why pointers?:
Many reasons, really. Here's just a few:

  • limit memory usage: If you're passing big structs by value all over the place (ie, copying the same data over and over), your code will be slow, and your stack might end up cluttered with the same value. (recursion induced stack overflow)
  • C can allocate heap memory, which can only be accessed through pointers, you can't turn a pointer variable into a non-pointer.
  • Return values are often ways to notify you of an error that may have occurred.

The last point is crucial. If you're passing a to explore for some complex computation, changing the value of a along the way. If, half way down the function, something goes pear-shaped, how do you notify the caller that the value of a is no longer reliable? Simple:

int complex_function( int *val)
{
    _Bool negative = *val > 0 ? false : true;
    //do all sorts of magic stuff
    //val wasn't negative, but is now, possible maxed int?
    if (negative == false && *val < 0) return -1;//notify using -1
    return 0;//usually, 0 means everything went well ok
}
//call like so:
if ((complex_function(&a)) != 0) exit( EXIT_FAILURE);//it's all gone wrong
//else, continue

Now that's where pointers are very useful. It doesn't require you to mess about with all too many temp variables, creating copies of values all over the place, and compare those to get to the same conclusion.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • this is just an example program. Actually i need to get a clients ip address in the place of 'a'. – Beginner Dec 13 '13 at 09:17
  • @Beginner: Either way, the principle stands: no matter what the variable is, you can either pass it by value, or as a pointer. Both have their pro's and cons. Pass the variable to the `explore` function if you want to use its value. if it's a `char[]`, note that it will degress into a pointer (`int explore(char *a_as_arg)`) – Elias Van Ootegem Dec 13 '13 at 09:20
  • ok sir thank you. can you answer this question[http://stackoverflow.com/questions/20540876/how-to-use-the-clients-ip-address-within-the-function-included-in-the-server-pro/20544729?noredirect=1#comment30754757_20544729]. i have a similar problem,actually its our mini-project but we aren't getting the solution. Can you help us please? – Beginner Dec 13 '13 at 10:01
0

Call explore with a as parameter.

Nevertheless, you don't really seem to have two "programs". You have two source files in one program. So another way to use a in both parts could be to define a in main.c and declare it as "extern" in explore.h. Keep in mind, though, that a is somewhat "global" to your program then.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
0

just pass the value (or pointer) of a to your explore function

pass by value (a copy of a)

#include<stdio.h> 
int explore(int value)
{

}

pass by reference (the pointer pointing at a)

#include<stdio.h> 
int explore(int *value)
{

}
Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
  • I wouldn't recommend passing the pointer (which technically is not pass by reference) unless the OP fully intends to change its value. – lurker Dec 12 '13 at 11:08
  • of course, it depends on what we want to do with A. In the C language context "passing by reference" means passing the pointer, see http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Gianluca Ghettini Dec 12 '13 at 11:12
  • @G_G: I'd have to disagree with you on that one, a reference behaves more like a pointer to a _"constantly dereferenced"_ pointer. If you want to translate that to C, you'd have to use constructs like `int explore( int **ref);` (sorry for being so nit-picky) – Elias Van Ootegem Dec 12 '13 at 11:15
0

Change to int explore(int some_var) and then you are free to use some_var inside this function. In main you need to call as explore(a).

Dayal rai
  • 6,548
  • 22
  • 29
0

First of all, they are not two programs. It's a single program. You are writing a function. If you want to pass the value of a to that function, you need to pass it as a parameter:

#include<explore.h>
int main()
{
  int a =10;
  explore(a);
}

#include<stdio.h> 
int explore(int a)
{
  // you can now use a here
}
parrowdice
  • 1,902
  • 15
  • 24
  • its not a siongle program sir. main and explore are different programs . I am accessing explore program in my main. but i need the value of a in which is declared (in main) in my explore program. – Beginner Dec 13 '13 at 09:18