+ 13
Why understanding pointers and structures are hard in C language
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)
+ 6
Harsh Singh
What do you find difficult exactly.
Please add descriptions to your questions in the future.
+ 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
+ 5
Sherlock 聂... So.... what's the better material?.... if u have... so drop the link here
+ 5
Sherlock 聂 thanks dear
+ 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
+ 3
Things seem hard until you understand them
Yes! Its hard, no doubt.
All you need is a right tutorial.
+ 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)
+ 2
Didn't understand that topic....
Yes'll add descriptions in future questions.
+ 2
no. practice are make strong grip for structure ...... statement
+ 2
please complete your question.
where do you find problem.
+ 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...
+ 1
yes,indeed.
+ 1
This is not much,but can help to understand the basics of pointer.
https://code.sololearn.com/coqySKO29c94/?ref=app
0
I can't understand that question