LinuxQuestions.org
Visit Jeremy's Blog.
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 08-05-2017, 01:10 PM   #61
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 120

Rep: Reputation: 24

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
 
Old 08-06-2017, 12:51 AM   #62
delhiank762
LQ Newbie
 
Registered: Jul 2017
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 08-06-2017, 01:37 AM   #63
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,417

Rep: Reputation: 608Reputation: 608Reputation: 608Reputation: 608Reputation: 608Reputation: 608
Quote:
Originally Posted by Peverel View Post
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.
 
Old 08-07-2017, 08:52 PM   #64
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,515
Blog Entries: 4

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
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.
 
Old 08-24-2017, 01:48 PM   #65
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,567

Rep: Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923
I'm reading Stroustrup's A Tour Of C++, and I think it explains it perfectly. Please allow me to quote:

Quote:
  • A type defines a set of possible values and a set of operations (for an object).
  • An object is some memory that holds a value of some type.
  • A value is a set of bits interpreted according to a type.
  • A variable is a named object.
Keep in mind that those who said that a pointer is "just" a memory address are not correct. Pointers by definition have a type attached to them.

Last edited by dugan; 08-24-2017 at 02:07 PM.
 
Old 08-24-2017, 11:01 PM   #66
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,277

Rep: Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994
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"
 
Old 08-25-2017, 07:48 AM   #67
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,515
Blog Entries: 4

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 08-25-2017, 11:10 AM   #68
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,277

Rep: Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994
'main' for example, is a pointer, so is "literal" but neither of them is variable.
 
Old 08-25-2017, 07:49 PM   #69
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,515
Blog Entries: 4

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
Quote:
Originally Posted by NevemTeve View Post
'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 ...)
 
Old 09-08-2017, 02:09 PM   #70
DeeDeeK
Member
 
Registered: Jan 2016
Distribution: Ubuntu 14.04
Posts: 33

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration