473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 .........pleaase 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 .........pleaase correct that
error

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

Thanks u

Nov 15 '05 #1
8 3479

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 .........pleaase 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 .........pleaase 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 .........pleaase 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 .........pleaase 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_DEFS)
#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("allocation 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**********@yahoo.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 .........pleaase 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_FAILURE);
}
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_Keith) 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_DEFS)
#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("allocation 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_DEFS)
#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("allocation 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_DEFS)
#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
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,...
4
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...
5
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>...
12
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...
4
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...
4
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
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 ...
7
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...
5
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.