+ 13

Why understanding pointers and structures are hard in C language

2nd Oct 2018, 3:50 AM
Harsh Singh
18 odpowiedzi
+ 2
Zoetic_Zeel Let me take an easy example to understand pointers. First of all, pointers are mainly used to pass value by reference. Now what does this mean? I hope you know how to pass by value: int add(int c, int d) { return (c+d); } int main() { int a,b; scanf("%d%d",&a&b); printf("%d",add(a,b)); } Here you can see, variables a and b inside the main() are its local variables and variables c and d in the parameters of add() are local to add(). This means any change in value of c and d will not change values of a and b and therefore return is used. Now, how about we skip return statement such that changes made in add() reflect changes in a and b inside main() ? This is where pointers comes into picture. Here's how: int add(int *c, int d) { *c=*c+d; } int main() { int a,b; scanf("%d%d",&a&b); add(&a,b); // only a is passed by reference printf("%d",a); } What happened here? Lets see-> We passed the address of variable (a) instead of passing its value using '&' operator. (1/2)
17th Oct 2018, 5:07 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 6
Harsh Singh What do you find difficult exactly. Please add descriptions to your questions in the future.
2nd Oct 2018, 5:33 AM
Manual
Manual - avatar
+ 6
just go ahead with basics of basic...... & just try to make 6-7 programs daily based on pointer & structure..... than... believe me...... u can't feel definitely at all
18th Oct 2018, 12:19 PM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 5
Sherlock 聂... So.... what's the better material?.... if u have... so drop the link here
23rd Oct 2018, 9:04 AM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 5
Sherlock 聂 thanks dear
24th Oct 2018, 11:55 AM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 4
Zoetic_Zeel To be honest, I need to look into this myself as I don't know much about this right now. As of now, I can say it seems to me like any other pointer variable... https://www.geeksforgeeks.org/whats-difference-between-char-s-and-char-s-in-c/amp/ https://code.sololearn.com/cEN7NaaeHx1g/?ref=app
17th Oct 2018, 8:46 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
Things seem hard until you understand them Yes! Its hard, no doubt. All you need is a right tutorial.
3rd Oct 2018, 5:11 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
(cont) Variable c is a pointer to variable a and contains its address and with '*' operator, we can access the value contained by the variable to which it points. Any changes made to c variable will reflect a change of value in a var. Therefore, when we write *c=*c+d, it actually is a=a+d (just for the sake of understanding) Without using return, we stored the sum in var a and then printed it (though you will loose the original value of a in this case, but this is just an example. If you wish you can use a temporary variable) Interestingly, we can change as many values as we want using pointers instead of only one value as in case of return. Hope this helps :) (2/2)
17th Oct 2018, 5:08 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
Didn't understand that topic.... Yes'll add descriptions in future questions.
2nd Oct 2018, 5:36 AM
Harsh Singh
+ 2
no. practice are make strong grip for structure ...... statement
4th Oct 2018, 2:15 PM
Ashish Sharma
+ 2
please complete your question. where do you find problem.
11th Oct 2018, 12:48 AM
Rohit Kumar
Rohit Kumar - avatar
+ 2
Infinity Oh, that was a great explanation. thank you very much. I see somewhere they use directly char* to declare string array or a string. is it similar thing as pointers or I'm missing something here...
17th Oct 2018, 6:25 AM
Zoetic_Zeel
Zoetic_Zeel - avatar
+ 1
yes,indeed.
3rd Oct 2018, 5:18 PM
Harsh Singh
+ 1
Manual Infinity What is the scope of a pointer? and why exactly we need pointers.. Can anyone give an comparative example, where one can show that using pointers is better approach here..
16th Oct 2018, 7:53 PM
Zoetic_Zeel
Zoetic_Zeel - avatar
+ 1
This is not much,but can help to understand the basics of pointer. https://code.sololearn.com/coqySKO29c94/?ref=app
17th Oct 2018, 8:50 AM
Harsh Singh
0
I can't understand that question
14th Oct 2018, 2:12 PM
Rajani Pambala
Rajani Pambala - avatar