Like Tree1Likes
  • 1 Post By grumpy

external variable

This is a discussion on external variable within the C Programming forums, part of the General Programming Boards category; Code: #include "vncstudent.h" #include "pprint.c" char HOMEDIR[20]; int main() { extern char HOMEDIR[]; padd(); pprint(); printf("here:%s\n", HOMEDIR); return 0; } ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    26

    external variable

    Code:
    #include "vncstudent.h"
    #include "pprint.c"
    char HOMEDIR[20]; 
    
    
    int main()
    {
      extern char HOMEDIR[]; 
      padd();  
      pprint();
      printf("here:%s\n", HOMEDIR);
      return 0;
    }
    
    
    void padd()
    { int c;
      char add1[20];
    strcat(add1, system("pwd"));
      printf("a:%s\n", system("pwd"));
    }

    Code:
    #include "vncstudent.h"
    
    
    void pprint()
    {
    
    
      extern char HOMEDIR[];
      printf("in pprint:%s\n", HOMEDIR);
    }



    I want to make the HOMEDIR to be able to contain the address after I run "pwd" in the shell. Therefore, I made the the c program to run a system() and paste the result to array, but it is not working. Therefore, I have 2 questions:
    1. How can I make the HOMEDIR reveive the updated address and be able to use it in every different module?

    2. strcat(system(add1, system("pwd")) seems giving me segmentation fault. how can I paste the result to an array?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,169
    Quote Originally Posted by sharonch View Post
    1. How can I make the HOMEDIR reveive the updated address and be able to use it in every different module?
    Declare HOMEDIR extern at file scope, viz
    Code:
    extern char HOMEDIR[20];
    In one (and only one) source file also define it.
    Code:
    extern char HOMEDIR[20];    /*  A declaration, just like in other source files */
    char HOMEDIR[20];             /*   A definition */
    In practice, it would probably be better to place the declaration in a header file, and #include that header file whether needed. That helps ensure consistency of the declarations.


    Quote Originally Posted by sharonch View Post
    2. strcat(system(add1, system("pwd")) seems giving me segmentation fault. how can I paste the result to an array?
    Yikes. Clearly you have not bothered to read the documentation about either the strcat() or system() function. You've made a wild guess at a solution, not checked if it is even realistic, and gotten it badly wrong - and then compounded the problem by random hacking.


    The system() function returns an int, not a pointer to char. Neither does it return a string. It also only expects one argument, not two. strcat() expects a pointer to a zero-terminated char array for the second argument. Giving it anything else causes undefined behaviour.

    What you need to do to use strcat() is #include <string.h>. Similarly, you need #include <stdlib.h> to use system. You have not bothered to #include either of those headers, so the compiler assumes both accept an arbitrary set of arguments. Since its assumptions are invalid, the code compiles but exhibits undefined behaviour (and one possible consequence of undefined behaviour is a segmentation violation).

    The short solution to your problem is not to use the system() call at all to retrieve a working directory. There are all sorts of reasons for that, but the main one is that such a call is not guaranteed to be able to retrieve the working directory in any consistent manner.

    If your system is posix compliant (that includes most unix variants, including linux, over the last 15 years or so) then use the getcwd() function. It is declared in a header file named <unistd.h>. Note that the getcwd() function and <unistd.h> header are not in the C standard, although they are widely available.
    Last edited by grumpy; 02-06-2013 at 11:06 PM.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    I didn't paste the header file which is where I place those library, but I still have question:
    Code:
    #include "vncstudent.h"
    #include <unistd.h>
    #include <string.h>
    
    
    int main()
    {
    
    
      padd();
      printf("1:%s\n", HDIR);
      pprint();
     return 0;
    }
    
    
    void padd()
    {char cwd[256];  
    char HDIR[256];
    
      if(getcwd(cwd, sizeof(cwd))==NULL)
        {
        }else
        {
          strcat(HDIR, cwd);
          printf("cwd = %s\n", cwd);
        }
      printf("a:%s\n", HDIR);
    }
    Code:
    #include "vncstudent.h"
    #include <string.h>
    void pprint()
    {
      char HDIR[256];
      printf("in pp:%s\n", HDIR);
      printf("ok\n");
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    extern char HDIR;
    void pprint();
    void padd();
    int main();
    Code:
    vncstudent: vncstudent.o pprint.o
                gcc -o vncstudent vncstudent.o pprint.o
    
    
    pprint.o: pprint.c vncstudent.h
              gcc -o -c pprint.c
    
    
    vncstudent.o: vncstudent.c pprint.c vncstudent.h
                  gcc -o -c vncstudent.c
    
    
    clean:
            rm -f vncstudent vncstudent.o pprint.o
    I have changed some mistakes and fixed everything I can. I have it run in the mac os x.
    In fact, I just want to be able to read the current working dir and have very function be able to call the variable, HDIR.

    pprint is already included in the header file, it doesn't make sense to complain?
    HDIR is also setup ok?
    but I still get the following weird message:

    $ make vncstudent
    cc vncstudent.c -o vncstudent
    vncstudent.c: In function ‘main’:
    vncstudent.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    Undefined symbols:
    "_pprint", referenced from:
    _main in ccCMSzuA.o
    "_HDIR", referenced from:
    _main in ccCMSzuA.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [vncstudent] Error 1

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,169
    Did you even bother to read my previous post?
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    I did!
    I place
    Code:
    extern char HDIR[20];
    char HDIR[20];
    to vncstudent.h
    and now it is not complain about HDIR.

    But did I do anything wrong with pprint()?
    I am using it to test in calling HDIR in another module. What is wrong?

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    I know what is wrong. It is in the Makefile.
    I run
    Code:
    vncstudent: vncstudent.c pprint.c
            gcc -o vncstudent vncstudent.c pprint.c -I.
    The program run. Is it the linkage issue for my earlier makefile?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying variable from external function
    By haifisch in forum C Programming
    Replies: 2
    Last Post: 05-11-2010, 06:02 AM
  2. Replies: 8
    Last Post: 02-14-2010, 03:14 PM
  3. Access restriction for external variable..
    By bhupesh.kec in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 04:01 AM
  4. external variable
    By OnionKnight in forum C Programming
    Replies: 11
    Last Post: 12-24-2005, 08:50 AM
  5. Is errnum an external variable like errno?
    By xstone in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2001, 12:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21