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.
I'd probably say that it's (c) none of these. Why?!?
Pointers are simply a variable that hold an address so one
could argue that a pointer is a data type, but it is not defined
as a data type (per "The C Programming Language". Kernighan & Ritchie).
A data structure is defined as "a collection of one or more variables...
grouped together under a single name for convenient handling." A
pointer is a collection of one variable (address). But structures are
declared with struct and pointers are denoted by the * operator.
I would suggest that a pointer is an operation (*, & operators) performed
on a data type or data structure that results in an memory address to that
data type.
pretty wild question. this ain't homework or a take home exam, right??
int *p; statement tells the compiler that 'p' is a 'variable' that 'points' to another variable of type 'int'. But, what is p? It is pointer and a variable. That means it is a pointer variable.
Now, agreed upon this. Read this. Any variable has to have a data-type. Right? In acse of structures, the definition given by 'struct' defines its data-type. In other words, the 'struct' keyword creates a new user defined data-type.
So, what is the data-type of 'p' then?
Again, I think that '*' tells the compiler to create a special type of variable on which certain special operations, like dereferencing' can be performed also. And again, the pointer variables like 'p' occupy a constant memory area irrespective of whether they are pointing to a char or int or whatever.
So, there are many things to be sorted out. And we all thought we had learned pointers!!!
Everything seems to be jumbled up to me.
It is not so complicated if one knows what pointers are for the processor (e.g. in assembler). They can be seen just as integers. The processor makes no difference between them. The only difference is what the programmer choose to do with them.
In C however, these integers are divided into two categories. The contents of a pointer refers to a memory location. A normal integer can be type casted to allow such operations.
Hi all,
Every variable associated with a data-type, which is required to provide the compiler about the operation valid for that variable.
Now 'pointer' means that the variable contains some address. The operations defined on that variable are not always the same as that of integers (which most of the C programmers confuse). Like increment operator for integer and a pointer but for the later it is dependent on the type of data it is pointing.
You can't apply multiplication operation on a pointer variable. This means that the compiler, during parsing stage, derives the type and during sematic analysis forbids illegal operation.
From the above facts one can surely aver that 'pointer' is a data-type.
Oh well,
for that matter you can even typedef structures. Can there be anything that is a data type as well as a data structure at the same time? I haven't got anything such on my mind, but if you do, do tell me, 'cause this is also one of the questions.
Hi Aditya,
A data structure is a the systematic(pre-defined) collection of nodes of some data-type. These data-types are user-defined also (using struct/class). For example, a linked list is a data-structure. Its nodes are of a particular data-type.
I think a pointer is a derived data type. Its very clear that pointer can hold data other than point to data. And the data held by pointers are always of same kind. I hope you agree with me that you havent come across a pointer that is half integer and half character. The main differance I can see between the data type and and data structure is the ability of a struct to hold different data types. So, as I told just now pointer is not a structure.
Now, pointers in c are mostly unsigned intgers or long ( correct me if I am wrong). But you are permitted to do only a restricted set of operations on pointers. Which is a clear indication to the fact that pointers are derived data types.
This is only my deduction. But I think I am not that wrong here.
--Sarin
Hi Sarin,
You are right the pointer is a data type. You must know difference between a pointer and a pointer variable. Former is a data type while the later one is a variable of that type (same as in case of int x , int is data type while x is an int variable).
Data is held by the variables. When the variable is of type pointer to a structutre that contains an int and a string then you can say that the variable is pointing to a heterogenous type. If the pointer variable points to an array of integers then you can say that it is pointing to homogenous type.
A pointer variable contains the address of the start of the memory location to which it is pointing. Now the address is always a number. Due to the increment in the size of addressable memory, the limit for a pointer variable has gone from maximum int to maximum long. Of course, addresses are always non-negative.
Well then
I think we have almost agreed on 2 things:
1. Pointer is a data type, irrespective of the fact that no2nt says that Richie's book doesn't consider pointers as data types (though I haven't yet looked it up myself)
2. Pointer is not a data structure.
So, no2nt, sorry but you'll have to give better arguments to contradict these points. Waiting for your reply.
I guess I'll keep trying to play the odd guy here. Please forgive my
random thoughts and statements; hard to do while at work :-) I have
absolutely no intention of flaming anyone here.
I will still suggest that a pointer is (c) none of these.
There are a few basic data types:
char, int, float, double. No mention of the infamous pointer. (pg 33-34)
Quoth the book (italics are original) "The sizes of [each data type] are also machine dependent... There are also arrays, structures, and unions of these basic types, pointers to them, and function that return them,..." (pg 9) Pointers do not seem to be grouped with data types,
according to this text.
I've read conclusions that pointers and integers are interchangable, so
therefore the pointer must be a data type. Most publications I have read
condemn the use of pointers and integers interchangably (mostly for
hardware compatibility reasons) and a whole paragraph entitled "Pointers
are not Integers" is devoted to such an idea (pg 102).
If pointers were data types, I would expect a native data type as such:
Code:
ptr c;
The only logical conclusion I can make is that a pointer is a special
kind of variable that knows nothing of data types and only holds a
memory reference to a declared data typed variable.
Reference:
KERNIGHAN, BRIAN W.
"The C programming language."
DENNIS M., joint author
ISBN 0-13-110163-3
Hmm, we can check it in gcc code, I suppose. I mostly agree with boku.
A pointer is not a data type, it's just an integer (in C). Pointer in general is just an address.
Originally posted by no2nt
... Most publications I have read
condemn the use of pointers and integers interchangably (mostly for
hardware compatibility reasons) and a whole paragraph entitled "Pointers
are not Integers" is devoted to such an idea [in the book referenced] (pg 102).
As far as pointers are integers, my above quote (yes, i'm lazy; i quoted myself) still makes me think otherwise.
Hi, this is getting interesting. Lets hope Mara will find it from gcc documents. But till then we will keep on playing with it. I just googled and found these where they state that pointer is a data type. I even looked at some gcc docs. But could not find anything much interesting. (Except that at some places they use POINTER_TYPE?). Though I understand that its just a matter of definition, and all that matters is the fact that pointer holds address to a data type, str or function, I am just anxious to know the standard definition.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.