+ 1

Is pointers and addresses really usefull for us in runtime??

many of programers say that we can decrease runtime with use of pointers and ... but i never don't understand this and don't understand why pointers are usefull really i can't understands it's application please help me (with example please) why pointers are important?? is pointers usefull for decreading runtime?? how?? with clear example please "if this question usefull for you please don't forgot upvote"

17th Jun 2018, 12:31 PM
Ehsan Fakharzadeh
Ehsan Fakharzadeh - avatar
4 odpowiedzi
+ 1
#ifndef __cplusplus typedef #endif struct some_struct() { //a lot of data #ifdef _cplusplus }; #else } some_struct; #endif some_struct modify(some_struct param); //very slow, copies a lot of data to and from the function #ifdef __cplusplus void modify(some_struct & param); #else void modify(some_struct *param); #endif //Very fast, only 4/8 bytes are copied
17th Jun 2018, 1:22 PM
Vlad Serbu
Vlad Serbu - avatar
+ 1
Ehsan Fakharzadeh What is there to explain. If you pass an argument to a function that argument is copied. The purpose of that is: void foo(int y) { ++y; } int x = 5; foo(x); //x will still be 5 If that's not what you want you can do: int bar(int y) { return ++y; } int x = 5; x= foo(x); //x will still be 6 What happens is that the value 5 gets copied to bar, and then the value 6 gets copied to the caller. However, when we work with large amounts of data, copying values like that can get expensive. If that's the case we can just pass a pointer to the data, as I outlined in my first comment.
17th Jun 2018, 1:37 PM
Vlad Serbu
Vlad Serbu - avatar
0
Vlad Serbu please a little explain this code
17th Jun 2018, 1:30 PM
Ehsan Fakharzadeh
Ehsan Fakharzadeh - avatar
0
Vlad Serbu ok i understand thank you
17th Jun 2018, 1:40 PM
Ehsan Fakharzadeh
Ehsan Fakharzadeh - avatar