473,796 Members | 2,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extern int *a , int a[10]

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

Aug 10 '07 #1
6 2984
On Aug 10, 12:24 pm, sinbad <sinbad.sin...@ gmail.comwrote:
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

Aug 10 '07 #2
sinbad wrote:
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 incompatabiliti es
between the shared declaration of `a` and its definition.

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

--
Chris "where's Ivanova when you need her?" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Aug 10 '07 #3
sinbad <si***********@ gmail.comwrote:
a.c
-----
int a[10];
b.c
-----
extern int *a;
If i compile the above two files together ...
what should be the behavior of variable a in fun() ,
*Sigh* The _first_ question in the relevant section of the FAQ:
<http://c-faq.com/aryptr/aryptr1.html>. And the following ones.

Richard
Aug 10 '07 #4
sinbad <si***********@ gmail.comwrites :
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.

--
Ben.
Aug 10 '07 #5
On Fri, 10 Aug 2007 11:56:09 +0100, Chris Dollin wrote:
[1] Skipping over that "link" and "object files" aren't part of the standard ...
But translation units are.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 10 '07 #6
Army1987 <ar******@NOSPA M.itwrites:
On Fri, 10 Aug 2007 11:56:09 +0100, Chris Dollin wrote:
>[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

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
4291
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except where it's defined.
16
1821
by: ooze | last post by:
hi all , anyone could tell me the useage of the keyworkd "extern" in c? and the difference between c & cplusplus? in the C99Rationalv5.10.pdf it lists 4 cases. while the first common cases will incur the vc compiler to complain.
12
2730
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external linkage? Not much on this in the FAQ or tutorials.
19
3862
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
17
4934
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
9
9420
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
4
6260
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare functions written in the "SomeLanguage". But I am quite confused what information does the linkage directive tells the compiler.The "generated" function name? The way the arguments are ordered?Or something else? And I am still wondering why extern...
5
3609
by: Rahul | last post by:
Hi Everyone, I wanted to know if the following declaration is valid? extern int i = 10; #include <cstdio> int main() {
5
1070
by: prashant.khade1623 | last post by:
HI I wrote a simple program to expect some error, but it is printing the value. main() { extern int i;
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.