ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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].
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.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.