473,320 Members | 1,572 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,320 software developers and data experts.

about pointer

hai,
any can tell what is the advantge of using pointers?any increase in
speed or execution time?
if you know please tell what are the real time applications of data
structure?
thanks in advance

Nov 16 '05 #1
24 2068
System programming, I guess. When I had to deal with
that level in the early 70s, the only choices were Fortran IV and
assembly. In assembly pointers are natural, but were missing
from Fortran except for vendor specific extensions such as %LOC.
C began as a "high level assembly language" and still is.

Nov 16 '05 #2
More specifically, many computers of that period (late 60s ->
mid 70s), during which C emerged, had "load indirect"
assembly instructors. For example (Univac 11xx)

LA,U A1,R4

loads into the A1 register the contents of the word address
currently in register R4, so "R4" can be thought of as a pointer.
The idea of C was to associate symbolic names with these
operations instead of specific register names, and let the
compiler do the translations.

Nov 16 '05 #3
On Tue, 15 Nov 2005 19:21:11 -0800, venkatesh wrote:
any can tell what is the advantge of using pointers?


You can use strings :P

And a number of more complicated data structures than strings too, of
course.
Daniel
Nov 16 '05 #4
venkatesh wrote:
any can tell what is the advantge of using pointers?any increase in
speed or execution time?
if you know please tell what are the real time applications of data
structure?


it's more about expressivenes than speed. This sort of
micro-optimisation is generally
unimportant with modern compilers.

Uses for pointers:-
1. C always passes by value the only way to get the effect of pass by
reference is by using pointers.
2. certain data structures are expressed most naturally using pointers.
Eg. trees and linked lists
3. certain algorithms are more clearly expressed using pointers
4. the only way a variable can refer to a function is by using a
pointer to a function

In short don't use pointers "for efficiency" but for clarity.
--
Nick Keighley

"premature optimisation is the root of all evil"

Nov 16 '05 #5

"venkatesh" <pv***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
hai,
any can tell what is the advantge of using pointers?any increase in
speed or execution time?
if you know please tell what are the real time applications of data
structure?


One case where they're useful, in terms of efficiency is where structures
are passed to functions.

Normally, as C only has pass by value, a copy of the structure has to be
made, the copy is what's passed to the function - obviously, if the
structure is large, this takes time (well, it *takes time* even if it's
small of course). So, you could elect to pass structures *by address*
instead.

The down side of that is that the called function can now crap on your
structure, whereas before, it could only crap on a copy.
Nov 16 '05 #6

venkatesh wrote:
hai,
any can tell what is the advantge of using pointers?any increase in
speed or execution time?
if you know please tell what are the real time applications of data
structure?
thanks in advance


Pointers are necessary for a number of operations:

1. If you want to write to a function parameter and have that change
reflected in the caller, you must use pointers:

void swap_wrong(int a, int b)
{
int tmp = b;
b = a;
a = tmp;
}

void swap_right(int *a, int *b)
{
int tmp = *b;
*b = *a;
*a = tmp;
}

int main(void)
{
int x = 1, y = 2;
printf("before swap_wrong: x = %d, y = %d\n", x, y);
swap_wrong(x, y);
printf("after swap_wrong: x = %d, y = %d\n", x, y);
swap_right(&x, &y);
printf("after swap_right: x = %d, y = %d\n", x, y);
return 0;
}
2. The only way to track dynamically allocated memory is through a
pointer:

char *newString = malloc(newStringSize);

3. The only way to create struct types that refer to instances of
themselves is to use a pointer:

struct tree {
int value;
struct tree *left;
struct tree *right;
};

4. If you want to associate specific behaviors with specific data, you
can use pointers to functions:

/**
* Routines to parse data and calibration files for various
* scientific instruments
*/
int parseGraDat(char *fileName) {...}
int parseGraCal(char *fileName) {...}
int parseMstDat(char *fileName) {...}
int parseMstCal(char *fileName) {...}
int parsePwvDat(char *fileName) {...}
int parsePwvCal(char *fileName) {...}

/**
* Lookup table type to associate parse functions
* with file types and extensions
*/
struct parserLookup {
char *instrumentType;
char *extension;
int (*parser)(char *fileName);
};

/**
* Lookup table instance
*/
struct parseLookup lookupTable[] = {
{"GRA", "dat", parseGraDat},
{"GRA", "cal", parseGraCal},
{"MST", "dat", parseMstDat},
...
};

...
for (file = getFirstFileName(); file != NULL; file =
getNextFileName())
{
int i = getLookupIndex(lookupTable, file);

if (i >= 0)
{
if ((*lookupTable[i].parser)(file) != 1)
{
printf("Error parsing %s\n", file);
}
}
}

5. All array types are converted to pointer types if the array appears
as a function parameter:

int foo(int *arr) {...}

int main(void)
{
int bar[10];
...
if (foo(bar)) {...}
...
return 0;
}

Technically, all array types are converted to pointer types if the
array appears in any context other than an array definition or as a
sizeof operand (I think there's one more that I'm forgetting).

Pointers *can* offer some optimization; instead of passing large
structs as function parameters, you can pass a pointer to the struct,
saving some overhead.

Nov 16 '05 #7
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.

Nov 16 '05 #8
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.

Happy are those who answer their own questions...

No, pointers are not necessary. Functions are not necessary either. Both
have their uses, though.

Many imperative languages that do not have pointers have references --
that includes Algol 68. (And, incidentally, C is an Algol descendant
too, if not a direct one.)

Pointers as used to access random parts of memory (whether belonging to
declared objects or not) do not have use outside systems programming.
References (which pointers can implement) are useful in a general
algorithmic context.

S.
Nov 16 '05 #9
ca****@colorado.edu writes:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


If you want to do any but the simplest programming in C, you
pretty much have to use pointers. Other languages may not have
them, but they usually have other facilities that substitute.

I'm not sure what you mean by "the Algol descendants" here. C is
an Algol descendant. So is Pascal. Both have pointers. Also,
any superset of C would have everything that C has, so "C
supersets" have pointers.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Nov 16 '05 #10
"Ben Pfaff" writes:

Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


If you want to do any but the simplest programming in C, you
pretty much have to use pointers. Other languages may not have
them, but they usually have other facilities that substitute.

I'm not sure what you mean by "the Algol descendants" here. C is
an Algol descendant. So is Pascal. Both have pointers. Also,
any superset of C would have everything that C has, so "C
supersets" have pointers.


I took it that he meant Algol 68.
Nov 16 '05 #11
> *Also, any superset of C would have everything that C has, so "C
supersets" have pointers.


Wrong choice of words. I should had said C-family languages, such
as Java. I dont know if Eiffel qualifies in this class.

Nov 16 '05 #12
In article <11*********************@g43g2000cwa.googlegroups. com>
<ca****@colorado.edu> wrote:
Are pointers necessary beyond system programming?


Are arrays necessary? Instead of op(a[i]) you can always write:

switch (i) {
case 0: op(a0); break;
case 1: op(a1); break;
case 2: op(a2); break;
...
case 999: op(a999); break;
}

Obviously, then, arrays are not necessary.

They sure are convenient though. Of course, if you write:

op(a[i]);

and "i" is not a valid index into the array, things could go wrong.

Are pointers necessary? Instead of op(*p) you can always write:

switch (integer_substitute_for_p) {
...
}

Obviously, then, pointers are not necessary.

They sure are convenient though. Of course, if you write:

op(*p);

and p is not a valid pointer, things could go wrong.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 16 '05 #13
ca****@colorado.edu wrote:
�Also, any superset of C would have everything that C has, so "C
supersets" have pointers.

Wrong choice of words. I should had said C-family languages, such
as Java. I dont know if Eiffel qualifies in this class.

No, it does not. Eiffel takes cues from Algol, Ada and Pascal, and does
not resemble C/C++ at all.

S.
Nov 16 '05 #14
In the last analysis all you need are 0s and 1s. (Seymour
allegedly programmed the 6600 directly in octal - lucky guy)
In some locales, though, you have only 0s available - then
you would need big-endian 0s and little endian 0s.

Nov 16 '05 #15
venkatesh wrote:
hai,
any can tell what is the advantge of using pointers?any increase in
speed or execution time?
if you know please tell what are the real time applications of data
structure?


There are very few data structures that do not involve the use of
pointers. Almost all useful C programs use some values of pointer type.
The C language is designed around pointers. Without them, it is severely
crippled.

Does that answer your question? If not, give a more specific question!

--
Simon.
Nov 16 '05 #16

ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


Anything involving memory management involves pointers of some form,
and memory management comes into play all over the place, not just in
systems programming.

Regardless of whether they're exposed with special syntax/semantics as
in C and C++ or hidden behind reference types as in Java, pointers are
necessary for a wide variety of operations. A host of basic data
structures such as trees and lists rely on the ability to reference
arbitrary memory locations.

Nov 16 '05 #17
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.
Many imperative languages that do not have pointers have references --
that includes Algol 68.


In "A Practical Guide to Algol 68" (Frank G. Pagan, 1976),
the section that discusses 'ref' has the heading

6.2.1 Pointers and Casts

and the term "pointer" is used throughout that section.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 17 '05 #18
Walter Roberson wrote:
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.


Many imperative languages that do not have pointers have references --
that includes Algol 68.

In "A Practical Guide to Algol 68" (Frank G. Pagan, 1976),
the section that discusses 'ref' has the heading

6.2.1 Pointers and Casts

and the term "pointer" is used throughout that section.


But they *are* references, not pointers, in modern parlance, even if
back in 1976 this distinction was not made. Correct me if I'm wrong, but
I do not believe Algol 68 has pointers in the C sense -- things you can
point to arbitrary memory locations, perform arithmetic on, cast to and
from all sorts of types, and allowing dereferencing only on the
programmer's responsibility that it makes sense at all.

S.
Nov 17 '05 #19
John Bode wrote:
ca****@colorado.edu wrote:
Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.

Anything involving memory management involves pointers of some form,
and memory management comes into play all over the place, not just in
systems programming.

Regardless of whether they're exposed with special syntax/semantics as
in C and C++ or hidden behind reference types as in Java, pointers are
necessary for a wide variety of operations. A host of basic data
structures such as trees and lists rely on the ability to reference
arbitrary memory locations.

I feel a slight reformulation is necessary. Basic data structures rely
on the ability to reference *objects*, not arbitrary memory locations.
(Some advanced data structures may take advantage of various tricks to
save time and space by going low-level on the bits that make up objects,
but these are not general or portable.)

Looking at references as "hiding" pointers is a good way to understand
it coming from C, but it's unnecessary if you're working in the
languages themselves -- references are just references. They reference
objects and different references can reference the same object, so that
operations affect the object as seen from all references to it. That's
about all you need to know; you don't want to think in terms of memory
locations unless you're implementing the language.

S.
Nov 17 '05 #20
Skarmander <in*****@dontmailme.com> writes:
Walter Roberson wrote:
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
ca****@colorado.edu wrote:

Are pointers necessary beyond system programming? There
are languages without them, e.g the Algol descendants and
some C supersets.

Many imperative languages that do not have pointers have references --
that includes Algol 68.

In "A Practical Guide to Algol 68" (Frank G. Pagan, 1976),
the section that discusses 'ref' has the heading
6.2.1 Pointers and Casts
and the term "pointer" is used throughout that section.


But they *are* references, not pointers, in modern parlance, even if
back in 1976 this distinction was not made. Correct me if I'm wrong,
but I do not believe Algol 68 has pointers in the C sense -- things
you can point to arbitrary memory locations, perform arithmetic on,
cast to and from all sorts of types, and allowing dereferencing only
on the programmer's responsibility that it makes sense at all.


I think this is about to devolve into a debate over the meaning of the
term "pointer". That's sensible in the context of a single language,
but it's going to cause confusion if you try to define the term across
languages that have different semantics for similarly-named
constructs. Each language has its own terminology.

(Personally, I'd say that both Algol 68 and C have pointers; C's
pointers are just particularly undisciplined.)

--
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.
Nov 17 '05 #21
Keith Thompson wrote:
[references and pointers]
I think this is about to devolve into a debate over the meaning of the
term "pointer". That's sensible in the context of a single language,
but it's going to cause confusion if you try to define the term across
languages that have different semantics for similarly-named
constructs. Each language has its own terminology.
Oh, poppycock. Languages agree on the general terms that allow effective
comparison, like function, procedure, subprogram, subroutine, routine... :-)
(Personally, I'd say that both Algol 68 and C have pointers; C's
pointers are just particularly undisciplined.)

I have no problem calling references "disciplined pointers", and in my
mind the distinction makes sense across languages, but it wasn't my
intent to start a definition war.

S.
Nov 17 '05 #22
Dear venktash,
Beside dynamic memory allocation, pointers increases the
execution time of a program. You can itself test it by writing programs
such as Fermat and Pythgoras in C and then in java or else.

Nov 17 '05 #23
"Amar Prakash Tripaithi" <tr************@gmail.com> writes:
Dear venktash,
Beside dynamic memory allocation, pointers increases the
execution time of a program. You can itself test it by writing programs
such as Fermat and Pythgoras in C and then in java or else.


That doesn't make any sense. A given algorithm implemented using
pointers *might* be slower than the same algorithm implemented without
pointers, but writing two different implementations is non-trivial,
and any performance difference caused by the use of pointers is likely
to be swamped by other differences. Comparing equivalent programs in
C and Java certainly isn't going to tell you anything about the
performance impact of pointers.

Use pointers when it makes sense to do so, and not when it doesn't.
Get the program working before you worry about micro-optimization.

--
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.
Nov 17 '05 #24
Ben Pfaff <bl*@cs.stanford.edu> writes:
I'm not sure what you mean by "the Algol descendants" here. C is
an Algol descendant. [...]


I would say C is more Algol-influenced than an Algol descendant.
Algol has nested functions, which C emphatically does not. Obviously
there are other differences, but this difference seems essential
rather than incidental.
Dec 3 '05 #25

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
9
by: wongjoekmeu | last post by:
Hello All, I am learning C++ at the moment. Going through the book of SAM of learning C++ in 21 days I have learned about pointers that it is good custome to always initialise them and to use...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.