Results 1 to 5 of 5

Thread: Testing pointers in C -> Can't define a pointer outside a function?!?!

  1. #1
    Join Date
    Oct 2009
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Testing pointers in C -> Can't define a pointer outside a function?!?!

    I was experimenting with pointers and I noticed this...

    Code:
    #include <stdio.h>
    
    int variable = 10;
    int *pointer;
    pointer = &variable;
    
    void address (int test)
    {
        printf("%d\n", test);
    }
    
    void fpointer (int *test)
    {
        printf("%d\n", *test);
    
        printf("%d\n", test);
    }
    
    void main ()
    {
        address(*pointer);
        fpointer(&variable);
    }
    When I tried to compile with gcc, and I get these errors:

    Code:
    point.c:5: warning: data definition has no type or storage class
    point.c:5: error: conflicting types for ‘pointer’
    point.c:4: note: previous declaration of ‘pointer’ was here
    point.c:5: warning: initialization makes integer from pointer without a cast
    If I move lines 4-6 inside main, the program compiles...

    Why can't I initialize a pointer outside main?

  2. #2
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Testing pointers in C -> Can't define a pointer outside a function?!?!

    You can't have any statements outside a function -- only declarations and definitions. However, if you merge the assignment into the definition:

    Code:
    int *pointer = &variable;
    it works.

  3. #3
    Join Date
    Jun 2008
    Beans
    381

    Re: Testing pointers in C -> Can't define a pointer outside a function?!?!

    pointer = &variable;
    is a statement. I.e. an executable line of code. When would you like it to occur, if it's not part of a function?

    If there's a problem here, it's that C actually interprets that as a declaration, albeit a redeclaration, of the pointer variable.

  4. #4
    Join Date
    Oct 2009
    Beans
    41
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Testing pointers in C -> Can't define a pointer outside a function?!?!

    Quote Originally Posted by wmcbrine View Post
    You can't have any statements outside a function -- only declarations and definitions. However, if you merge the assignment into the definition:

    Code:
    int *pointer = &variable;
    it works.
    Weird... Anyway, that way really works! Thanks!

  5. #5
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Testing pointers in C -> Can't define a pointer outside a function?!?!

    It's really not weird, if you follow the logic of C. Note that something like "int *pointer = malloc(4096)" will still not work -- you'll get a message about a non-constant initializer. "int *pointer = &variable" only works because it can be fully evaluated at compile-time.

    Nor is it wrong that the compiler interprets "pointer = &variable", on the top level, as a redeclaration. In C, everything on the top level is a declaration or definition. If there's no type provided, the default type of "int" is assumed. (This is why you get "initialization makes integer from pointer".) In the early days of C, it was common to declare a function with no return type -- letting it default to int. This is frowned on nowadays, but is still valid C. (This is why "data definition has no type or storage class" is only a warning, and not an error.)

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
  •