469,602 Members | 1,931 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

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 3253

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by matthurne | last post: by
4 posts views Thread by Mark Hannon | last post: by
5 posts views Thread by dam_fool_2003 | last post: by
12 posts views Thread by flipflop | last post: by
4 posts views Thread by Bilgehan.Balban | last post: by
15 posts views Thread by Geoff Cox | last post: by
7 posts views Thread by pauld | last post: by
5 posts views Thread by Immortal Nephi | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.