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,
but I still don't understand how could a pointer pointing to different structures or data types occuy different lengths. This I found at the site that no2nt has given.
because it points to the start address of what it is pointing to.
the type before the *, eg int*, tells the rest (size of data)
and pointers are types, of course, in some languages more noticeable than in others
On some platforms the pointers themself might be of different length. Most infamous example is intel-8086 with its 'small data, large code', 'large data, small code', 'huge data, huge head-ache' combinations.
On some platforms the pointers themself might be of different length. Most infamous example is intel-8086 with its 'small data, large code', 'large data, small code', 'huge data, huge head-ache' combinations.
hm, and the near and far pointer, let's be glad that they are gone, even if we still have to deal with artefacts of them (eg std::allocators where just a kind of hack to handle these pointers and where added just that MS would vote for the std to become standard, what they of course not did anyway.)
On some platforms the pointers themself might be of different length. Most infamous example is intel-8086 with its 'small data, large code', 'large data, small code', 'huge data, huge head-ache' combinations.
Whether a pointer is a "near" type or a "far" type, its size depends only on the addressing mechanism. It has nothing to do with the size of the data it is pointing to.
But the interviewer corrected me and said that the pointer is a data structure.
The original question of this thread was stupid, but that interviewer significantly stupider.
When you are doing anything other than quoting from a standards document English is far too ambiguous a language for this kind of question.
Even when you are quoting from a standards document, English tends to be too ambiguous.
The simplest and most general/abstract questions (such as the original one here) are the ones in which English fails the most at carrying actual meaning.
Within the overwhelming ambiguity of English, a "pointer" would most plausibly be a "pointer variable" which is neither a type nor a data structure. Stretching the meaning of "pointer" to be a type is a modest (but I think unsound) leap. Stretching the meaning of "data structure" to cover a reasonable meaning of "pointer" is much worse.
Within the overwhelming ambiguity of English, a "pointer" would most plausibly be a "pointer variable" which is neither a type nor a data structure.
That probably explains it best. A pointer is simply a variable that contains an address.
What type of variable is stored at that address depends on the rules of the compiler. Prior to the ANSI standard, C didn't do type checking and a pointer to a float variable could just as easily be regarded as a pointer to a character array without modifying the pointer variable at all. (You can still do this in assembler or FORTH). In ANSI C, you would have to do a type cast to change the type of variable the pointer references but the number stored in the pointer variable still doesn't change.
Pointer: A variable whose value is understood to contain a memory address. The value "zero," a.k.a. NULL or "nil," by-convention is taken to mean that the pointer points to nothing at all.
Composite Data Type: Any construction, usually called a "struct" or "record," which consists of multiple simple data-types in a fixed arrangement, occupying consecutive bytes in memory.
Data Structure: An arbitrary arrangement of data in memory, ordinarily constructed from memory that is dynamically allocated ("on demand") from the heap. The members of the structure are Composite Data Types of various kinds. Because the memory for the structure is dynamically allocated, pointers are necessarily used to connect one part of the structure to other parts. (In "on-disc" data structures, record-numbers or file-positions serve the same role and purpose as do pointers in "in-memory" data structures.)
hey aditya,
if u have read about the datatypes then you will find that they are classified as follows in C++
1)address pointer,references
2)logical bool
3)numeric intger,real\float
4)structure array,union,structure,class..
i hope you got the answer what actually the pointer is!!
thanks
Strictly speaking, a pointer is just a number (like anything else in a computer). It's higher level languages that make things more complicated.
A pointer just points to a piece of memory. Nothing more. Attaching an identifier to it just makes life easier for the developer.
A pointer is an integer-type variable whose value is understood to be a memory address. And, by convention, the value zero is NULL (or nil) – an indication that the value of the pointer is not significant; that "it doesn't point to anything at all."
I don't consider it to be a "data type" since it is the means to an end. It is not something of particular interest, except that it is the address of something that is interesting to somebody. It is the means of obtaining the address of "the data."
Last edited by sundialsvcs; 08-04-2017 at 08:51 AM.
it is a data type, just it can be converted to and from int means not that it is an in
this
Code:
int* a;
int b;
a++ ;
b++ ;
does 2 total different things because integer and pointer are not of the same type
C++ did it's best to at least fix on problem with C++11
Code:
void foo(int);
void foo(bar*);
foo(NULL) ; // does not work , ambiguous because of implicit conversion -> BAD!
foo(nullptr) ; // works sind nullptr_t is a type
fortunately, in modern languages, lice C++ , raw pointers are not needed anymore and can be implemented as strong types that avoid the horrible and unsafe implicit conversion topic totally
example https://a4z.bitbucket.io/blog/2017/0...y-toolbox.html
seeing pointers as what they are, types, and use them like this, avoids lots of the common mistakes that have lead, and still leads, to a lot of expensive problems with the pointer is a int approach
C is a language. In any language, certain words have fixed meanings irrespective of what one "considers them to be" (Donald Trump and the Cheshire Cat tend to disagree, of course). In C, either K&R or ANSI, pointers are data types: thus, int* and int** are not only data types but different data types. The fact that both are stored as integers representing virtual addresses, is irrelevant.
A null pointer is a special case: its value is conventionally zero and it is automatically converted to whatever is the relevant type. Without strong typing, as in original versions of C, it was possible to assign an integer value to a pointer variable. This was useful when the addresses were real memory addresses, for example to access memory mapped I/O, no longer necessary or indeed possible with virtual addresses.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.