473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why pointers??

Why use pointers at all??

Sep 21 '06
62 4932
In article <is********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>>>>Why use pointers at all??
>>>How else would you point to things?
>>Well you could let that be the compiler's problem, like it is in many
other languages.
>Huh? You mean languages where its not possible to point at things,
like Basic ? There's a reason Basic didn't take over the world.
I'll treat your question seriously. I wasn't really thinking of
Basic.

What's the difference between variable in C containing a pointer to a
structure, and one in Java or Lisp whose value is an object? All of
them are an abstraction of the address of the object. In languages
like Java and Lisp almost everything is in that sense a pointer without
any explicit action by the user.

But we don't usually call the Java and Lisp ones "pointers". C's
pointers are much closer to the machine addresses: in particular you
can do arithmetic on them, while the Java and Lisp versions are quite
opaque. You can confine yourself to a subset of C that only uses
pointers in the way that Java and Lisp do - to pass around references
to structures and (implicitly) for array operations.

So to return to the orignal question, you *have* to use pointers in
C to get what other languages provide without explicit pointers, but
taking advantage of the extra things you can do with pointers in C
is entirely optional.

-- Richard
Sep 23 '06 #51
Spiros Bousbouras wrote:
>
.... snip ...
>
But BASIC has peek and poke so in a sense you can
point at things. Your pointers are simply integers
which hold addresses.
No they aren't. In some systems they may be.

if ((sizeof(int) != sizeof(int*))
puts("Certainly not on this machine");
else puts("Could be, not guaranteed");

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 23 '06 #52
CBFalconer <cb********@yahoo.comwrites:
Spiros Bousbouras wrote:
>>
... snip ...
>>
But BASIC has peek and poke so in a sense you can
point at things. Your pointers are simply integers
which hold addresses.

No they aren't. In some systems they may be.
I think Spiros was referring to BASIC (i.e., the integer arguments you
pass to peek and poke are the closest thing BASIC has to pointers).

There are a number of different versions of BASIC, and I haven't used
any of them in decades; his statement is plausible, but I can't
comment on whether it's accurate.
if ((sizeof(int) != sizeof(int*))
puts("Certainly not on this machine");
else puts("Could be, not guaranteed");
He said "integers", not "ints". If pointers are bigger than ints, but
are represented the same way as unsigned longs, then you could almost
reasonably say that pointers are "simply integers that hold addresses"
-- though of course it's a bad idea to do so if you're talking about
standard C (as opposed to the innards of some particular
implementation).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 23 '06 #53
Richard Tobin wrote:
[...]
What's the difference between variable in C containing a pointer to a
structure, and one in Java or Lisp whose value is an object? All of
them are an abstraction of the address of the object. In languages
like Java and Lisp almost everything is in that sense a pointer without
any explicit action by the user.
Then we have the languages between C and Java in high-levelness. In
Oberon for instance, pointers are explicit but restricted. Raw pointers,
when needed, are available through the SYSTEM module.
August
Sep 23 '06 #54

Tom St Denis skrev:
Richard Heathfield wrote:
onkar said:
Why use pointers at all??
No reason at all, if you don't want to. So don't bother, until a need for
them arises.

Explain to me how you'd use memory mapped I/O then?

Tom
make a struct/array that looks the like the memory mapped device and
make the linker place it on the rigth adress

-Lasse

Sep 23 '06 #55
Old Wolf wrote:
Peter Nilsson wrote:
Conformance is generally limited to two things: issuing _at least
one_ diagnostic for constraint violations, and the correct semantic
implementation of strictly conforming programs (subject to
implementation limits.)

Code that is not strictly conforming may or may not require a
diagnostic. If it does, then a conforming compiler must issue it.

But whether such code is accepted, compiled, linked, etc... is
completely at the implementation's discretion. That is the freedom
that undefined behaviour allows.

It also has to compile conforming (but not strictly conforming)
programs that are don't have any syntax errors or constraint violations.
Note that the language doesn't state what 'compiling' is. That is
simply
a term commonly used to describe a collection of later translation
phases. There is nothing that I can see in the standard that states
that
a non strictly conforming translation unit must 'pass' a number of
translation phases before an implementation is allowed to reject it.

--
Peter

Sep 24 '06 #56
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
Spiros Bousbouras wrote:
>
... snip ...
>
But BASIC has peek and poke so in a sense you can
point at things. Your pointers are simply integers
which hold addresses.
No they aren't. In some systems they may be.

I think Spiros was referring to BASIC (i.e., the integer arguments you
pass to peek and poke are the closest thing BASIC has to pointers).
Yeah , when I said "Your pointers are simply integers
which hold addresses" I meant in the context of BASIC
ie you would use BASIC's integer types to hold addresses
and that would make them "pointers".
>
There are a number of different versions of BASIC, and I haven't used
any of them in decades; his statement is plausible, but I can't
comment on whether it's accurate.
if ((sizeof(int) != sizeof(int*))
puts("Certainly not on this machine");
else puts("Could be, not guaranteed");

He said "integers", not "ints". If pointers are bigger than ints, but
are represented the same way as unsigned longs, then you could almost
reasonably say that pointers are "simply integers that hold addresses"
-- though of course it's a bad idea to do so if you're talking about
standard C (as opposed to the innards of some particular
implementation).
In a BASIC which has poke and peek you can use BASIC's
integers to refer to memory addresses. There's no pointer
type separate from integers , you just treat integers as
pointers. (Actually for all I know there may be BASIC dialects
where you have an innate pointer type but I wasn't thinking
of those)

On second thought though whether you have poke and peek is
irrelevant. You can define a large integer 1-dimensional
array and you will treat it as memory. Indices in the array
will be your pointers. But then creating a pointer to pointer
will be problematic.

I guess C is better after all ;-)

Sep 24 '06 #57
"Spiros Bousbouras" <sp****@gmail.comwrites:
[...]
In a BASIC which has poke and peek you can use BASIC's
integers to refer to memory addresses. There's no pointer
type separate from integers , you just treat integers as
pointers. (Actually for all I know there may be BASIC dialects
where you have an innate pointer type but I wasn't thinking
of those)

On second thought though whether you have poke and peek is
irrelevant. You can define a large integer 1-dimensional
array and you will treat it as memory. Indices in the array
will be your pointers. But then creating a pointer to pointer
will be problematic.

I guess C is better after all ;-)
Agreed, but pointers to pointers wouldn't be a problem. A pointer
variable would be an integer whose value is an array index. A pointer
to pointer would be an integer whose value is an array index, where
the specified array element contains an array index.

You can do the same thing in C. It could even be useful, if you want
to represent a dynamic data structure (e.g., a binary tree) in a way
that can easily be written to an external file, without having to
worry about maintaining pointer representations.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 24 '06 #58

onkar wrote:
Why use pointers at all??
Possible reasons:

1) with a declaration like "int a;" you
allocat memory on the stack. When you use a
pointer and the malloc-function you allocate memory on
the heap. Allocation memory on the stack is
usually faster than on the heap, but the stack size
is limited.

2) when you declare a function like foo (int a),
the value will be copied into the new stackframe when
you call the function foo. To copy a value in the memory
is slow. It is faster when you point to the memory area where
the value is located (foo (int *a)).

Sven

Sep 25 '06 #59

onkar wrote:
Why use pointers at all??
1. To allow a function to modify its arguments (technically, what its
arguments are pointing to);
2. To track dynamically allocated memory;
3. To reduce redundancy (i.e., instead of multiple instances of the
same data, have one instance with multiple pointers to it);
4. To easily build dynamic structures such as lists, trees, and queues
(ties in to #2);
5. To use functions and objects in a dynamically loaded library;

And a few others I can't think of off the top of my head. You'll know
it when you see it.

Sep 25 '06 #60

Richard Heathfield wrote:
lovecreatesbea...@gmail.com said:
onkar wrote:
Why use pointers at all??
If the arguments of a function should be modified

You can't modify a function's arguments. You can, however, modify the value
of a function parameter.
The function parameters might not be changeable always, for example
when they are qualified with const keywords.
then the addresses of
them

You can't take the address of an argument. Arguments are expressions, and
expressions in the general case do not necessarily have addresses.
should be passed into the function and the corresponding
parameters should be declared as pointers of type same as the arguments.

What you're trying to explain is /how/ to use a pointer in a function call
to facilitate the modification of an object's value within that function,
Thank you for the correction.
but the question is /why/ you would want to do that.
Sep 27 '06 #61
lovecreatesbea...@gmail.com said:
>
Richard Heathfield wrote:
>lovecreatesbea...@gmail.com said:
onkar wrote:
Why use pointers at all??

If the arguments of a function should be modified

You can't modify a function's arguments. You can, however, modify the
value of a function parameter.

The function parameters might not be changeable always, for example
when they are qualified with const keywords.
That is indeed true, but extremely irrelevant to the point I was making.
Similarly, one might say "you can't drive a garage, but you can drive a
car", only to be met with the objection "you can't drive a car if it has no
fuel".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 27 '06 #62
In article <6J********************@bt.com>,
Richard Heathfield <in*****@invalid.invalidwrote:
....
>That is indeed true, but extremely irrelevant to the point I was making.
Similarly, one might say "you can't drive a garage, but you can drive a
car", only to be met with the objection "you can't drive a car if it has no
fuel".
Ah, but you *can* drive a car if it has no fuel. You just won't get
very far.
Sep 27 '06 #63

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.