extern int *a , int a[10]

S

sinbad

hi, check out the following program ...

a.c
-----
extern void fun();

int a[10];

int main ()
{
fun ();
}

b.c
-----
extern int *a;

void fun()
{
a++;
printf ("%d",a);
}
If i compile the above two files together ...
what should be the behavior of variable a in fun() , it should be
pointing to the array right ? More specifically this should give a
compile error , as i expected because in file a.c , a is an array that
means the memory location pointed to by 'a' is a constant ( a is a
constant pointer , although not completely correct , we can think like
that) and in file b.c , we are declaring 'a' as a non const pointer to
integer ... There is a type mismatch here ... Can somone explain
this ...

thanks
sinbad
 
A

anon.asdf

hi, check out the following program ...

a.c
-----
extern void fun();

int a[10];

int main ()
{
fun ();

}

b.c
-----
extern int *a;

void fun()
{
a++;
printf ("%d",a);}

If i compile the above two files together ...
what should be the behavior of variable a in fun() , it should be
pointing to the array right ?

But it does not!

Try these test programs:

/** a.c **/
#include <stdio.h>

extern void fun();

int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int main ()
{
printf("&a = %p\n", a);
fun ();

}


/** b.c **/
#include <stdio.h>

extern int *a;

void fun()
{
a++;
printf("a++; &a = %p\n", a);
// printf ("%d\n",*a);
}



On gcc (linux) I get:
&a = 0x8049600
a++; &a = 0x4


You can get your desired behaviour (but that was not what your
question is about, right?) with
/** b2.c **/
#include <stdio.h>

extern int a[10];
int *a2 = a;

void fun()
{
a2++;
printf("a++; &a = %p\n", a2);
// printf ("%d\n",*a2);
}


-anon.asdf
 
C

Chris Dollin

sinbad said:
hi, check out the following program ...

a.c
-----
extern void fun();

int a[10];

int main ()
{
fun ();
}

b.c
-----
extern int *a;

void fun()
{
a++;
printf ("%d",a);
}
If i compile the above two files together ...

What do you mean by "together"? Catenate the files and supply them
as one unit to the compiler, or compile each separately and link
the object files [1]?

If the latter, you have a type mismatch that the implementation
isn't required to detect. Undefined behaviour. If you're lucky,
BOOM. If you're unlucky, BOOM tomorrow, for some value of "tomorrow".
what should be the behavior of variable a in fun() , it should be
pointing to the array right ? More specifically this should give a
compile error , as i expected because in file a.c , a is an array that
means the memory location pointed to by 'a' is a constant ( a is a
constant pointer , although not completely correct , we can think like
that)

We can, but we should not. Just think of it as "`a` is [the name of] an
array of [[10]] ints". Arrays are not pointers; pointers are not arrays.
(Books are not newspapers; newspapers are not books; but they have
shared operations, such as providing entertainment and kindling, as
well as unshared ones -- wrapping one's fish'n'chips in a book is
a tad tricky.)
and in file b.c , we are declaring 'a' as a non const pointer to
integer ... There is a type mismatch here ... Can somone explain
this ...

You're right. There's a type mismatch. Given that the implementation
isn't required to detect it, what should we do?

Answer: don't mess around putting declarations for `a` in the .c
file. Put them in a .h file. My advice would be to put such a
declaration in a .h file with the same name as the .c file that
/defines/ `a`, here `a.c`. So in `a.h` put

extern int a[10];

Then have `a.c` and `b.c` both #include `a.h`. Now `b.c` sees the correct
declaration for `a`, and compiling `a.c` will expose incompatabilities
between the shared declaration of `a` and its definition.

[1] Skipping over that "link" and "object files" aren't part of the standard ...
 
B

Ben Bacarisse

sinbad said:
hi, check out the following program ...

a.c
-----
extern void fun();

int a[10];

int main ()
{
fun ();
}

b.c
-----
extern int *a;

void fun()
{
a++;
printf ("%d",a);
}
If i compile the above two files together ...
what should be the behavior of variable a in fun() , it should be
pointing to the array right ? More specifically this should give a
compile error , as i expected because in file a.c , a is an array that
means the memory location pointed to by 'a' is a constant ( a is a
constant pointer , although not completely correct , we can think like
that) and in file b.c , we are declaring 'a' as a non const pointer to
integer ... There is a type mismatch here ... Can somone explain
this ...

What is the real question here? Most C linkers can't type check, so
it is up to you not to lie. If you lie, your get a program with
undefined behaviour. Your example is really no different to declaring
a function int foo(void) and defining it (in another source file) as
double foo(void). The program might or might not break, but it is not
a valid C program.
 
K

Keith Thompson

Army1987 said:
[1] Skipping over that "link" and "object files" aren't part of the
standard ...
But translation units are.

And translation phase 8 (C99 5.1.1.2) is:

All external object and function references are resolved. Library
components are linked to satisfy external references to functions
and objects not defined in the current translation. All such
translator output is collected into a program image which contains
information needed for execution in its execution environment
 

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

Similar Threads

extern function defination 8
Distributed array initialization 11
Question about extern keyword 7
extern 29
Why no compilation error. 14
Use of extern 2
extern variable 3
Restricting the access of a Global variable 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top