This post contains details about pointer, their usability and many more things.
So lets start the post without any further delay.
Hello Readers,
Lets get going with pointers and its details.
A pointer in C is just a variable like any other numerical variable except that it accepts a special value called an address of a variable or a function.
If we try to assign an integer value or any other numerical value to a pointer variable then we will get an error.
Below I have include the sample code and the results for the same.
Code
Output
In the code above we faced the error due to the assignment operation where we are trying to assign 10 to a variable ptr which is an integer pointer variable.
If you want to still use ptr variable then on the left hand side of the assignment there needs to be an address insted of a numerical value.
In order to use pointer there is basic syntax for its assignment and to retrive value from the variable.
Declaring syntax:
datatype *var_name;
Assigining value(address) to pointer variable:
datatype *var_name;
datatype x;
var_name = &x;
where x can be variable of any data type. For the pointer variable to accept the variable address , data of pointer should match to that of the declared variable.
Accessing value of a variable using a pointer:
int *ptr;
int x=10;
ptr = &x;
printf("Value of X:%d\n",*ptr);
*ptr means to de reference the pointer which in simple words means to access the value stored at the address within the pointer variable.
Thank you.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.
So lets start the post without any further delay.
Hello Readers,
Lets get going with pointers and its details.
A pointer in C is just a variable like any other numerical variable except that it accepts a special value called an address of a variable or a function.
If we try to assign an integer value or any other numerical value to a pointer variable then we will get an error.
Below I have include the sample code and the results for the same.
Code
Output
In the code above we faced the error due to the assignment operation where we are trying to assign 10 to a variable ptr which is an integer pointer variable.
If you want to still use ptr variable then on the left hand side of the assignment there needs to be an address insted of a numerical value.
In order to use pointer there is basic syntax for its assignment and to retrive value from the variable.
Declaring syntax:
datatype *var_name;
Assigining value(address) to pointer variable:
datatype *var_name;
datatype x;
var_name = &x;
where x can be variable of any data type. For the pointer variable to accept the variable address , data of pointer should match to that of the declared variable.
Accessing value of a variable using a pointer:
int *ptr;
int x=10;
ptr = &x;
printf("Value of X:%d\n",*ptr);
*ptr means to de reference the pointer which in simple words means to access the value stored at the address within the pointer variable.
Thank you.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.
Comments
Post a Comment