what exactly ARE pointers, and what are they for?
i knwo they can refer things to the address in the memory in which tey are stroed, &number, par example! but i dobnt understand... WHY! you would want to do that!, why not use a normal int/char etc?
Printable View
what exactly ARE pointers, and what are they for?
i knwo they can refer things to the address in the memory in which tey are stroed, &number, par example! but i dobnt understand... WHY! you would want to do that!, why not use a normal int/char etc?
I am at Chapter 9 Sams book (Sams Teach yourself C++). I have just finished the chapter 8 who talk about Pointer.
Pointer is a ways to declare a variable to store is adress in the memory of the computer. With that you can take and change the value of that adress in all you function without declaring anything in public. I just know that it's very usefull but I do not understand all all I might read again that chapter later :)
Hope that give you some information.
Pointers aren't meant to exist in a highlevel programming language but C++ is a hybrid. Main reason why there are pointers is because of backward compability with C.
Pointers are mainly useful for dynamical memory allocation, that is when you construct datastructures with variable size, such as arrays. Pointer aritmetics is a powerfull tool as well which often saves performance, dealing with C style strings you can skip a load of rutine that would waste performance of a string type, but on the other hand you would need to follow them strictly, a lot of bugs are related to lack of thinking lowlevel.
yeh, but what good are they for, and what is the differance in using them and in usingint or osmething
My guess is that you lack imagination, or you never run into a programming problem which could be solved using pointers.
Make more programs in C/C++ and you'll see by yourself the use of them. It's hard to explain what are they for as their use allow a lot of flexibility.
Personally they turn me on. :p
yeh, but i cant very welhem in a program if i dont know what to do with them can i!!!!!!!
give me a program ot do, and a use of pointers in that program, and hwy a normal int/char/float etc. wouldnt be as good.
I have heard the right use of pointers can make your program faster!!! but why!
this is what my book is seemingly failing to help me with here
C example:Here, this means that rather than copying the ENTIRE structure to the function, you pass a pointer instead - thus only 4 bytes are copied (for the pointer), rather than sizeof(bigbugger) bytes.Code:typedef struct _bigbugger {
int a, b, c, d, e, f, g;
double x, y, z;
} bigbugger;
void somecode(const bigbugger *ptr) {
printf("%d, %d, %d\n", ptr->a, ptr->b, ptr->c);
}
Here's your program:
Write a function that takes an integer and CHANGES it to it's own square. So when I write...
...the output is 16. C++ references are not allowed (although you should use them when you're doing real things).Code:int i = 4;
// call the function with i
// then output i:
cout << i;
Another one: write your own resizable array :)
i wont include includes etc!!!
is that what you mean?Code:int main()
{
int i = 4;
int t;
t = i * i;
cout << i;
return 0;
}
you mean like makeQuote:
Originally posted by kedaman
Another one: write your own resizable array :)
char nanny[]={ted,bill}
into something constantly addable to?
Where did includes come into it? :confused:Code:void square_it(int *num) {
*num *= *num;
}
void somecode(void) {
int x = 5;
square_it(&x);
}
so why did you need pointers in that, what would have happened without the pointers?
Hmm, looks like you need to learn some functional programming, but I'd recommend you go learn OOP directly, there's a online tutorial TYSC++ which is quite good: http://newdata.box.sk/bx/c/
ok im gonna read a bit about functions, maybe this will make pointers clearer to me!
If I hadn't used a pointer, it would have modified the COPY that was sent to the function.Quote:
Originally posted by JafferAB
so why did you need pointers in that, what would have happened without the pointers?
C and C++ pass by value (ByVal) as the default - VB uses ByRef.
No, I want you to write a function that basically does this:
i *= i; // will make i the square of itself
ooooh, i see, i see why tht would be usefull because its very complex without that, you got a huge line thing in this here book and a massive program!
its just a matter of knowing when to use em!
unfortunately parksie screwed it up :p
Why change the habit of a lifetime? :pQuote:
Originally posted by kedaman
unfortunately parksie screwed it up :p
yeah why? life is unfortunately :pQuote:
Originally posted by parksie
Why change the habit of a lifetime? :p
Why you use pointers is the same reason why you pass a parameter ByRef in VB - so that:
a: you don't have to pass a mess of data on the stack, just one long. This is one big reason why it's faster.
b: so that when you alter the parameter, the change is reflected in the calling module as well.