Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-12-2009
Member
 
Join Date: Feb 2006
Posts: 185
Accessing a variable outside a function without arg

I have a variable declared in main () and function according to finally use a arg_fonction ():
Code:
int main ()
 (
	 int variable = 23;
	 function (& variable);
	
	 getchar ();
	 return 0;
 )
 void function (int * variable)
 (
	 arg_fonction (variable);
 )
arg_fonction void (int * variable)
 (
	 printf ( "% d \ n", * variable);
	 * Variable = 42;
 )
There is no a way to access this variable in another way? Thank you in advance.
Reply With Quote
  #2  
Old 04-12-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Accessing a variable outside a function without arg

What you want to do is called a global variable, use with moderation ...
To make a global variable, it is sufficient for you to declare it outside your function :
Code:
arg_var int = 42;
 int main (void)
 (
      printf ( "% d \ n", arg_var);

      return 0;
 )
Reply With Quote
  #3  
Old 04-12-2009
Member
 
Join Date: Jan 2009
Posts: 199
Re: Accessing a variable outside a function without arg

Have you tried with varying types of extern?
Code:
var j;
$.getJSON("url", function (json) {
// update iji = json.var;
});
Reply With Quote
  #4  
Old 04-12-2009
Member
 
Join Date: Dec 2008
Posts: 177
Re: Accessing a variable outside a function without arg

try this example :

main.c
Code:
# include <stdio.h>
 # include <stdlib.h>
 # include <string.h>

 # include "global.h"

 int main (void)
 (
     printf ( "foo =% i \ n", foo);
     foo = 2;
     printf ( "foo =% i \ n", foo);

     return 0;
 )
global.h
Code:
 # indef global_header
 # define global_header

 # indef GLOBAL_PRIVATE
 extern int foo;
 # else
 int foo = 123;
 # endif

 # endif
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Accessing a variable outside a function without arg"
Thread Thread Starter Forum Replies Last Post
Call a function depending on the variable name Captain Carrot Software Development 5 23-03-2010 09:27 AM
How to pass PHP variable value to jquery function? Kasper Software Development 5 03-03-2010 06:41 PM
Function with a variable number of arguments Chrisch Software Development 3 20-11-2009 02:03 PM
If variable is not initialized in main function Jaisudha Software Development 3 18-11-2009 02:13 PM
How to Call a variable subroutine or function Sayam Software Development 2 16-04-2009 09:33 PM


All times are GMT +5.5. The time now is 07:21 PM.