What exactly are pointers good for? It seems to me like they're the same thing as variables.
What exactly are pointers good for? It seems to me like they're the same thing as variables.
Yes pointers are varaible but variable that can point to the address of other varaible .Why is it so use full well one use of pointer in C is pass-by-reference that when you pass a value lets say from main() to a function it is by default passed by value that mean if the function does any change to that varaible that is passed it wont affect the one in main() but using pass-by-reference it will change the value in main().Originally posted by Yawgmoth
What exactly are pointers good for? It seems to me like they're the same thing as variables.
There are many other uses for pointer and its the back bone of C..
"I wish i could wish my wishs away"
"By indirections find directions out" -- William Shakespears
"Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"
Mizra -> love = Death...
RDB(Rocks yooo)..
http://www.cbeginnersunited.com
Are you ready for the Trix ???
Imagine if you have this structure:
And you need to pass it to a function for some modification. Then you can:Code:typedef struct { int Ages[10000000]; char Names[10000000][1024]; char Locations[10000000][4096]; }MYSTRUCT;
A) Pass the structure, this means sending ~50 Gb of data.
B) Pass a pointer to it, this means sending 4 bytes of data.
Seems a little faster to send a pointer than the data, right?
Another thing, run this program and compare the results:
As you see, in 2 you can modify a variable through a function, in 1 you cannot.Code:void Modify1(int Var) { Var = 456; } void Modify2(int* Var) { *Var = 456; } int main() { int MyVar; MyVar = 123; printf("Before: %d\n", MyVar); Modify1(MyVar); printf("After: %d\n\n", MyVar); MyVar = 123; printf("Before: %d\n", MyVar); Modify2(&MyVar); printf("After: %d\n\n", MyVar); return 0; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Pointers are variables that store memory address, and you can use it to pass variables to functions and modify them...
You can also dynamically allocate memory using malloc() and free() in C, or new and delete in C++
And pointers are very important in WIN32 Programming.
none...
And don't forget about function pointers, also a very usefull application of pointers.
>What exactly are pointers good for?
In my opinion pointers are good for making programs more simple and more efficient.
pointers are for many purposes
- without pointers you can not access memory address.
- you can create dynamic array with pointer.
- many data structures need pointer to solve.
- function can't return char[] but it can return char*
- etc.
Pointers aren't important. You'll never use them.
Pointers allow you to keep hold of an adress of a variable or literal. This adress can then be passed on from function to function without multiple variable declarations. Many OS's use pointers extensively.
If you know the adress of a variable or a literal, you can modify it's calling paramaters!
This is one of those features that really makes C and C++ stand out from most other programming languages as it sort of like bridges between low level and high level programming.
I recommend that you research this subject, hovewer this is Level 3 material (advanced diploma) and you shouldn't worry about it too much if you are studying for your high school exam or equvalent (unless of course it is in the syllabus).
Most C programmers are very aware of pointers and so should you - even if you don't intend to use them.
Best regards.... Happy studying -- C is a very nice language if you get use to it.
Thanks for answering my question, even though it was probably a newbie thing to ask.
True literals (IE not string literals) can not be referred to by a pointer because they have no location in memory. They are strictly rvalues.Originally posted by momo20016
Pointers allow you to keep hold of an adress of a variable or literal.