Thread: A quick pointer question

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    A quick pointer question

    My code is divided in two source files. One of the source files includes main, the other one some functions I wrote.

    In the source file where main is, I declare the global variable
    Code:
    char hello[256];
    And pass it to a function defined in the other source file:
    Code:
    void function(char *hello); // the prototype
    function(hello);
    Quick question... will the function "function" work with the address of the global variable defined in the other source file, or will it receive a new copy of "hello" and work with that?

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a pointer, so it's the same array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    OK thanks, I thought so, just had a small doubt.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    another relative quick question.

    the function on the other file is prototyped
    Code:
    void function(char *hello);
    but hello is also a global variable, won't it get mixed up since a local variable is named just like a global?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The local variable will "hide" the global variable.
    The global variable will exist but is unaccessible due to the local variable hello.
    This can be circumvented in C++, but not C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    ok thanks.

    i'm learning C++ little by little at the same time I do a lot of stuff...
    I don't know exactly how I could circumvent it, but I bet I need to use the scope operator :: right?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int a = 2;
    void foo(int a)
    {
    	cout << a << endl;
    	cout << ::a << endl;
    }
    
    int __cdecl _tmain(int /*argc*/, TCHAR* /*argv*/[], TCHAR*)
    {
    	foo(1);
    }
    Output:
    1
    2
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    cool, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick Pointer question
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2006, 07:36 PM
  3. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 11:36 PM
  4. Declaring a Pointer (quick question)
    By viciousv322 in forum C Programming
    Replies: 4
    Last Post: 12-16-2005, 11:27 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM