static array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    static array

    hai,
    i have a doubt.will you please explain me the use of static in the below snippets?what happens if we dont use static?
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[5]={1,2,3,4,5};
    int *p[5]={a,a+1,a+2,a+3,a+4};
    clrscr();
    printf("%d\n%d\n%d",p,*p,**p);
    getch();
    }
    output:
    error
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[5]={1,2,3,4,5};
    static int *p[5]={a,a+1,a+2,a+3,a+4};
    clrscr();
    printf("%d\n%d\n%d",p,*p,**p);
    getch();
    }
    output:
    error
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    static int a[5]={1,2,3,4,5};
    static int *p[5]={a,a+1,a+2,a+3,a+4};
    clrscr();
    printf("%d\n%d\n%d",p,*p,**p);
    getch();
    }
    output:
    2000
    1000
    1
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    static int a[5]={1,2,3,4,5};
    int *p[5]={a,a+1,a+2,a+3,a+4};
    clrscr();
    printf("%d\n%d\n%d",p,*p,**p);
    getch();
    }
    output:
    2000
    1000
    1
    note:
    assuming the starting address of array a as 1000 and assuming the starting address of pointer array p as 2000

    this is the output i get in my compiler.
    thanks for your reply on advance.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    These just look like samples from a textbook.

    That means there is no reason to use static except to show that it can be done.

    The better thing is to learn what static means.

    Static means to stay in one place. Inside a function it means the variable remains after the function completes. That provides a way for a function to remember vaues from the previous call.

    If you don't understand static, then you get this kind of mistake from your second example:

    Code:
    void main()
      {
      int a[5]={1,2,3,4,5};
      static int *p[5]={a,a+1,a+2,a+3,a+4};
      etc...
    Here an array a[5] is local and will be destroyed when main() completes. However, the array of pointers to the elements of a[5] remains. The pointers now point to deleted memory.

    This is the dangling pointer error.
    Last edited by weaknessforcats; Jun 3 '12, 05:53 AM. Reason: typo

    Comment

    • jeyshree
      New Member
      • Jul 2010
      • 75

      #3
      but what about the first snippet?why does that show an error?both(inte ger array and array of pointers) are created when control enters the function main and destroyed when control leaves the function main.then none of the elements in array of pointers point to deleted memory.then why does it show an error?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You don't say what error you are getting but here is my opinion.

        There some non-standard calls, like clrscr() and getch(). Plus main() returns an int and not void.

        There is no problem with the arraya.

        Only Microsoft tried to push main() returning void so I am guessing that you are using an old version of Visual C++.

        Comment

        • jeyshree
          New Member
          • Jul 2010
          • 75

          #5
          the error shown is "illegal initialisation" .now please explain me the reason for error in first snippet.thanks for your replies.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I have no idea. The code compiles and links as either C or C++ using Visual Studio.NET 2008 after removif clrscr() and getch().

            Comment

            Working...