473,473 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static array

75 New Member
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?
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[5]={1,2,3,4,5};
  6. int *p[5]={a,a+1,a+2,a+3,a+4};
  7. clrscr();
  8. printf("%d\n%d\n%d",p,*p,**p);
  9. getch();
  10. }
output:
error
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[5]={1,2,3,4,5};
  6. static int *p[5]={a,a+1,a+2,a+3,a+4};
  7. clrscr();
  8. printf("%d\n%d\n%d",p,*p,**p);
  9. getch();
  10. }
output:
error
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. static int a[5]={1,2,3,4,5};
  6. static int *p[5]={a,a+1,a+2,a+3,a+4};
  7. clrscr();
  8. printf("%d\n%d\n%d",p,*p,**p);
  9. getch();
  10. }
output:
2000
1000
1
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. static int a[5]={1,2,3,4,5};
  6. int *p[5]={a,a+1,a+2,a+3,a+4};
  7. clrscr();
  8. printf("%d\n%d\n%d",p,*p,**p);
  9. getch();
  10. }
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.
Jun 3 '12 #1
5 2032
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:

Expand|Select|Wrap|Line Numbers
  1. void main()
  2.   {
  3.   int a[5]={1,2,3,4,5};
  4.   static int *p[5]={a,a+1,a+2,a+3,a+4};
  5.   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.
Jun 3 '12 #2
jeyshree
75 New Member
but what about the first snippet?why does that show an error?both(integer 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?
Jun 3 '12 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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++.
Jun 3 '12 #4
jeyshree
75 New Member
the error shown is "illegal initialisation".now please explain me the reason for error in first snippet.thanks for your replies.
Jun 4 '12 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
I have no idea. The code compiles and links as either C or C++ using Visual Studio.NET 2008 after removif clrscr() and getch().
Jun 4 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rahul Joshi | last post by:
Hi, I want to create a static array whose size is also a const member of the class. Something like: // A.h class A { private: static int size = 0; static int array;
4
by: Javi | last post by:
Hello there!. I would like to create a class with an array as attribute. This array is the same for all the objects so I'd like to declare it as static, but the problem is: where and how should I...
4
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
11
by: dis_is_eagle | last post by:
hi...i am new to c programming....please explain me as to why should an character array be declared as static...thanx...eric
2
by: Ananas | last post by:
Hi, Please give me an idea how to send a static array from dll written on C++ to C# application. This is a C++ code: const SIGNATURE_LENGTH = 50; struct Info {
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
5
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
1
by: flowstudioLA | last post by:
I have a template class object that I use as a mesaging queue between threads. I use it as a static object that I initialize like so: foo.h class foo{ static LFQueue<const char*,100lfqMyQueue;...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.