473,769 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the Point of Pointer's

I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!
Nov 14 '05 #1
20 1654
In article <yv************ ********@comcas t.com>,
Bill Potter <lo********@com cast.net> wrote:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


The same reason why we use pointers in human languages, e.g.
"home" instead of "17859 Main Street"
"the car behind us" instead of "car with registration no KN5567YZ"
"you" instead of "Bill Potter"

--
Göran Larsson http://www.mitt-eget.com/
Nov 14 '05 #2
Bill Potter wrote:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


Let's say you're shopping for a car at a car dealership and the dealer
asks you which car you like, what do you do?

1. __Point__ at a couple of cars...

or

2. Go to each car, and physically move each of them directly in front
of the dealer?
Probably #1, if you're at all concerned about efficiency. You can refer
to more cars quicker by pointing at them than moving them each directly
in front of you, one by one.

Does that help?
Mark F. Haigh
mf*****@sbcglob al.net
Nov 14 '05 #3

"Bill Potter" <lo********@com cast.net> wrote in message
news:yv******** ************@co mcast.com...
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


for many reasons! 2 simple ones:

1) when passing a large structure to a function, we dont want to copy every
element and slow things down to a snail-pace. So pass a pointer instead,
which is very quick.

2) what if you want some information to be passed to a function and that
function modifies some data. For example a sort function. We pass a
pointer to an array to the function and let it do all the work.

Basically, pointers speed things up. They also allow dynamic memory
allocation using malloc() or equivalent. Things start to get more advanced
when you have pointers to pointers ;-)
Allan
Nov 14 '05 #4
Bill Potter writes:
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


In C - as distinct from C++ - using a pointer is the *only* way a called
function can alter the contents of a variable in the calling function.
Parameters are passed in C in what is called "pass by value", that is, a
*copy* is passed to the callee.
Nov 14 '05 #5
>> I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


In C - as distinct from C++ - using a pointer is the *only* way a called
function can alter the contents of a variable in the calling function.
Parameters are passed in C in what is called "pass by value", that is, a
*copy* is passed to the callee.


In fact, C++'s references could be taken as a syntactic modification of The
Pointer, e.g.:

C> void callee(int *ptr) {
C> ++*ptr;
C> }

C++> void callee(int &ptr) {
C++> ++ptr;
C++> }
Jan Engelhardt
--
Nov 14 '05 #6
"Goran Larsson" <ho*@invalid.in valid> wrote in message
news:I1******** @approve.se...
In article <yv************ ********@comcas t.com>,
Bill Potter <lo********@com cast.net> wrote:
I am a learning programmer in C and i want to know why some one would use pointers instead of going direct!


The same reason why we use pointers in human languages, e.g.
"home" instead of "17859 Main Street"
"the car behind us" instead of "car with registration no KN5567YZ"
"you" instead of "Bill Potter"


Actually "17859 Main St" is a pointer as well; the house itself is not.
Obviously every time you invite someone over, you'd rather give them a
pointer to your house instead of building a copy and handing it to them.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

Nov 14 '05 #7
Bill Potter wrote:

I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!

Look into a typical linked list implementation.


Brian Rodenborn
Nov 14 '05 #8
In article <U2************ *******@cyclops .nntpserver.com >,
Stephen Sprunk <st*****@sprunk .org> wrote:
Actually "17859 Main St" is a pointer as well; the house itself is not.
No. "17859 Main St" is the name of the house (variable).
Obviously every time you invite someone over, you'd rather give them a
pointer to your house instead of building a copy and handing it to them.


No. You give them the name of the house. The problem starts when you
move and they continue to visit the hose by its name. The pointer "home"
should have been used instead.

--
Göran Larsson http://www.mitt-eget.com/
Nov 14 '05 #9
"Bill Potter" <lo********@com cast.net> wrote in message news:<yv******* *************@c omcast.com>...
I am a learning programmer in C and i want to know why some one would use
pointers instead of going direct!


Pointers are direct. That's the point!
Nov 14 '05 #10

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

Similar topics

6
2579
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
140
7899
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
23
7751
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
11
3417
by: J Wang | last post by:
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv) { struct stat buf;
51
4557
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
5
1941
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is relevant to my question... Which is: What is the (*vector) in the "struct function" declaration. What is that line doing? My best guess is that it is a function definition that returns a struct
16
10395
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
4
6696
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
12
2570
by: Michael Bell | last post by:
I am trying to put my learning effort into the most useful things. What do pointers do that other things can't? Michael Bell --
19
2392
by: Eric | last post by:
What is this? ( char * ) &( ( struct aStruct * ) 0 ) It looks like it's taking the address of something that contains a pointer to null, and casting it to a pointer to char. (Actually, technically, the address of a pointer to the beginning of a struct aStruct, that starts at address 0).
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10198
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.