3

Suppose we have 2 files

1) file1.c

int Appples[10];

2) file2.c

extern int *Appples;

Is there any prob with this type of declaration except that i will have to handle size independently ?

4
  • Why not extern int Apples[10]; ? Commented Jan 3, 2016 at 8:52
  • @Mahesh, Does it really affects behavior? Commented Jan 3, 2016 at 8:58
  • Yes, one problem: it's not valid. Don't do this. Commented Jan 3, 2016 at 9:03
  • @Alex Yes. If you like, I can add another answer with concrete examples and assembly code. Commented Jan 3, 2016 at 12:21

2 Answers 2

5

This is covered in C FAQs 6.1

The type pointer-to-type-T is not the same as array-of-type-T. Use extern char a[].

while this answer addresses the issue more specifically. The final point is: an array isn't a pointer and you shouldn't treat one as such.

Sign up to request clarification or add additional context in comments.

Comments

1

When you declare a variable as:

extern int *Appples;

it needs to be defined as:

int *Appples = <initializer>;

not as:

int Appples[10];

If you want to define using:

int Appples[10];

It can be declared as:

extern int Appples[10];

or

extern int Appples[];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.