473,748 Members | 9,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer Declaration/Array definition


Why does this declaration give undefined result:

file1: extern char * p;
file2: char p[10];

Let's assume p has been initialized, now accessing p[i]...
Nov 14 '05 #1
12 2395
<ur**@ur8x.co m> wrote in message
news:cg******** **@news-int2.gatech.edu ...
|
| Why does this declaration give undefined result:
|
| file1: extern char * p;
Allocates memory to store a pointer, which may later be changed
to refer to any memory location.
| file2: char p[10];
Allocates memory for 10 characters at a fixed address.
When the variable p is used, the array is implicitly
converted to a pointer to the first element of the array.
|
| Let's assume p has been initialized, now accessing p[i]...

What is supposed to happen if code that includes
file1 contains a statement such as:
p = NULL;
hth,
Ivan
--
http://ivan.vecerina.com/contact
Nov 14 '05 #2
ur**@ur8x.com wrote:

Why does this declaration give undefined result:

file1: extern char * p;
file2: char p[10];

Let's assume p has been initialized, now accessing p[i]...


file1 thinks that p is a pointer to a char. file2 thinks that p
is an array of 10 chars. This is why the "extern char *p;" should
be in a header file that is included in both file1 and file2, and
then the compiler would complain. This follows the simple
principle that header files are used to export things other
modules need to know about.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #3
ur**@ur8x.com wrote:
Why does this declaration give undefined result: file1: extern char * p;
file2: char p[10];


Other people already explained why this won't work, i.e. because
a char array and a char pointer are very different things, having
not much in common. I guess your confusion is coming from the
fact that under certain conditions the name of an array is dealt
with as if it would be a pointer to (the first element of) the
array, e.g. in

char p[ ] = "hello word";
char *pp = p;

But this only happens when the array is used in "value context",
i.e. if it is used as if it had a value. Then, and only then, it
is taken to mean (often called "it decays into") the address of
the first element of the array.

But in

extern char *p;

'p' isn't used in "value context" (the compiler even doesn't know
that somewhere else an array of chars named 'p' was defined since
that's in a different source file), so the "decay to pointer" rule
doesn't get involved.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
Ivan Vecerina <to************ ***********@vec erina.com> wrote:
<ur**@ur8x.co m> wrote in message
news:cg******** **@news-int2.gatech.edu ...
|
| Why does this declaration give undefined result:
|
| file1: extern char * p;
Allocates memory to store a pointer, which may later be changed
to refer to any memory location.
| file2: char p[10];
Allocates memory for 10 characters at a fixed address.
When the variable p is used, the array is implicitly
converted to a pointer to the first element of the array.
|
| Let's assume p has been initialized, now accessing p[i]...
Yes, well if p is NULL, accessing p[i] wouldn't make sense.
But let's say p[] has been initialized, if the array is
implicitly converted to a point to the first element, shouldn't
pointer arithmetic get us to p + i * sizeof(char)?
What is supposed to happen if code that includes
file1 contains a statement such as:
p = NULL;
hth,
Ivan
--
http://ivan.vecerina.com/contact

Nov 14 '05 #5

Ok, here is what I want to know: What exactly happens when
p[i] is called, as far accessing and dereferncing that makes
the code wrong (yes, I know it should not work, I just want
to know why).

Thanks.
Je***********@p hysik.fu-berlin.de wrote:
ur**@ur8x.com wrote:
Why does this declaration give undefined result: file1: extern char * p;
file2: char p[10];

Other people already explained why this won't work, i.e. because
a char array and a char pointer are very different things, having
not much in common. I guess your confusion is coming from the
fact that under certain conditions the name of an array is dealt
with as if it would be a pointer to (the first element of) the
array, e.g. in char p[ ] = "hello word";
char *pp = p; But this only happens when the array is used in "value context",
i.e. if it is used as if it had a value. Then, and only then, it
is taken to mean (often called "it decays into") the address of
the first element of the array. But in extern char *p; 'p' isn't used in "value context" (the compiler even doesn't know
that somewhere else an array of chars named 'p' was defined since
that's in a different source file), so the "decay to pointer" rule
doesn't get involved.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de

Nov 14 '05 #6
ur**@ur8x.com wrote:
Je***********@p hysik.fu-berlin.de wrote:
Please be so kind not to top-post.
Ok, here is what I want to know: What exactly happens when
p[i] is called, as far accessing and dereferncing that makes
the code wrong (yes, I know it should not work, I just want
to know why).


In the process of compiling and linking the symbol 'p' will
get replaced by a certain memory address. The code in file2
knows that at this address there's a string, e.g. "ABCDEFG".
But the code in file1 assumes that at that address a pointer
to char is stored. Since you have "ABCDEFG" at that address
the code in file1 will interpret this value stored there as
an address like 0x61626364' (assuming you have 4 byte char
wide addresses on a big-endian machine and ASCII charset, so
0x61 == 'A' etc.). But that's of course no address but just
the bit pattern of the start of the string. If you then use
'p[i]' it tries to dereference that address (0x61626364 + i),
an address to which you proably have no access to and thus
you get a segmentation fault.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #7
<ur**@ur8x.co m> wrote in message
news:cg******** **@news-int.gatech.edu. ..
| Ivan Vecerina <to************ ***********@vec erina.com> wrote:
| > <ur**@ur8x.co m> wrote in message
| > news:cg******** **@news-int2.gatech.edu ...
| > |
| > | Why does this declaration give undefined result:
| > |
| > | file1: extern char * p;
| > Allocates memory to store a pointer, which may later be changed
| > to refer to any memory location.

| > | file2: char p[10];
| > Allocates memory for 10 characters at a fixed address.
| > When the variable p is used, the array is implicitly
| > converted to a pointer to the first element of the array.
| > |
| > | Let's assume p has been initialized, now accessing p[i]...
|
| Yes, well if p is NULL, accessing p[i] wouldn't make sense.
| But let's say p[] has been initialized, if the array is
| implicitly converted to a point to the first element, shouldn't
| pointer arithmetic get us to p + i * sizeof(char)?

This "implicit conversion" is performed by the compiler, when
it knows that an array is being used as if it were a pointer.
But the generated code and memory layout is very different.

For the array, the assembly pseudocode for p[1] looks like:
- if p is an array:
1) load the address of p in register A
2) increment register A
3) read the byte at address A
- if p is a pointer:
1) load the address of p in register A
2) load the pointer at address A into register B
3) increment register B
4) read the byte at address B

The memory layout is what my previous comments where trying
to explain (left quoted above).
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 14 '05 #8
> get replaced by a certain memory address. The code in file2
knows that at this address there's a string, e.g. "ABCDEFG".
But the code in file1 assumes that at that address a pointer
to char is stored. Since you have "ABCDEFG" at that address
the code in file1 will interpret this value stored there as
an address like 0x61626364' (assuming you have 4 byte char
wide addresses on a big-endian machine and ASCII charset, so
0x61 == 'A' etc.). But that's of course no address but just
the bit pattern of the start of the string. If you then use
'p[i]' it tries to dereference that address (0x61626364 + i),
an address to which you proably have no access to and thus
you get a segmentation fault.


Excellent, so the p[i] is treated as if it holding an address
to the actual data intended to be read. Thanks.

P.S. Sorry about the top-posting, I just switched my default
editor to emacs.
Nov 14 '05 #9
Ivan Vecerina <to************ ***********@vec erina.com> wrote:
This "implicit conversion" is performed by the compiler, when
it knows that an array is being used as if it were a pointer.
But the generated code and memory layout is very different. For the array, the assembly pseudocode for p[1] looks like:
- if p is an array:
1) load the address of p in register A
2) increment register A
3) read the byte at address A
- if p is a pointer:
1) load the address of p in register A
2) load the pointer at address A into register B
3) increment register B
4) read the byte at address B The memory layout is what my previous comments where trying
to explain (left quoted above).


Thanks. Referring to some other posts, does this "implicit
conversion" also known as "decaying convention?"
Nov 14 '05 #10

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

Similar topics

5
5642
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as 1. char array or char array and it will get decayed to 2. char *array
7
8723
by: Michael Birkmose | last post by:
Hi everyone!, Are pointers to incomplete types allowed in ANSI C? I can see that pointer arithmic on pointers to incomple types is impossible, however there are situations where it can be useful: consider this:
5
41035
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based on the previous definition of typedef, I can't understand the next:
42
5619
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
7
2806
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint uiParam0; public uint uiParam1;
73
3833
by: Markus | last post by:
Hi, I can't understand why this code causes a "memory read exception" at int x=**a; void pass(int** a) { int x=**a; } void main()
17
3259
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
17
2720
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This compiler I use for the Samsung 16 bit Smartcard chips and I want to know if it is compliant with the ANSI standard or if it violates it. Have a look at this super simple example, where the value of b is incorrect:
42
5338
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
0
8991
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
9374
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
9249
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...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
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...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.