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.
Well, NevemTeve has introduced an interesting point of semantics. In fact argv is a formal parameter of data type char**; at run time it is an actual parameter. that is a variable whose value is a pointer to an array of pointers to character arrays, composed of the strings supplied by the call. This might loosely be described as a data structure, but is not a C struct. In a similar way, one tends to say that, for example, 4.32 is a float, which is actually shorthand for "a constant of data type float".
Incidentally, in K&R C a variable defined as a struct had a value which was a pointer to the structure, mimicking arrays. This was for efficiency, since passing a large structure to a function meant a large overhead in both time and memory at that time. When this was changed to the structure itself, it meant that there were two sets of incompatible compilers, since in one, if a formal parameter was declared as a struct, a pointer was expected, in the other, the object itself.
Last edited by Peverel; 08-05-2017 at 03:01 PM.
Reason: mistaken attribution
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."
hey my friend,
bro never use that statement henceforth pointer is not a memory its not a debatable topic as it can never be a memory. its juts a variable that points to a memory location. it can have its own memory location can point to a memory location but cannot be a memory location.
hope you know what i am trying to convey. what i can say that you know what exactly pointer is but the term you used is not appropriate
thanks for the reply.
Well, NevemTeve has introduced an interesting point of semantics.....
not really
don't get confused, or let confuse you
a pointer to a pointer is just a pointer, to a pointer ;-)
this can be chained as deep as you like
Code:
#include <stdio.h>
int main(void) {
char* a = "hello" ;
char** b = &a ;
char*** c = &b ;
char**** d = &c ;
printf ("%s \n", ***d);
return 0;
}
pointers to pointers are not uncommon i C as function arguments, and not only used for arrays.
in case of argv it is just a pointer to an array, this can be written
Code:
char *argv[]
//or
char **argv
since C allows this.
C was never evolved to a language the removes its flaws, it was invented to solve a problem, than it was there, than the creators moved on to other problems, and today we have to live with this kind of a technical debt.
In my usage of the term, data structure, I specifically did not equate it to "a struct." In fact, this interpretation would be fundamentally incorrect.
A "data structure" is any arrangement of data – in memory or otherwise – that is done to facilitate the computer's subsequent access of it. The "things" which comprise that structure are sometimes, but not always, multi-byte "things" which are most-conveniently described as structs.
struct is a semantic device, found in nearly every programming language, which is used to describe a grouping of related variables which occupy a necessarily-contiguous group of bytes. Sometimes, more than one struct definition is applied to the same group of bytes, and the language has no means to know which of these is "correct."
I would consider a struct to be, shall we say, merely a "data block," because it is a single thing ... "structured within itself," yes, but (perhaps) only part of a "data structure." structs are customarily found as the building-blocks of a data structure.
Last edited by sundialsvcs; 08-07-2017 at 08:55 PM.
Let me repeat myself again: (without context) the term 'pointer' might refer to a type, a value, a variable or a constant; e.g. char *, (void *)0x1, environ, main, "literal"
Let me repeat myself again: (without context) the term 'pointer' might refer to a type, a value, a variable or a constant; e.g. char *, (void *)0x1, environ, main, "literal"
A pointer is an integer-type variable whose value is understood to be a memory address (with zero meaning "NULL") and that address can be the address of anything you please. The language has no idea whether the address is sensible or correct: the computer will do precisely as it is told.
'main' for example, is a pointer, so is "literal" but neither of them is variable.
main() is a function name. But, basically, you're allowed to determine the address of anything and to put that address into a pointer-type variable. The "C" language will basically do what you tell it to do. (Which is why they use "C" to build interpreters and runtime-environments that are much more sophisticated in this regard ...)
In K&R's "The C Programming Language," 2nd Edition, void * is specifically called a type.
On page 83
Quote:
In addition, the type void * (pointer to void ) replaces char * as
the proper type for a generic pointer.
Additionally, pointers are listed as one of several derived types.
Page 173
Quote:
A.4.3 Derived types
Beside the basic types, there is a conceptually infinite class of derived types constructed from
the fundamental types in the following ways:
arrays of objects of a given type;
functions returning objects of a given type;
pointers to objects of a given type;
structures containing a sequence of objects of various types;
unions capable of containing any of one of several objects of various types.
Further, it states the pointers are variables.
Page 84
Quote:
Finally, since pointers are variables, they can be used without dereferencing.
So pointvariable++ is valid, as are pointervariable + constantintegervalue, and pointervariable - pointervariable.
All variables are of one or another type.
Pointers are not defined as equivalent to an int because the size of pointers can vary from one processor to another, and on some machines there are several different sizes of pointers. And the size of int is different on different architectures.
Dereferencing is an operation done on a type. The operators related to pointers are * and &. They are used in expressions and are more tightly bound than arithmetic operators. Operators act upon data and expressions, and both data and expressions have types. so *(functionname()) is valid expression. However, &(functionname()) is NOT valid. The & operator only takes an lvalue.
So, you can see that a pointer is a data type while * and & are operators.
I forgot to add that the compiler translates for us when we perform arithmetic on pointers. If we increment a pointer to any type, the pointer's value is incremented by the size of that type. This is very handy for stepping through arrays.
add an integer to a pointer, such that adding one to a pointer actually increments the pointer by the size of the type the pointer is pointing to, i.e. a pointer to uint64_t would be incremented by 8 bytes, a pointer to char would be incremented by one byte, a pointer to a struct would be incremented by the size of that struct as it is represented in memory.
In Using the GNU Compiler Collection GCC 4.8.1 (I don't have the manual for other versions), math on the type void* is allowed, and the increment for that is one byte. You can find that information on page 342.
I have to say, there are some very mean posts in this thread! Those insulting posts are just sad. Why get angry over a disagreement which can be settled by looking it up? Why? What for? A careful referral to the relevant literature is all it takes. Someone else stated that K&R C doesn't say that a point is a type and I disagreed, instead of getting angry, I carefully looked it up. And so the other guy was mistaken, so what? Big deal.
I'm very glad someone found my post useful but that doesn't make me feel like a big shot or like I'm somehow better than someone who didn't know. I didn't know until the initial question motivated me to look it up.
Last edited by DeeDeeK; 09-08-2017 at 05:08 PM.
Reason: Added some info, editorialized about mean and insulting posts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.