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.
Are you referring to this statement from that link?
(italics are mine)
Quote:
In most C implementations, pointers are simply represented using natural
machine addresses, which are basically binary numbers. In segmented mainframe
architectures, a pointer might consist of two fields: an object selector field,
and an offset into that object. The selector is an index into a descriptor
table, which holds the actual physical address of each described object, as
well as information about its size, access permissions and so on.
If so, it seems to depend on the machine's architecture and compiler. In the
case of a pointer utilizing two (or more) fields, then one could call a pointer
a data type or maybe even a data structure. That may be exactly the type
of architecture the Crays use. Would this be a incorrect conclusion?
Hi, I was preparing for my exams ( This is my quiz week... ) And happen to see this. This is from the book "Data Structures And Algorithms" By Aho, Hopcroft and Ullman ( ISBN 81-7808-102-4 ). This is how it goes.
From chapter 1, section 1.3
"1.3 Data types, Data structures and Abstract Data Types.
........
A cell is the basic building block of data structures. We can picture a cell as a box that is capable of holding a value drawn from some basic or composite data type.
........"
( Under the same subsection )
"Pointers and cursors.
........
A pointer is a cell whose value indicates another cell.
........
A cursor is an integer valued cell, used as a pointer to an array. As a method of connection, cursor is essentially the same as the pointer,....
......."
I think this should clear all doubts. POINTER IS A DATA TYPE. IT CAN BE AN INTEGER OR SOME OTHER BASIC OR COMPOSITE DATA TYPE. ( Refer to the definition of cell )
Hi no2nt,
Sorry but you quoted the wrong lines. The lines of which I was talking about are:
'On the vast majority of implementations, all pointers have the same size, regardless of the type they point to, but you should not rely on that. All that is guaranteed is that void pointers and char pointers have the same size and representation.'
But yeah, that cray example you gave me, I had overlooked that. A pointer might be data structure on architectures and not on others. That's the inference I draw from your words. But, one more thing, I've read the generally intel besed processors, like the pentium series, are segmented architetures, so the segment descriptors etc. also aplly to them. (And they do, I know that).
And by the post of sarin, it's becoming more convincing that in general the pointer might be a data-type, if we leave the specifications of a particular language behind.
But this data-structure part needs more probing, as well as the lines I have just quoted from the site no2nt had given.
I don't think data structure and data type have an exact meaning. I know there are lots of books which have a little sentence saying what a data structure and a data type are, but that's just to help you understand the rest of the book; it's not a canonical definition.
I mean, if I define structure in C:
struct foo
{
int a;
int b;
};
Then I would say struct foo is a data type, but an instance of struct foo would be a data structure. However, you could also refer to struct foo as a data structure, and an instance of struct foo as an instance of a data structure. My point is that no-one has ever come up with a standard definition of these terms.
You could say a pointer is a data type, or that a pointer variable has a data type, etc., etc., but none of those terminologies would be any more correct than the other, IMHO.
Hi,
i think Alex has really something to say. So, we can safely say that pointer is a data-type after all. (Again, I'm not taking particular laguages into account).
But, I have something more to say. I happened to stumble upon my engg. notes one day, and as I ahd time I just galanced through them. Hence, I know that at least the intel 80x86 series is paged segmented architecture, but inherently segmented only, paging was added later on from 80386. Hence, all the addresses are in the form of
[segment no.]:[offset within segment].
When we write some address in C, then the compiller by default assumes that it is the offset, and the segment is assumed to be the one in which your program is running, ( I think it's most probably the data segment).
Through 'far' pointers or 'huge' pointers we can address other segments too, but then both the segment no, and offset have to be provided, as in the C function 'MK_FP'.
Again, when an interuupt occurs, the Interrupt Vector Table is organized as the [Code Segment]:[Intruction Pointer] data structures.
Though a pointer can't be data-structure in the sense that it is half int, and half float, it certainly can be in the sense that both the fields are integers (keeping in mind that pointers are integers, though with some constraints).
So, it seems to me that pointers are also data-structures. Flat addresses are generally not used in any machine since a very long time now.
Well the reason of not having a pointer data type could be the size of the pointer variable.
Integer,characters,float double all have fixed sizes for a given machine.buT that is not the case with pointer variable.
pointer variable size depends upon the type of variable it points to.
Also Pointers can be dereferenced and there can be hierarchy of pointers(i.e pointer to a pointer to a pointer.....) which are not a property of other data type.
Best here to go back to first principles. In a strongly typed language, which C is now, every datum has a type. This is used for two purposes: to define a location suitable for storing a datum and for type checking to ensure that, for example, assignments make sense. Thus, pointer is not a type, but describes a collection of types.
For example, int* is a data type derived from the base type int; it is not the same as int** nor as double*. The fact that in each case the datum is some flavour of integer is irrelevant here: the program treats them as all different.
One should avoid a possible fallacy: note that while it is true to say 3 is an int, it is quite untrue to say int is a 3.
A pointer is an integer-like variable whose value is understood to be a memory address (or zero, which is NULL, which simply means that the pointer points to nothing-at-all).
Pointers enable you to refer to locations in memory indirectly. Its most-common purpose is the construction of arbitrary data-structures in memory, using blocks of storage that are carved-out on demand via malloc() or what-have-you. (This function returns a pointer to the block of storage that was just allocated, which could be anywhere.)
Part of the definition of the pointer variable is to identify what sort of thing it "points to." (If you say void *, it means that you aren't telling the compiler what it points to.)
Point your finger at the wall. The pointer is your hand. What the pointer (currently) points to is the wall. So you can refer to the wall as "what the pointer is currently pointing-to." And you can, at any time, make it point to something else by loading a different memory-address value into it.
The only "gotcha" with regard to using pointers is that you must always know whether you are referring to the pointer itself (your hand ...), or the pointer's target (the wall, or whatever the pointer is currently pointing to). The syntax is not entirely un-ambiguous.
Last edited by sundialsvcs; 02-03-2014 at 08:00 PM.
Hey guys I went through the whole discussion. The conclusion reached here was that pointer is a data type.
I was asked this question for an interview last week. And I answered that the pointer is a data type.
But the interviewer corrected me and said that the pointer is a data structure.
His view about it was, data structures are used to store data with some basic functionalities defined with it. And pointer qualifies this condition.
He said, i quote "a pointer stores the address of a variable, it also holds information on the type of data that should be held in that memory. This is a functionality of the pointer which is predefined hence making it a data structure."
These terms are quite "soft", especially without any context. Even if you choose a language, say C, the word 'pointer' can mean any or all of these:
Code:
a pointer constant -- (char *)300
a pointer variable -- argv
a type -- FILE *
a data pointer -- &errno
a code pointer -- &main
a pointer to a built-in type -- int *p
a pointer to a compound type -- struct tm *
a pointer to another pointer -- char **argv
a pointer to a string-literal-- "literal"
(many other things I forgot to mention)
He said, i quote "a pointer stores the address of a variable, it also holds information on the type of data that should be held in that memory. This is a functionality of the pointer which is predefined hence making it a data structure."
The compiler might hold that information about a pointer but once the program is compiled to an executable, I think that you will find that the pointer variable contains nothing other than an address (at least, I can't see anything if I run gcc with the -S option).
He said, i quote "a pointer stores the address of a variable, it also holds information on the type of data that should be held in that memory. This is a functionality of the pointer which is predefined hence making it a data structure."
And that's not true for all C data types?
Honestly, if you get that question on an interview, I'd recommend walking out. Because, aside from the much-discussed merits of the question itself, they're plagiarizing interview questions from 2002 instead of coming up with their own.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.