LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-04-2002, 01:10 AM   #1
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Rep: Reputation: 15
Is pointer a data-type or what


Hi,
I recently faced this question:

What is a pointer:
(a) data type
(b) data structure
(c) none of these
(d) both of these

Please give explanation also.
Thanks.
 
Old 09-04-2002, 08:15 AM   #2
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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??
 
Old 09-04-2002, 11:35 PM   #3
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Original Poster
Rep: Reputation: 15
That's strange

You say a pointer is an operation. Well then,

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.
 
Old 09-05-2002, 07:06 AM   #4
boku
Member
 
Registered: Aug 2002
Location: Sweden
Distribution: Gentoo
Posts: 43

Rep: Reputation: 15
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.
 
Old 09-05-2002, 07:17 AM   #5
dharmender_rai
Member
 
Registered: Aug 2002
Location: Pune,India
Posts: 39

Rep: Reputation: 15
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.
 
Old 09-06-2002, 04:43 AM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Pointers are definately a data type in C because you can typedef them:

typedef (char *) stringp;

(Not sure if the syntax is 100% right there).

You could then use stringp to mean char *.

Alex
 
Old 09-09-2002, 12:06 AM   #7
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Original Poster
Rep: Reputation: 15
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.
 
Old 09-09-2002, 01:54 AM   #8
dharmender_rai
Member
 
Registered: Aug 2002
Location: Pune,India
Posts: 39

Rep: Reputation: 15
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.

Cheers !!
 
Old 09-09-2002, 02:05 AM   #9
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
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
 
Old 09-09-2002, 02:26 AM   #10
dharmender_rai
Member
 
Registered: Aug 2002
Location: Pune,India
Posts: 39

Rep: Reputation: 15
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.

Cheers !!

Dharmender Rai
 
Old 09-09-2002, 02:43 AM   #11
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Original Poster
Rep: Reputation: 15
Cool Almost final

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.
 
Old 09-09-2002, 03:24 PM   #12
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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
 
Old 09-09-2002, 04:56 PM   #13
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 09-10-2002, 07:24 AM   #14
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Quote:
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.
 
Old 09-10-2002, 07:44 AM   #15
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
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.

http://www-3.ibm.com/software/data/d...0/frame3.htm#c
http://www.csc.uvic.ca/courses/perm_details/csc110.html
( see under dynamic data strs and recursion)

http://www.csci.csusb.edu/cs202/templates.html
(During discussion of templete. 4th para last line)
--Sarin
 
  


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
redeferenced pointer to incomplete type Aldair1808 Programming 1 11-28-2005 03:23 PM
list<type> how can I make type be a pointer? exodist Programming 2 06-06-2005 08:40 AM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
ereferencing pointer to incomplete type? ams Programming 5 03-03-2005 10:32 AM
Getting an incompatible pointer type error... JStew Programming 4 03-06-2003 05:08 PM

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

All times are GMT -5. The time now is 08:57 PM.

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