473,404 Members | 2,137 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,404 software developers and data experts.

Pointers with C- a query

Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.
Regards,
s.subbarayan
Nov 14 '05 #1
19 1599
s.subbarayan wrote:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?


Why in the world would anyone want to? Your question is similar to
asking "why don't people just cut off their left arms?".

Nov 14 '05 #2
s.subbarayan wrote:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.


When you pass around objects in a Java program you're actually
passing around 'pointers' (references). In Java you don't write
the * and & as you would do in C because working with 'pointers'
has been built into the language.

I would argue that it is impossible to write well structured C
programs without using pointers. There are several applications
for pointers, which are hard/stupid to implement without:

a. Passing large chucks of data to functions (avoiding making
local copies.
b. Passing data to a function to be modified inside.
c. Various 'object-oriented' programming techniques using function
pointers.
d. Creating variable size data-structures like trees, lists, ...
e. ...

Perhaps we could say that imperative programming languages do well
with an abstract representation of a memory address (which 'is' a
pointer)? Well at least this might result in some interesting
responses. ;-)

Kees alias Case

Nov 14 '05 #3
On 21 May 2004 06:06:01 -0700, s_**********@rediffmail.com (s.subbarayan)
wrote:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
Java is an object-oriented language, and uses the "reference" type to refer
to objects. Primitive types are handled more-or-less the same way as they
are in C. Thus there's a dichotomy in Java: objects gets referred to only
via references (to facilitate performance and garbage collection), while
stand-alone ("naked") primitives are referred to directly by name, as in C,
for performance reasons (note there's no garbage collection for naked
primitives.). Primitives in Java rarely need to be accessed indirectly;
when they do, you in fact have to wrap them in an object and then use a
reference to that object.

C, on the other hand, has only one mechanism to refer to data indirectly,
and that's the pointer. It is the only game in town; it reflects well C's
nature of being "close to the machine", and allows the use of indirection
with or without dynamic allocation, and even with garbage collection if you
take the trouble to set up a custom framework for it. Pointers are the most
general-purpose way to do it, and provided the facilities necessary to make
C the first viable high-level (relatively speaking) systems programming
language. If you modified C to "remove pointers", it wouldn't be C any
longer. C++, for example, provides its own distinct flavor of reference
type, suitable for off-loading a number of things that, in C, have to be
done with pointers. But references aren't a universal replacement for
pointers even in C++.
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?
The potential for pointer bugs is just the price you pay for the low-level
control C gives you. It is essentially a choice (made by /someone/, if not
you personally) regarding what language to use. If you need the greatest
possible "micro-management" of your application's behavior, you end up
using C. If you're willing to pay some price (in terms of either
performance, or learning curve struggles, or code size, or whatever), you
might choose to use a different language such as Java or C++ or Python or
something else.
-leor

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.
Regards,
s.subbarayan


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #4
s.subbarayan wrote:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
Java is not "able to do this without pointers". Anything derived from
Object _is_ represented as a pointer (under a different name) in Java.
If you follow a null reference, you get an exception in Java.
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.
You could make _every_ object a pointer and get rid of the syntactic
differences between "pointer to user defined type" and "user defined
type" in a language similar to C. Just forbid storage class "auto"
for anything that is not a primitive built-in type.
While pointers provide flexibility most bugs are with respect to
pointers. So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?


Forbid pointer arithmetic, add bounds checks to array access? Forbid the
use of types without a specifed (and matching) range as array indices?
Make free() take a pointer to the pointer to be disposed, and keep a
list of all pointer variables in your program so that free can set
the pointer _and all other pointer variables holding the same value_
to a trap representation? Forbid type casts on pointers?

While we are at it: Add explicit preconditions and postconditions
to functions. Add explicit conditions for validitiy to every user
defined type and make the language check those before and after every
use of an object of the user defined type. Make loop variants explicit.
Make loop invariants explicit.

And, last but not least: Change the name of the language. C is a language
that does not make you pay for things you don't need. If you need them,
you can implement them in C, and pay for them.

Kurt Watzka
Nov 14 '05 #5

"s.subbarayan" <s_**********@rediffmail.com> wrote in message
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows > java able to do this with out pointers? All computer programs need to address memory (ie use pointers) internally. C
makes this fairly transparent to the user. Other languages hide the
underlying operation, at the cost of usually making code slower, but with
the advantage that it is less easy to access memory illegally.

You could try writing a "C--" (without pointers). When you try to write real
programs with it you will probably find that you need at the very least an
expandable array, and probably a "reference" scheme as well.
Nov 14 '05 #6
Malcolm wrote:
"s.subbarayan" <s_**********@rediffmail.com> wrote in message
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows > java


able to do this with out pointers?

All computer programs need to address memory (ie use pointers) internally.


Off-topic nit: Alan Turing demonstrated that this is false.

--
Er*********@sun.com

Nov 14 '05 #7
Eric Sosman <Er*********@sun.com> writes:
Malcolm wrote:
All computer programs need to address memory (ie use pointers)
internally.


Off-topic nit: Alan Turing demonstrated that this is false.


Even a Turing machine has a "pointer" as a part of its state: the
current tape position.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #8
On Fri, 21 May 2004 18:51:57 +0100, Malcolm wrote:
You could try writing a "C--" (without pointers).


The name 'C--' is already in use: It's a language even lower-level than C
that is being touted as a compiler backend language.

http://www.cse.ogi.edu/PacSoft/projects/C--/

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #9

In article <cu*************@zero-based.org>, Martin Dickopp <ex****************@zero-based.org> writes:
Eric Sosman <Er*********@sun.com> writes:
Malcolm wrote:
All computer programs need to address memory (ie use pointers)
internally.


Off-topic nit: Alan Turing demonstrated that this is false.


Even a Turing machine has a "pointer" as a part of its state: the
current tape position.


An n-PDA (n > 1) is equivalent to a TM. I don't see any pointer-
equivalent in an n-PDA.

Pointers aren't necessary. They're just very convenient. And to
answer the OP's question: if pointers were removed from C, it would
no longer be C.

--
Michael Wojcik mi************@microfocus.com

HTML is as readable as C. You can take this either way. -- Charlie Gibbs
Nov 14 '05 #10
In <pa****************************@sig.now> August Derleth <se*@sig.now> writes:
On Fri, 21 May 2004 18:51:57 +0100, Malcolm wrote:
You could try writing a "C--" (without pointers).


The name 'C--' is already in use: It's a language even lower-level than C
that is being touted as a compiler backend language.

http://www.cse.ogi.edu/PacSoft/projects/C--/


It's already in use by how many people?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
In <c3**************************@posting.google.com > s_**********@rediffmail.com (s.subbarayan) writes:
I had this following doubt,while java is able to carryon with out
pointers
Not true. It just doesn't call them pointers and doesn't make as flexible
as the C pointers.
why C language cant be modified to remove pointer?


Try to engage your brain and explain the advantages of a C without
pointers. What class(es) of applications would be better addressed by
such a language?

Removing pointers from C simply because newbies have a hard time getting
them right is NOT going to happen: one is supposed to be a C newbie for
a few months and something better than that for the rest of his life as a
C programmer...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
"s.subbarayan" <s_**********@rediffmail.com> wrote in message
news:c3**************************@posting.google.c om...
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
Removing pointers from C is like removing the gas pedal from a car. It'd be
a lot easier to learn, but you wouldn't get anywhere.

References in Java, Perl, etc. are just pointers with garbage collection and
other abstractions to make them "safe". This conflicts with the primary
goal of C, which is to be as close to assembly as possible while remaining
(mostly) portable. Since the machine deals in pointers, any abstraction
above that necessarily means a loss in run-time efficiency.
Jus want to know alternatives to pointers which can be used with
C.
There is no alternative to pointers in a non-trivial C program because
they're a fundamental part of the language. If you want a language that
hides pointers from you, go learn Java or Perl.
While pointers provide flexibility most bugs are with respect to
pointers.


Perhaps among novices, but not among experienced programmers. Being able to
use pointers correctly is a key attribute of someone who really understands
C.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #13
s.subbarayan <s_**********@rediffmail.com> wrote:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.
Regards,
s.subbarayan


You got things wrong: Every object is a pointer in Java. You just don't see
it. Basic types such as int and short are not pointers. But everything else is.

--
Viktor Lofgren, Umea, Sweden
Mainly self-eductated UNIX programmer, running Slackware-current.
Nov 14 '05 #14
mw*****@newsguy.com (Michael Wojcik) writes:
In article <cu*************@zero-based.org>, Martin Dickopp <ex****************@zero-based.org> writes:
Eric Sosman <Er*********@sun.com> writes:
> Malcolm wrote:
>> All computer programs need to address memory (ie use pointers)
>> internally.
>
> Off-topic nit: Alan Turing demonstrated that this is false.


Even a Turing machine has a "pointer" as a part of its state: the
current tape position.


An n-PDA (n > 1) is equivalent to a TM. I don't see any pointer-
equivalent in an n-PDA.


I would consider a top-of-stack position a pointer.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #15

"Dan Pop" <Da*****@cern.ch> wrote in message

Try to engage your brain and explain the advantages of a C without > pointers. What class(es) of applications would be better addressed > by
such a language?

My MiniBasic doesn't use pointers. There is no PEEK or POKE. However it does
have a DIM statement and it is possible to redimension arrays, which of
course calls malloc() internally. Trying an out-of-bounds access will cause
an error.
The advantage of MiniBasic is that it is impossible, barring bugs on my
part, to cause undefined behaviour. The idea is to allow programs to be
extended by anyone who learnt a bit of BASIC from having a microcomputer
back in the olden days.
Nov 14 '05 #16
On Mon, 24 May 2004 14:24:55 +0000, Dan Pop wrote:
In <pa****************************@sig.now> August Derleth <se*@sig.now>
writes:
On Fri, 21 May 2004 18:51:57 +0100, Malcolm wrote:
You could try writing a "C--" (without pointers).


The name 'C--' is already in use: It's a language even lower-level than
C that is being touted as a compiler backend language.

http://www.cse.ogi.edu/PacSoft/projects/C--/


It's already in use by how many people?


Well, the implementors, for a few.

Good point, I suppose: C-- is probably a solution looking for a problem,
at least at this stage. Maybe when seriously complex hardware
architectures filter down to the mass-market PCs (for example, RISC-like
out-of-order execution and VLIW slot-filling), compiler designers will
want to palm off as much of the /real/ hard work to the backend folks,
making intermediate languages more interesting.

August "EPIC challenges to compiler design..." Derleth

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #17

In article <cu*************@zero-based.org>, Martin Dickopp <ex****************@zero-based.org> writes:
mw*****@newsguy.com (Michael Wojcik) writes:
In article <cu*************@zero-based.org>, Martin Dickopp <ex****************@zero-based.org> writes:
Eric Sosman <Er*********@sun.com> writes:

> Malcolm wrote:
>> All computer programs need to address memory (ie use pointers)
>> internally.
>
> Off-topic nit: Alan Turing demonstrated that this is false.

Even a Turing machine has a "pointer" as a part of its state: the
current tape position.


An n-PDA (n > 1) is equivalent to a TM. I don't see any pointer-
equivalent in an n-PDA.


I would consider a top-of-stack position a pointer.


Sigh. I was going to address this proleptically, then didn't out
of laziness.

I can see the argument for calling the current tape position in a
TM a "pointer". It marks one of the positions on the tape as
current. Machine operations can change it.

The top of the stack in a PDA is not a pointer in any useful sense.
What makes a pointer a pointer per se is that it can be changed to
point to a different object. The top of the stack "points" to only
one thing: whatever is currently on top of the stack. "Changing"
this "pointer" has side effects: it's either a pop, which is a
destructive operation, or a push, which has much more limited
semantics than a "move tape" (or equivalently "move tape pointer")
operation.

Calling the top of the stack a pointer in this sense is uninteresting.
It's equivalent to calling any variable a "pointer". (That a stack
can be implemented with a moving pointer is utterly beside the point.)

--
Michael Wojcik mi************@microfocus.com

Every allegiance to some community eventually involves such a fetish,
which functions as the disavowal of its founding crime: is not 'America'
the fetish of an infinitely open space enabling every individual to
pursue happiness in his or her own way? -- Slavoj Zizek
Nov 14 '05 #18
Viktor Lofgren <zw*@eudial.nos--pam.mine.nu> scribbled the following:
s.subbarayan <s_**********@rediffmail.com> wrote:
Dear all,
I had this following doubt,while java is able to carryon with out
pointers why C language cant be modified to remove pointer?Hows java
able to do this with out pointers?
I jus plead sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be good if we are able to either forgo the
pointers or do some sort of modification(I am not aware how,but it can
be considered for research) so that our s/w s are more pure with out
bugs?

I am sorry for my amature query,but it will be helpful and I will be
thankful to all who makes me understand this.
You got things wrong: Every object is a pointer in Java. You just don't see
it. Basic types such as int and short are not pointers. But everything else is.


Your terminology is lax. Objects are not pointers or vice versa. Every
object *variable* is really an object *pointer* variable in Java, but
the things those variables' values point at are real, honest-to-gosh,
all-signing, all-dancing objects. How do you suppose pointers with
nothing to point at could ever be of any use?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
Nov 14 '05 #19
Michael Wojcik wrote:
In article <cu*************@zero-based.org>, Martin Dickopp <ex****************@zero-based.org> writes:
mw*****@newsguy.com (Michael Wojcik) writes:

In article <cu*************@zero-based.org>, Martin Dickopp <ex****************@zero-based.org> writes:

Eric Sosman <Er*********@sun.com> writes:
>Malcolm wrote:
>
>>All computer programs need to address memory (ie use pointers)
>>internally.
>
> Off-topic nit: Alan Turing demonstrated that this is false.

Even a Turing machine has a "pointer" as a part of its state: the
current tape position.

An n-PDA (n > 1) is equivalent to a TM. I don't see any pointer-
equivalent in an n-PDA.
I would consider a top-of-stack position a pointer.

Sigh. I was going to address this proleptically, then didn't out
of laziness.

I can see the argument for calling the current tape position in a
TM a "pointer". It marks one of the positions on the tape as
current. Machine operations can change it.


Rhetorical question: If you were building a Turing machine,
how many bits would you use to hold the value of this "pointer?"
(Hint: The tape has a countably infinite number of cells.)

Rhetorical question: Since Turing himself allocated zero bits
of his conceptual machine to designate the position of "HERE,"
how many different values can his "pointer" take on? (Hint:
What is the Shannon entropy of a constant signal?)
Calling the top of the stack a pointer in this sense is uninteresting.
It's equivalent to calling any variable a "pointer". (That a stack
can be implemented with a moving pointer is utterly beside the point.)


Exactly -- just as in the case of "HERE" in a Turing machine.

--
Er*********@sun.com

Nov 14 '05 #20

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

Similar topics

9
by: Neil Zanella | last post by:
Hello, Some programmers like to use a coding convention where all names of variables that are pointers start with the letter p (and sometimes even use similar conventions for strings and other...
2
by: DirtyClamDigger | last post by:
Hi Everyone: I'm trying to develop a property list to include as metadata about my object classes. i.e. I want each class I'm developing to include a PropertyList which will contain ObjectProperty...
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
10
by: dbz | last post by:
Hello everyone. I have a query. Lets say that following is given:- struct struct1{ char *word; int n; } *p; QUERIES: What does the following refer to?
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
16
by: ajinkya.coep | last post by:
What's wrong with this program? If you were to fix it, what would the intended output be? void swap(char *str, int index1, int index2) { char tmp = str; str = str; str = tmp; } int...
158
by: madhawi | last post by:
This question is occur in interview. Please help me.
22
by: ravi | last post by:
Hi all, I m relatively new to C. I have few queries related to malloc(): 1. When we perform malloc(), the memory allocated dynamically comes from the heap area of the process in concern. Well,...
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.