Pointers and arrays between files..

Posted on 2003-02-25
Medium Priority
Last Modified: 2010-04-15
Please go thru the fn below and tell me how to explain the reason why the pointer notation does not work with external arrays.


int a[10] = { 1,2,3,4};


extern int *a;
int b[10];
#include <stdio.h>

void Print()
  int *c = b;

  printf("%d\n", a[0]); //does not print
  printf("%d\n", (&a)[0]); //prints
  printf("%d\n", c[0]); //prints
Question by:nagubala
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 8015926
If I recall correctly
int *a;
int a[10];
are different variable declarations according to the linker - therefor it will not "link" them as the same variable.

You should use
extern int a[];

LVL 46

Accepted Solution

Kent Olsen earned 80 total points
ID: 8016459

Within file1, the declaration 'int a[10]' reserves ten integers.  The nature of an integer array is such that you actually put a 1 into element 0, 2 into element 1, etc.

Withing file2, the declaration 'extern int *a' doesn't reserved space.  It tells the compiler that the "already defined symbol 'a'" contains a pointer to an integer.  Where you stored a 1 in element 0, this declaration treats the contents of 'a' as a pointer to an integer.  If a pointer and an int are the same size, you're using the value 1 as an address.  It implies that the array is created by:

  int *a = (int *)malloc (10 * sizeof (int));

Writing the declaration as 'extern int a[]' tells the compiler that the "already defined symbol 'a'" is an array of consecutive integers.

This is quite subtle -- I missed it the first time that I looked at it.  If this had been posted as a quiz, without stating that the code didn't work, I'd have flunked.  :(


Author Comment

ID: 8017356
Thanks! Thats a good way to put it.
This also points out that C should never have a relaxed syntax that array name and a pointer are the same. If they have made it mandatory that only by applying the & operator one can get the address of an array, then this problem would never arise and that rule will universally be applicable. That will also invalidate the extern int *a declaration.
LVL 46

Expert Comment

by:Kent Olsen
ID: 8017634

The standard is much more stingent in C++, which was part of the intent when C++ was developed.

Be aware that as you transition the data type from int to char and finally string (char*) that the behavior may fool you, again.

Strings are already pointers to objects so you'll often see '*' and '[]' used on strings just like you tried to use them on integers.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question