A very interesting puzzle of "extern char *"

This is a discussion on A very interesting puzzle of "extern char *" within the C++ Programming forums, part of the General Programming Boards category; Here is the code: file.h Code: char p[5] = "abcd"; A.cpp Code: extern char* p; int main(){ printf("%c",p[2]); } The ...

  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 12:14 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    pointers and arrays are different types. Furthermore, I don't know why it would work since one p is in global name space and the other is scoped in main.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Match types and indent properly.

    Code:
    extern char p[5];
    
    int main()
    {
    	printf("%c",p[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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by robwhit View Post
    pointers and arrays are different types. Furthermore, I don't know why it would work since one p is in global name space and the other is scoped in main.
    OK. I made the typo. I revised the post
    Last edited by meili100; 01-09-2008 at 02:06 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Is that a question? Do you understand that char * and char [] are different and not the same type?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Sorry, I don't understand.

    Quote Originally Posted by robwhit View Post
    Is that a question? Do you understand that char * and char [] are different and not the same type?
    Last edited by meili100; 01-09-2008 at 02:15 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Presumably you #include file.h in A.cpp . . . .

    Evidently the type mismatch between *p and p[] is enough to cause the code to not work. I'm not sure why; *p and p[] aren't all that different. I can see *p and p[5] not working, but *p and p[] . . . it's quite strange.

    [edit] Found it:
    2.1: I had the definition char a[6] in one source file, and in another I declared extern char *a. Why didn't it work?
    The declaration extern char *a simply does not match the actual definition. The type "pointer-to-type-T" is not the same as "array-of-type-T." Use extern char a[].

    References: CT&P Sec. 3.3 pp. 33-4, Sec. 4.5 pp. 64-5.
    From http://www.lysator.liu.se/c/c-faq/c-2.html
    [/edit]
    Last edited by dwks; 01-09-2008 at 02:48 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    p[] is an array. *p is a pointer. However, int p[] is an incomplete type so I'll say:

    int p[5];

    is the type. An array is a data type of consecutive elements of a specific type. A pointer simply points to one instance of a specific type.

    When you pass an array to a function expecting a pointer, the compiler does not complain because the array, when used simply with it's name, degenerates into a pointer to the first element in the array. So you are actually passing a pointer, and not an array.
    Code:
    int func(int *pointer)
    {
        printf ("%d", *pointer);
        return;
    }
    
    int main()
    {
        int p[5];
    
        func(p); //passing a pointer
        func(&p[0]); // equivalent
    
        return 0;
    
    }
    p in main has 5 elements. pointer in func doesn't have elements, because it's not an array. It's just a container to hold a variable that refers to an instance of a type. You could access the element in an array if the pointer was pointing to an array like this:
    Code:
    int func(int *pointer)
    {
        printf ("%d", pointer[1]);
        return;
    }
    
    int main()
    {
        int p[5] = {12, 23, 34, 45, 56};
    
        func(p); //passing a pointer
        func(&p[0]); // equivalent
    
        return 0;
    
    }
    A side effect of this behavior is that you can't pass a copy of the array to a function. It always passes a pointer.
    Code:
    int func(int *pointer)
    {
        pointer[1] = 456;
        return;
    }
    
    int main()
    {
        int p[5] = {12, 23, 34, 45, 56};
    
        printf ("%d", p[1]);
    
        func(p); //passing a pointer
        func(&p[0]); // equivalent
    
        printf ("%d", p[1]);//print 456
    
        return 0;
    }
    You can use brackets with pointers because the inventors of the language knew that referring to arrays was a very common use for pointers, and so they decided to include it in the language.

    Pointers might not point to anything. Arrays always have some elements.

    http://www.cprogramming.com/tutorial/lesson6.html
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407

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

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