Results 1 to 4 of 4

Thread: Accessing a variable outside a function without arg

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

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

  3. #3
    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;
    });

  4. #4
    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

Similar Threads

  1. Call a function depending on the variable name
    By Captain Carrot in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 09:27 AM
  2. How to pass PHP variable value to jquery function?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 06:41 PM
  3. Function with a variable number of arguments
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 02:03 PM
  4. If variable is not initialized in main function
    By Jaisudha in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 02:13 PM
  5. How to Call a variable subroutine or function
    By Sayam in forum Software Development
    Replies: 2
    Last Post: 16-04-2009, 09:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,095,067.50935 seconds with 17 queries