intialise array size using global variable

C

chellappa

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;
}
}

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
 
J

junky_fellow

chellappa said:
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;
}
}

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.
 
K

kernelxu

you just have a declaration, but you didn't define it .
variable 'a' hadn't been allocated memory units.
 
M

Martin Ambuhl

chellappa said:
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;
}
}

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;
return ar;
}
 
K

Keith Thompson

chellappa said:
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;
}
 
T

tedu

Martin said:
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 */
 
M

Martin Ambuhl

tedu said:
Martin said:
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.
 
M

Martin Ambuhl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top