extern pointers.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    extern pointers.

    /* a.h */

    #include <stdio.h>
    #include <stdlib.h>

    extern int *a;

    /*a.c */

    #include "a.h"
    #include "b.h"

    int *a = NULL;

    int main(void)
    {
    call(a);
    print(a);
    return 0;
    }

    void call(int *);
    void print(int *);


    /* b.h */

    void call(int *);
    void print(int *);


    /* b.c */

    #include "a.h"
    #include "b.h"

    int n = 5;

    void call(a)
    {
    a = calloc(sizeof(i nt), n);

    a[0] = 0;
    a[1] = 2;
    a[2] = 7;
    a[3] = 9;
    a[4] = 90;

    }

    void print(a)
    {
    int i;
    for(i=0; i<n; i++)
    printf("%d\n",a[i]);
    }


    I'm getting an error saying "Conflictin g argument declarations for
    function 'call'. " in b.c

    Why am I getting an error if a was declared as extern and defined in
    a.c ?
  • pereges

    #2
    Re: extern pointers.

    I think using extern keyword can be very helpful while writing a
    modular program for a binary tree or link list without having to deal
    with double pointers or returning pointers. Just declare the root node
    as extern.

    also, one question i have is: why must we always define an extern
    variable outside the scope of a function ? Is it wrong to define it
    within a function ?

    Comment

    • Ben Bacarisse

      #3
      Re: extern pointers.

      pereges <Broli00@gmail. comwrites:
      I think using extern keyword can be very helpful while writing a
      modular program for a binary tree or link list without having to deal
      with double pointers or returning pointers. Just declare the root node
      as extern.
      You've hit one of the problems: "the" root node. It gets very messy
      building three trees if they all use a "global" for the root. Please,
      don't go down that road. The sooner you learn the other ways (even
      though they will seem overly complex for you current needs) the better.
      also, one question i have is: why must we always define an extern
      variable outside the scope of a function ? Is it wrong to define it
      within a function ?
      Words matter here. You can declare it there but you can't define it
      there. Many people object to the style (local declarations of
      external objects) but doing so does accord with previous advice you
      were given about minimising the scope your names. I am in two minds.
      I've seen (and used) both styles and I can't decide between them!

      --
      Ben.

      Comment

      • David Thompson

        #4
        Re: extern pointers.

        On Fri, 11 Apr 2008 11:26:20 +0100, Philip Potter <pgp@doc.ic.ac. uk>
        wrote:
        pereges wrote:
        /* b.h */

        void call(int *);
        void print(int *);
        /* b.c */

        #include "a.h"
        #include "b.h"

        int n = 5;

        void call(a)
        >
        The a in the parenthesis here has nothing to do with the a declared in
        a.h and defined in a.c.
        Right.
        This just says "call is a function taking one
        argument of unspecified type and returning void."
        Not unspecified; this is oldstyle/K&R1 format using implicit int (no
        longer allowed in C99).
        But because you
        declared call() in b.h as void call(int *), the compiler is rightly
        complaining that you've changed your mind about call's type.
        >
        Right.

        <snip rest>
        - formerly david.thompson1 || achar(64) || worldnet.att.ne t

        Comment

        Working...