Thread: A very interesting puzzle of "extern char *"

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A very interesting puzzle of "extern char *"

    Here is the code:

    file.h
    Code:
    char p[5] = "abcd";
    A.cpp
    Code:
    extern char* p;
    int main(){
      printf("%c",p[2]);
    }
    The result is a run-time error (memory access fault, I think)
    Why is that?

    When I correct the A.cpp as
    Code:
    extern char p[];
    int main(){
      printf("%c",p[2]);
    }
    It does work .
    Last edited by meili100; 01-09-2008 at 01:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A few interesting tools...
    By Mad_guy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-10-2009, 06:07 PM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:12 AM