NOT SO COMMON C++
Hello Techiez, today in this post i'll be discussing about some shortcuts and some methods in C++ which are we generally miss while studying C++. This post will be useful for both C++ beginners as well for experienced programmers.
So lets explore.
Now as we discussed earlier about the addition on pointer , we will see an example on it.
ptr1=ptr1+1; // w.r.t the above example
So, what will happen now ....????
As we discussed earlier addition of a pointer and an integer gives you back a POINTER.
When we are adding 1 to the ptr1 then ptr1 would be increased by (1*sizeof(int)) and now ptr1 will point to arr[1].
I hope all this info on C++ was helpful to you, i'll be posting more post like this one on C++ in coming time.
For any queries leave comment below.
Keep Exploring.
So lets explore.
1. Convert a decimal value to hexadecimal/octal.
Program:
#include<iostream>
using namespace std;
int main()
{
int num1=300;
cout<<hex<<num1; //hex is an manipulator used to convert num1 into hexadecimal value
}
Output:
12c
Same program can be used to convert :
(a)decimal to octal, just change "hex" to "oct" and
(b)for octal/hexadecimal to decimal, just change "hex"/"oct" to "dec" (before a hex/oct number).
lets see an example for part '(b)'
Program:
#include<iostream>
using namespace std;
int main()
{
int num1=01067; // 01067 is an octal for 567
cout<<dec<<num1;
}
Output:
567
Another way to do the same:
Program :
#include<iostream>
#include<iomanip> // for using setbase() using iomanip is necessary
using namespace std;
int main()
{
int num1=345;
cout<<setbase(16)<<num1; // to convert num1 int octal change "16" to "8"
return 0;
}
similarly to convert hexadecimal(or octal) value to decimal use setbase(2) before the hexadecimal value.
2. Different ways to Initialize and not to Initialize an array.
Yes there are a number of ways to initialize an array.
(a) int array[6]; // CORRECT
(b) int array[6]={9,4,6,2,3,5}; //CORRECT
(c) int array[6]={9,4}; //CORRECT
(d) int array[]; //INCORRECT
(e) int array[2]={9,4,6,2,7,8}; //INCORRECT
(f) int array[]={2,3,6,7}; //INCORRECT
3. Structure Initialization.
Structures can also be initialized, the structure initiator is enclosed in curly brackets and contains different values which w.r.t the subsequent fields.
Program:
#include<iostream>
using namespace std;
struct Student
{
string name;
int age;
int Std;
};
int main()
{
struct Student student={"Rahul",12,6}; // This is structure initialization
cout<<student.name<<endl;
cout<<student.age<<endl;
cout<<student.Std<<endl;
}
Output:
Rahul
12
6
4. Structure can be a field inside an another Structure.
Lets see an example to understand this.
Program:
#include<iostream>
#include<iomanip>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
struct Emp
{
string name="Rahul";
int age=26;
struct Date dateofjoin={12,9,2011}; //Structure as a field inside an another structure
};
int main()
{
struct Emp emp;
cout<<emp.name<<endl;
cout<<emp.age<<endl;
cout<<emp.dateofjoin.day<<"-"<<emp.dateofjoin.month<<"-"<<emp.dateofjoin.year<<endl
}
Output:
Rahul
26
12-9-2011
Now, what is that "emp.dateofjoin.day" ?
Here we are using two subsequent operators(selection operator) to dig deeper into the structure.
We are first selecting a structure within a structure and then the desired field of the inner structure.
5. Can we assign values to pointer?
(a) We cannot assign a literal value to the pointer.
Program:
int *p;
p=45678; // this is will show error beacuse 45678 is a literal value
Output:
error
(b) we can assign some meaningful value to pointer.
Program:
int *p;
p=&i; //assigning the pointer with the value which points to already existing variables.
(c) we can also assign zero or null to the pointer
Program:
int *p;
p=0; // zero is the only literal we can assign to pointer
OR
There are some more things I would like to point out about the Pointers which may b useful to useful to use.
- If we use the name of an array(without indices), it is equivalent to the pointer pointing to the first element of the array. i.e
int *p, array[10];
here, "array" is equivalent of using "&array[0]".
But remember you can only use "array" to point to the first element in the array(i.e array[0]).
here, "array" is equivalent of using "&array[0]".
But remember you can only use "array" to point to the first element in the array(i.e array[0]).
- Adding an integer value to a pointer gives back a Pointer.
- Subtracting an integer value from a pointer gives back a Pointer.
- Subtracting a pointer from another pointer gives back an Integer.
- comparison of two pointers for equality or inequality is also meaningful and can be practiced in C++.
Any other operation is either prohibited or meaningless.
Lets see an example:
Lets see an example:
Program:
int *ptr1, *ptr2;
int arr[3];
ptr1=arr;
ptr2=ptr1;
are you able to understand the flow of program, consider this diagram to get a better idea about what happened in the above code
.
ptr1=ptr1+1; // w.r.t the above example
So, what will happen now ....????
As we discussed earlier addition of a pointer and an integer gives you back a POINTER.
When we are adding 1 to the ptr1 then ptr1 would be increased by (1*sizeof(int)) and now ptr1 will point to arr[1].
I hope all this info on C++ was helpful to you, i'll be posting more post like this one on C++ in coming time.
For any queries leave comment below.
Keep Exploring.
Comments
Post a Comment