Skip to main content

Posts

Showing posts from 2018

Smart Pointers in C++

* Smart pointer classes are defined in <memory> header, so we need to include "memory" header to use smart pointers. * The header gives access to three template based smart pointers each with unique features. * We get "unique_ptr", "shared_ptr", "weak_ptr" *  Unique pointer are type of smart pointers that can’t be copied.std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. *  Shared pointer is much like “unique_prt” but we can make copies of it. *  Weak pointer is a special case of shared pointer. Weak pointer helps in breaking circular references.Destruction using weak pointer will not care about destruction of other objects referenced from this present object under destruction.

Drive Bot In A Path

#define LEFT 13 #define RIGHT 12 #define PATH_DURATION1 5000 #define PATH_DURATION2 1000 #define PATH_DURATION3 500 // the setup function runs once when you press reset or power the board void setup() {   // initialize digital pin LED_BUILTIN as an output.   pinMode(LEFT, OUTPUT);    pinMode(RIGHT, OUTPUT); } // the loop function runs over and over again forever void loop() {   moveStraight();   delay(PATH_DURATION1);          moveRight();   delay(PATH_DURATION2);        moveStraight();   delay(PATH_DURATION1);   moveLeft();   delay(PATH_DURATION2); } void moveStraight(){   digitalWrite(LEFT, HIGH);   digitalWrite(RIGHT,HIGH);           // turn the LED on (HIGH is the voltage level) } void moveRight(){   digitalWrite(LEFT,HIGH);   digitalWrite(RIGHT,LOW); } void moveLeft(){   digitalWrite(LEFT,LOW);   digitalWrite(RIGHT,HIGH); }

Learning C from Snippets : Pointers to structures

struct exam{ int a; char *ch; }st[]={10,"Testing",11,"Computer",12,"Science",13,"Engineering",14,"Custo",15,"2018"}; int main() { struct exam *e = st; printf("%s, ",e++->ch); ++e; printf("%s, ",++e->ch);   // -> operator has higher precedence than the ++ opearator. printf("%s, ",++e++->ch); printf("%d, ",e[0].a); printf("%s",++e->ch); return 0; } OUTPUT : Testing, cience, ience, 13, ngineering

Learning C from Snippets : What does pointer +1 means ?

int main(int argc, char** argv) { char p[3][4]={{'R','B','R','a'},{'S','R','K','a'},{'A','P','J','a'}}; printf("%d\n",p); //Prints 7339584 printf("%d",p+1); //Prints 7339588 , i.e there is an addition of 4 to the previous number return 0; } int main(int argc, char** argv) { char p[3][3]={{'R','B','R'},{'S','R','K'},{'A','P','J'}}; printf("%d\n",p); //Prints 7339584 printf("%d",p+1);//Prints 7339583 return 0; } _________________________________________________________________________________ int main(int argc, char** argv) { char p[3][2]={{'R','A'},{'S','R'},{'A','P'}}; printf("%c",*(p+1)); //Prints B return 0; } int main(int argc, char** argv) { char p[3][3]={{'R','B','R