pointer and array

Discussion in 'C Programming' started by sumitverma1680@gmail.com, Dec 12, 2007.

  1. Guest

    recently i came across this problem in one of C magzines, and it has
    kept me wondering for some time.I am posting it here as i myself could
    not figure it out ! any comments are most welcome.
    I have tried MVC++ for general code test.

    the problem is divided in two .c files as follow :

    file1.c
    #include <stdio.h>
    extern void fn();
    int arr[10] ;
    int main()
    {
    fn();
    }


    file2.c
    #include <stdio.h>
    extern int *arr;
    void fn()
    {
    arr[5]=5;
    printf("%d",arr[5]);

    }

    when the above are compiled and linked there is no error . But during
    de-referncing of arr ponter in fn() , memory acesss violation occurs?
    can any body explain ? thanks in advance.

    --- sumit ---
    , Dec 12, 2007
    #1
    1. Advertising

  2. Ben Pfaff Guest

    writes:

    > file1.c
    > #include <stdio.h>
    > extern void fn();


    The "extern" keyword is unneeded here.

    It would be better to declare the parameters explicitly:

    void fn(void);

    > int arr[10] ;
    > int main()
    > {
    > fn();
    > }
    >
    >
    > file2.c
    > #include <stdio.h>
    > extern int *arr;


    This declaration is incorrect. It should read:
    extern int arr[10];
    or
    extern int arr[];

    > void fn()
    > {
    > arr[5]=5;
    > printf("%d",arr[5]);
    >
    > }
    >
    > when the above are compiled and linked there is no error . But during
    > de-referncing of arr ponter in fn() , memory acesss violation occurs?
    > can any body explain ? thanks in advance.


    The problem is that you are lying to the compiler: arr is defined
    as an array but declared as a pointer. Pointers and arrays are
    not the same thing. When you lie to the compiler, it will get
    its revenge (as someone smarter than me once said).
    --
    "Given that computing power increases exponentially with time,
    algorithms with exponential or better O-notations
    are actually linear with a large constant."
    --Mike Lee
    Ben Pfaff, Dec 12, 2007
    #2
    1. Advertising

  3. Lew Pitcher Guest

    On Dec 12, 12:45 pm, wrote:
    > recently i came across this problem in one of C magzines, and it has
    > kept me wondering for some time.I am posting it here as i myself could
    > not figure it out ! any comments are most welcome.
    > I have tried MVC++ for general code test.
    >
    > the problem is divided in two .c files as follow :
    >
    > file1.c
    > #include <stdio.h>
    > extern void fn();
    > int arr[10] ;


    Here you define arr as an array of 10 integers

    > int main()
    > {
    > fn();
    >
    > }
    >
    > file2.c
    > #include <stdio.h>
    > extern int *arr;


    Here you define arr as a pointer to integers
    Let's make sure you understand what that means: You've said that arr
    names a space which contains a pointer, which, when dereferenced, will
    point to an int.

    > void fn()
    > {
    > arr[5]=5;


    Here, you dereference the pointer, add 5 * sizeof(int) to it, and use
    that pointer value as the target to store the value (5) to.

    The problem is that arr is an array of int, not a pointer. Since arr
    is static, it's contents are initialized to zero.

    So, your code above dereferences arr, pulling what it thinks is a
    pointer out of a space containing 5 integer zero values, adds (5 *
    sizeof(int)) to that pointer, and then tries to access the space
    pointed to by that value.

    > printf("%d",arr[5]);
    >
    > }
    >
    > when the above are compiled and linked there is no error . But during
    > de-referncing of arr ponter in fn() , memory acesss violation occurs?


    Because *(0 + (5 * sizeof(int))) is not writable by your program.

    > can any body explain ? thanks in advance.
    >
    > --- sumit ---
    Lew Pitcher, Dec 12, 2007
    #3
  4. Lew Pitcher <> writes:
    > On Dec 12, 12:45 pm, wrote:

    [...]
    >> int arr[10] ;

    >
    > Here you define arr as an array of 10 integers


    [...]

    >> extern int *arr;

    >
    > Here you define arr as a pointer to integers


    [...]

    Quibble: here he *declares* arr as a pointer to int. (This is a
    declaration; it's not a definition.)

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 12, 2007
    #4
  5. Tor Rustad Guest

    Ben Pfaff wrote:
    > writes:
    >
    >> file1.c
    >> #include <stdio.h>
    >> extern void fn();

    >
    > The "extern" keyword is unneeded here.


    Yes, but fn() do have external linkage.

    > It would be better to declare the parameters explicitly:
    >
    > void fn(void);


    Agreed, but I see nothing wrong with

    extern void fn(void);

    which at least, I do use.

    --
    Tor < | tr i-za-h a-z>
    Tor Rustad, Dec 12, 2007
    #5
  6. Ben Pfaff Guest

    Tor Rustad <> writes:

    > Agreed, but I see nothing wrong with
    >
    > extern void fn(void);
    >
    > which at least, I do use.


    Why do you prefer to write the extern keyword explicitly?
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, Dec 12, 2007
    #6
  7. Tor Rustad Guest

    Ben Pfaff wrote:
    > Tor Rustad <> writes:
    >
    >> Agreed, but I see nothing wrong with
    >>
    >> extern void fn(void);
    >>
    >> which at least, I do use.

    >
    > Why do you prefer to write the extern keyword explicitly?


    I have sometimes forgot to use 'static', when using 'extern', I rarely
    forget.

    --
    Tor < | tr i-za-h a-z>
    Tor Rustad, Dec 12, 2007
    #7
  8. Joe Wright Guest

    wrote:
    > recently i came across this problem in one of C magzines, and it has
    > kept me wondering for some time.I am posting it here as i myself could
    > not figure it out ! any comments are most welcome.
    > I have tried MVC++ for general code test.
    >
    > the problem is divided in two .c files as follow :
    >
    > file1.c
    > #include <stdio.h>
    > extern void fn();
    > int arr[10] ;
    > int main()
    > {
    > fn();
    > }
    >
    >
    > file2.c
    > #include <stdio.h>
    > extern int *arr;
    > void fn()
    > {
    > arr[5]=5;
    > printf("%d",arr[5]);
    >
    > }
    >
    > when the above are compiled and linked there is no error . But during
    > de-referncing of arr ponter in fn() , memory acesss violation occurs?
    > can any body explain ? thanks in advance.
    >
    > --- sumit ---


    Why lie to the compiler? In file1, arr is array 10 of int and indeed has
    external linkage. In file2 you declare it a pointer to int. An array is
    not a pointer. A pointer is not an array.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Dec 13, 2007
    #8
  9. Chris Dollin Guest

    Ben Pfaff wrote:

    > Tor Rustad <> writes:
    >
    >> Agreed, but I see nothing wrong with
    >>
    >> extern void fn(void);
    >>
    >> which at least, I do use.

    >
    > Why do you prefer to write the extern keyword explicitly?


    I do it so I can grep for it.

    --
    Chris "sometimes influenced by available tools" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Dec 13, 2007
    #9
  10. Richard Bos Guest

    wrote:

    > recently i came across this problem in one of C magzines,


    > file1.c


    > int arr[10] ;


    > file2.c


    > extern int *arr;



    The editors of that "C" magazine would do well to read the C FAQ, and in
    particular question 6.1: <http://c-faq.com/aryptr/aryptr1.html>.

    Richard
    Richard Bos, Dec 13, 2007
    #10
  11. Tor Rustad wrote:

    > I have sometimes forgot to use 'static', when using 'extern', I rarely
    > forget.


    Except that using or not using 'static' makes a difference whereas using or
    not using 'extern' doesn't.

    robert
    Robert Latest, Dec 13, 2007
    #11
  12. (Richard Bos) writes:
    > wrote:
    >> recently i came across this problem in one of C magzines,

    >
    >> file1.c

    >
    >> int arr[10] ;

    >
    >> file2.c

    >
    >> extern int *arr;

    >
    > The editors of that "C" magazine would do well to read the C FAQ, and in
    > particular question 6.1: <http://c-faq.com/aryptr/aryptr1.html>.


    I suspect they're aware of it, or at least of the information that it
    presents. The program apparently was presented as a problem; the
    reader is being asked to figure out why it doesn't work.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 13, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    346
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    672
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,244
  4. Hansen
    Replies:
    3
    Views:
    1,097
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page