Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 1st, 2008, 11:23 PM   #1
jamie7823
Newbie
 
Join Date: Jun 2008
Posts: 2
Rep Power: 0 jamie7823 is on a distinguished road
When will i ever use pointers?

hello everyone, im new to programming and trying to visualize using pointers. when would i ever use them?
jamie7823 is offline   Reply With Quote
Old Jun 1st, 2008, 11:31 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: When will i ever use pointers?

you can NOT write a C or C++ program of any significance without using pointers. And there are thousands of reasons why you might want to use them.

One example is when you need an array whose size is not known when you write the program. Lets say you need to read a text file into memory. When you write the program you will not know how big the text file is so it is not possible to create an array of the correct size. The solution is to use a pointer and allocate the memory once the program knows the size of the file.

That's only one of a trillion possible examples.
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Jun 2nd, 2008, 12:00 AM   #3
jamie7823
Newbie
 
Join Date: Jun 2008
Posts: 2
Rep Power: 0 jamie7823 is on a distinguished road
Re: When will i ever use pointers?

Wow,

Im trying to grasp that question and several more at the same time. programming is fun but mentally exhausting. Just when you think you understand a concept, It dosnt compile!

Im messing around trying to write a script and something I am trying to understand is how to get a variable inside func_2 and use it as a variable in main().


c Syntax (Toggle Plain Text)
  1. int func_two(x);
  2.  
  3. int main()
  4. {
  5. cout<<func_two(x);
  6. return 0;
  7. }
  8.  
  9. func_two(x)
  10. {
  11. x=2
  12. }

Last edited by Ancient Dragon; Jun 2nd, 2008 at 10:01 AM. Reason: add code tags
jamie7823 is offline   Reply With Quote
Old Jun 2nd, 2008, 12:20 AM   #4
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: olympia,WA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: When will i ever use pointers?

stop what you are and read about the language.

http://www.cplusplus.com/doc/tutorial/
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jun 2nd, 2008, 3:33 AM   #5
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 132
Rep Power: 1 BstrucT is on a distinguished road
Send a message via MSN to BstrucT
Re: When will i ever use pointers?

Quote:
Originally Posted by mrynit View Post
stop what you are and read about the language.

http://www.cplusplus.com/doc/tutorial/
This is good link for you as I have personally worked through it and have done almost all the examples.

Where might you find pointers, and why use them?
Another typical use to add to the millions of uses for pointers, is that they are used to open and close files for writing and reading purposes. Example from Sams teach yourself c in 21 days:

/* Demonstrates the fprintf() function. */
2:  #include <stdlib.h>
3:  #include <stdio.h>
4:
5:  void clear_kb(void);
6:
7:  main()
8:  {
9:      FILE *fp;
10:     float data[5];
11:     int count;
12:     char filename[20];
13:
14:     puts("Enter 5 floating-point numerical values.");
15:
16:     for (count = 0; count < 5; count++)
17:         scanf("%f", &data[count]);
18:
19:     /* Get the filename and open the file. First clear stdin */
20:     /* of any extra characters. */
21:
22:     clear_kb();
23:
24:     puts("Enter a name for the file.");
25:     gets(filename);
26:
27:     if ( (fp = fopen(filename, "w")) == NULL)
28:     {
29:         fprintf(stderr, "Error opening file %s.", filename);
30:         exit(1);
31:     }
32:
33:     /* Write the numerical data to the file and to stdout. */
34:
35:     for (count = 0; count < 5; count++)
36:     {
37:         fprintf(fp, "\ndata[%d] = %f", count, data[count]);
38:         fprintf(stdout, "\ndata[%d] = %f", count, data[count]);
39:     }
40:     fclose(fp);
41:     printf("\n");
42:     return(0);
43: }
44:
45: void clear_kb(void)
46: /* Clears stdin of any waiting characters. */
47: {
48:     char junk[80];
49:     gets(junk);
50: }

This is just one pointer example, but should show the importance and usefullness of using them correctly.

Please take note that pointers can and will most probably be hard to grasp at first, but don't be put off by it.

>BstrucT
__________________
>BstrucT
BstrucT is offline   Reply With Quote
Old Jun 2nd, 2008, 10:08 AM   #6
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: When will i ever use pointers?

Quote:
Originally Posted by jamie7823 View Post
Wow,

Im trying to grasp that question and several more at the same time. programming is fun but mentally exhausting. Just when you think you understand a concept, It dosnt compile!

Im messing around trying to write a script and something I am trying to understand is how to get a variable inside func_2 and use it as a variable in main().


c Syntax (Toggle Plain Text)
  1. int func_two(x);
  2.  
  3. int main()
  4. {
  5. cout<<func_two(x);
  6. return 0;
  7. }
  8.  
  9. func_two(x)
  10. {
  11. x=2
  12. }
The problem with the above code is that you have to declare vairable x in main() then pass a pointer to it to func_two(), like this:
int func_two(int* x);

int main()
{
   int x = 0;
     cout<<func_two(&x);
     return 0;
}

int func_two(int *x)
{
     *x=2
    return *x;
}
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Jun 2nd, 2008, 4:37 PM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Re: When will i ever use pointers?

Quote:
Originally Posted by Ancient Dragon View Post
you can NOT write a C or C++ program of any significance without using pointers. And there are thousands of reasons why you might want to use them.
I don't really agree with this.

My first programming language was Fortran 77, and some of the most significant applications I ever wrote were in that language. Fortran 77 does not support any notion equivalent to pointers.

In C, pointers make some substantial things much easier than other techniques, hence the many thousands of reasons you might want to use them.

That is also true in C++ but, thanks to the standard library, it is quite possible to write substantial programs without knowing anything about pointers (the standard library often uses pointers behind the scenes, but that's implementation detail), and without running into the various mistakes that are possible with pointers. Of course, the trade-off is that it also takes time and effort to learn how to use the standard library effectively.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusion With Pointers and Arrays JawaKing00 C 10 Sep 14th, 2006 7:33 AM
pointers are difficult cjaime C 3 Nov 12th, 2005 12:26 AM
references vs. pointers bl00dninja C++ 3 Apr 30th, 2005 5:21 PM
Pointers in C (Part II) Stack Overflow C 2 Apr 29th, 2005 10:39 AM
Pointers in C (Part I) Stack Overflow C 4 Apr 28th, 2005 7:03 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC