473,770 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

intialise array size using global variable

hi Everybody !

decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error

is it Possible? i am trying it gives error!

In file included from main.c:2:
a1.c:3: error: initializer element is not constant
a1.c:4: error: variable-size type declared outside of any function

if possible ,please correct it

a.h
====
extern int a;
void store();

main.c
========
#include<stdio. h>
#include "a1.c"
int main(void)
{
a=10;
}

a1.c
=======
#include<stdio. h>
#include "a.h"
int size=a;
int ar[size];
void store()
{
int i=0;
for (i=0;i<a;i++)
{
ar[i]=i;
}
}

1) decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error

please correct i am trying.. .this is intialise array using global
variable

Thanks u

Nov 15 '05 #1
8 3512

chellappa wrote:
hi Everybody !

decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error

is it Possible? i am trying it gives error!

In file included from main.c:2:
a1.c:3: error: initializer element is not constant
a1.c:4: error: variable-size type declared outside of any function

if possible ,please correct it

a.h
====
extern int a;
void store();

main.c
========
#include<stdio. h>
#include "a1.c"
int main(void)
{
a=10;
}

a1.c
=======
#include<stdio. h>
#include "a.h"
int size=a;
int ar[size];
void store()
{
int i=0;
for (i=0;i<a;i++)
{
ar[i]=i;
}
}

1) decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error

please correct i am trying.. .this is intialise array using global
variable

Thanks u


You haven't declared the variable "a" anywhere. In the "a.h" file
it is decalred as an extern variable. What you can do is to have
another
file "a2.c" where you may declare all the global variables
including "a".
See FAQ no 1.7 to find out the correct way to declare a global
variable.

Nov 15 '05 #2
you just have a declaration, but you didn't define it .
variable 'a' hadn't been allocated memory units.

Nov 15 '05 #3
chellappa wrote:
hi Everybody !

decalaring variable in a.h
Don't do this[1]. Headers are not the place to declare variables.

[1] With care you might. You need to force the declarations to be
external in all translation units but one of those that include the
header. This is easy to do, but error-prone and not recommended.
and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error

is it Possible? i am trying it gives error!

In file included from main.c:2:
a1.c:3: error: initializer element is not constant
a1.c:4: error: variable-size type declared outside of any function

if possible ,please correct it

a.h
====
extern int a;
void store();

main.c
========
#include<stdio. h>
#include "a1.c"
Don't do this. Compile the source files and link them together.
int main(void)
{
a=10;
}

a1.c
=======
#include<stdio. h>
#include "a.h"
int size=a;
int ar[size];
void store()
{
int i=0;
for (i=0;i<a;i++)
{
ar[i]=i;
}
}

1) decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error


If you must do this, look at the following. Consider why you might
*not* want to take the approach I have shown:

/* a.h */
#if defined(LOCAL_D EFS)
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int a;
EXTERN int *ar;
int *store();
/* main.c */
#include<stdio. h>
#include <stdlib.h>
#define LOCAL_DEFS
#include "a.h"

int main(void)
{
a = 10;
printf("Before allocation ar (%p) points to (%p)\n", (void *) &ar,
(void *) ar);
store();
if (ar) {
printf("ar successfully allocated, points to %p\n",
(void *) ar);
free(ar);
}
else
printf("allocat ion for ar failed.\n");
return 0;
}
/* a1.c */
#include <stdlib.h>
#include "a.h"
int *store()
{
int i = 0;
if ((ar = malloc(a * sizeof *ar)))
for (i = 0; i < a; i++)
ar[i] = i;
return ar;
}

Nov 15 '05 #4
ju**********@ya hoo.co.in writes:
chellappa wrote:
hi Everybody !

decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaas e correct that
error

is it Possible? i am trying it gives error!

In file included from main.c:2:
a1.c:3: error: initializer element is not constant
a1.c:4: error: variable-size type declared outside of any function

if possible ,please correct it

[snip]
You haven't declared the variable "a" anywhere. In the "a.h" file
it is decalred as an extern variable. What you can do is to have
another
file "a2.c" where you may declare all the global variables
including "a".
See FAQ no 1.7 to find out the correct way to declare a global
variable.


Correction: he's declared it (as extern), but he hasn't defined it.

He should also pay attention to the error message: "variable-size type
declared outside of any function". A variable-size array can be
declared only inside a function.

If you want (the equivalent of) a global variable-size array, declare
a global pointer and assign a value to it. Note that you can't
actually initialize it with a function call, but you can call a
function to do the assignment. (If you forget to call the function,
you've got problems.)

For example:

==> a.h <==
#ifndef HEADER_A
#define HEADER_A

#include <stddef.h>

extern int *a;
extern size_t a_len;

void init_a(size_t length);

#endif

==> a.c <==
#include "a.h"

int *a; /* definition */
size_t a_len; /* definition */

void init_a(size_t length)
{
a = malloc(length * sizeof *a);
a_len = length;
}

==> main.c <==
#include <stdio.h>
#include <stdlib.h>
#include "a.h"

int main(void)
{
init_a(100);
if (a == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILU RE);
}
printf("a = %p\n", (void*)a);
printf("a_len = %lu\n", (unsigned long)a_len);
/*
* Now we can do stuff with a
*/
return 0;
}

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

Martin Ambuhl wrote:
chellappa wrote:
decalaring variable in a.h


Don't do this[1]. Headers are not the place to declare variables.

[1] With care you might. You need to force the declarations to be
external in all translation units but one of those that include the
header. This is easy to do, but error-prone and not recommended.

If you must do this, look at the following. Consider why you might
*not* want to take the approach I have shown:

/* a.h */
#if defined(LOCAL_D EFS)
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int a;
EXTERN int *ar;
int *store();
/* main.c */
#include<stdio. h>
#include <stdlib.h>
#define LOCAL_DEFS
#include "a.h"

int main(void)
{
a = 10;
printf("Before allocation ar (%p) points to (%p)\n", (void *) &ar,
(void *) ar);
store();
if (ar) {
printf("ar successfully allocated, points to %p\n",
(void *) ar);
free(ar);
}
else
printf("allocat ion for ar failed.\n");
return 0;
}


what's wrong with the following?

/* a.h */
extern int a;
/* EOF */

/* a.c */
#include "a.h"

int a;
/* EOF */

Nov 15 '05 #6
tedu wrote:
what's wrong with the following?

/* a.h */
extern int a;
/* EOF */

/* a.c */
#include "a.h"

int a;
/* EOF */


Nothing.

--
pete
Nov 15 '05 #7
tedu wrote:
Martin Ambuhl wrote:
chellappa wrote:
decalaring variable in a.h


Don't do this[1]. Headers are not the place to declare variables.

[1] With care you might. You need to force the declarations to be
external in all translation units but one of those that include the
header. This is easy to do, but error-prone and not recommended.


If you must do this, look at the following. Consider why you might
*not* want to take the approach I have shown:

/* a.h */
#if defined(LOCAL_D EFS)
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int a;
EXTERN int *ar;
int *store();
/* main.c */
#include<stdi o.h>
#include <stdlib.h>
#define LOCAL_DEFS
#include "a.h"

int main(void)
{
a = 10;
printf("Before allocation ar (%p) points to (%p)\n", (void *) &ar,
(void *) ar);
store();
if (ar) {
printf("ar successfully allocated, points to %p\n",
(void *) ar);
free(ar);
}
else
printf("allocat ion for ar failed.\n");
return 0;
}

what's wrong with the following?


Nothing. you have explicitly declared 'a' in a.c rather than simply
including the header. Your approach is functionally equivalent to mine.
If you have a large number of "global" variables, you will find that
the approach shown above avoids all the retyping that your approach
involves.

/* a.h */
extern int a;
/* EOF */

/* a.c */
#include "a.h"

int a;
/* EOF */

Nov 15 '05 #8
Martin Ambuhl wrote, but no one has yet remarked on:
/* a.h */
#if defined(LOCAL_D EFS)
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int a;
EXTERN int *ar;
int *store();


This is, of course, a no-no. Inxtead of 'EXTERN' you might want to use,
for example, 'XTERN'. I was very naughty using a macro beginning with
'E' followed by an uppercase letter.
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10710
by: matthurne | last post by:
I need to send just an array to a function which then needs to go through each element in the array. I tried using sizeof(array) / sizeof(array) but since the array is passed into the function, sizeof(array) is really sizeof(pointer to first element in array) and therefore doesn't solve my problem. If I can't calculate the size of the array, can I go through the elements without knowing the size and somehow test whether I'm off the end...
4
38255
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is global and any functions should be able to access it. I have been having trouble getting this to work. In the 1st of the 2 examples below I initialize the Array "initArray" with 4 form text fields. When I try to use initArray inside either of...
5
14114
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
12
2755
by: flipflop | last post by:
I need to create a global array whose dimensions depend on the contents of another global array populated at its initialisation. For example: int array1={3,2,1}; int array2]; //should be equiv. to: int array2; GCC gives these errors: variable-size type declared outside of any function variable-sized object may not be initialized
4
5635
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving results in a file, which doesn't hold the information of the changed array anymore. I dont know why. Here is the code: /*
4
21710
by: Bilgehan.Balban | last post by:
Hi, The following code: #include <stdio.h> // const int const_asize = 10; #define define_asize = 10; int array = {1,2,3,4,5,6,7,8,9,0};
15
5324
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have private string myArray;
7
2160
by: pauld | last post by:
Ive got a series of 2 dimensional associative arrays while(list($key, $val) = each ($results)) { {foreach($val as $i=>$v) print "$i = $v"; } } but i want to define the order that the various $i's are printed in
5
3653
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.