LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2009, 03:10 AM   #1
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Pointer definition


Ok, it might seem as a really stupid question, but I need to know how the pointer is defined:

1) As a data type, which holds a value in some place in memory and that value is an address of an object
2) or simply as an address of an object

The difference between 1st and 2nd is that 1st requires space to store pointer's value, while second doesn't. Or maybe my value's definition is also wrong (value can't exist without a variable)

For example:

char a[] ="bar";
char *p; //p is a pointer and a place to hold it's value (a reference to a char) was allocated in memory

Now, according to this:
Quote:
Originally Posted by c-faq/6:3
A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.
That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0].
array is decaying into a pointer. If pointer is defined as 1st, then memory is allocated to hold in my case a (name of array) value, which is &a[0]. But according to second paragraph &a[0] is generated, which is simply an address of b letter (and memory for storing that value is not allocated).

So does pointer mean several related things or is it just another problem of not being native and misinterpreting text?

Basically, if we could do something like this: *(0xDEADBEEF)='z' would 0xDEADBEEF be considered as a pointer?

Last edited by Alien_Hominid; 05-21-2009 at 03:29 AM.
 
Old 05-21-2009, 08:56 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
In the simplest of terms, without any of that programming jargon:

A pointer is a variable that contains a memory address.

That's all it is, and as a variable it too resides at some address, and its contents or value is a memory address, usually the address of another variable (the variable to which it "points" to). If you think of it in these terms, I don't think you can go wrong. If you want a more complicated definition, not to worry, because I'm positive the more professional programmers here will provide you with one shortly.
 
Old 05-21-2009, 12:10 PM   #3
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
Quote:
That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0].
&a[0] does not create a variable
 
Old 05-21-2009, 12:23 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I don't normally get all pedantic about these kinds of things, but in this case the original poster came looking for some finer points of interpretation. I would disagree with the statement 'A pointer is a variable that contains a memory address'. I think a more accurate description would be that a pointer is a data type or an instance of one such type, and that a variable may be defined as storage for a pointer datum. By comparison to other data types, such as integers or floating points, you can have pointers which are not stored in variables, in other words, constants. The particularly confusing one of these is the literal string, from which the compiler produces a pointer. This pointer data may then be stored in a suitable variable. Similarly, with numeric data types, one can have constants, which are not variables, but which may be assigned to suitable variables.
As an example
Code:
   double  f;      /* 'f' is a variable that contains a double-precision floating point datum */
   int     i;      /* 'i' is a variable that contains a signed integer datum */
   char *  s;      /* 's' is a variable that contains a pointer to a character */

   f = 123.456;    /* constant value assigned to variable 'f' */
   i = -7;         /* constant value assigned to variable 'i' */
   s = "this is a string";    /* the address of the zeroth element of the 
                              ** constant string literal is assigned to the variable 's'
                              */
The 'address of the zeroth element of the constant string literal' is a constant in the same sense as the two numeric constants. Since it is the address of something, we call it a pointer. In common usage, we say that 's is a pointer to a character'. Some might argue that this is technically incorrect.
--- rod.

Last edited by theNbomr; 05-21-2009 at 12:25 PM.
 
Old 05-21-2009, 01:01 PM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
Just a quick remark before analyzing your reply: I came here not because I wanted to discuss intepretation, but because intepreting correctly is important to understand the difference between array name and a pointer (that the memory is accessed differently), which I need to figure out.

Last edited by Alien_Hominid; 05-21-2009 at 01:03 PM.
 
Old 05-21-2009, 02:03 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, then I will try to address this issue more directly. It turns out that my previous post sets up the description nicely.
A pointer is a data type, or an instance of a pointer data type. An array defines some storage for a specified data type. In order to access an array indirectly, we must point to it. By virtue of the shorthand method provided in the C language, we can get the address of the zeroth element of any array. This 'address of' is a pointer. We may then assign the pointer to a suitable variable. As you pointed out, simply taking the address of the zeroth element of a named array does not create a variable. To be more accurate, it does not create storage for anything. It is merely a constant pointer. As a constant, it may be assigned to a variable. The same shorthand notation for a constant pointer can be used for any named array variable (of any data type), or for literal strings, which is a special form of array data.
Your use of the phrase 'array name' touches upon an important concept. Named arrays have a constant address in memory, and we can therefore treat their addresses as compile-time constants. In contrast, we can have dynamically allocated arrays, as allocated by the malloc() family of functions, and which do not exist until run-time.
I think that the use of this shorthand notation is the source of more confusion among [mostly beginner] C programmers than any other aspect of the language. Once one understands it and the subtleties associated with it, the notation is very useful, expressive, and consistent. Just to heap on a little more confusion, we are allowed to use string literals as initializers for named character arrays. I don't think your question relates to that matter, however.
--- rod.
 
Old 05-21-2009, 07:15 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In front of me, I see four bytes of memory. The value in those four bytes of memory, on my 32-bit computer, happen to be (in hexadecimal): $12345678.

What does this mean?

Answer: it's completely up to you!

If you tell me that those four bytes are an int, then they represent the number (in decimal): 305,419,896. A perfectly ordinary integer.

If you tell me, on the other hand, that those four bytes should represent a pointer to an int, then I will retrieve the value of that integer indirectly. That is to say, I will go to location $12345678 (or die trying...) and retrieve the four bytes located there, and whatever those four bytes turn out to be, that will be your answer. (Either way, unless I "die trying," what I'll come up with is ... a perfectly ordinary integer. In one case, those four bytes came from "here," while in the other case they came from location $12345678.)

So, when it comes to the physical hardware implementation of "a pointer," then the original statement is exactly correct: a pointer is a variable whose purpose is to contain "a memory address." The semantics of this concept vary from language to language, but the concept itself is universal.
 
Old 05-21-2009, 07:59 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by theNbomr View Post
As you pointed out, simply taking the address of the zeroth element of a named array does not create a variable. To be more accurate, it does not create storage for anything. It is merely a constant pointer. As a constant, it may be assigned to a variable.
This is not necessarily a constant (neither in the sense of a “constant expression” nor as a const-qualified type). It may be more accurately be described as being a non-lvalue.
 
Old 05-23-2009, 05:59 AM   #9
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by osor
This is not necessarily a constant (neither in the sense of a “constant expression” nor as a const-qualified type). It may be more accurately be described as being a non-lvalue.
That is this value is known at compile time and memory isn't allocated in the stack to hold it as it is done with variables.

Thank you all. Now I understand that saying that array name decays into pointer does not mean that a space to hold that value is created (in the stack). This value is placed directly into a text (code) segment during compilation and therefore memory access is different when comparing to the pointer variable.

Last edited by Alien_Hominid; 05-23-2009 at 06:02 AM.
 
Old 05-26-2009, 06:51 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Alien_Hominid View Post
That is this value is known at compile time and memory isn't allocated in the stack to hold it as it is done with variables.
Not necessarily. The value is most likely computed at runtime (especially since nowadays compilers are more and more using position-independent code), and depending on the complexity of the expression/the number of available registers, you may need to store it on the stack temporarily.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration