![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2008
Posts: 2
Rep Power: 0
![]() |
When will i ever use pointers?
hello everyone, im new to programming and trying to visualize using pointers. when would i ever use them?
|
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 545
Rep Power: 4
![]() |
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.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2008
Posts: 2
Rep Power: 0
![]() |
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)
Last edited by Ancient Dragon; Jun 2nd, 2008 at 10:01 AM. Reason: add code tags |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Re: When will i ever use pointers?
__________________
i dont know much about programming but i try to help |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 194
Rep Power: 1
![]() |
Re: When will i ever use pointers?
Quote:
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
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle. |
|
|
|
|
|
|
#6 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 545
Rep Power: 4
![]() |
Re: When will i ever use pointers?
Quote:
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;
}
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr. |
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: When will i ever use pointers?
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |