Answers for "example of pointer"

C
1

What is This pointer? Explain with an Example.

Every object in C++ has access to its own address through an important pointer called this pointer.
The this pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to the invoking object.

Example:
#include <iostream>
using namespace std;
class Demo {
private:
  int num;
  char ch;
public:
  void setMyValues(int num, char ch){
    this->num =num;
    this->ch=ch;
  }
  void displayMyValues(){
    cout<<num<<endl;
    cout<<ch;
  }
};
int main(){
  Demo obj;
  obj.setMyValues(100, 'A');
  obj.displayMyValues();
  return 0;
}
Posted by: Guest on November-30-2020
1

poiner in c

#include <stdio.h>
int main() {
   int c = 5;
   int *p = &c;

   printf("%d", *p);  // 5
   return 0; 
}
Posted by: Guest on July-01-2020
1

poiner in c

int* pc, c, d;
c = 5;
d = -15;

pc = &c; printf("%d", *pc); // Output: 5
pc = &d; printf("%d", *pc); // Ouptut: -15
Posted by: Guest on July-01-2020
0

simple example of pointers

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *nums;
    int x;
//	Allocate storage for 3 integers 
    nums = (int *)malloc( sizeof(int) * 3 );
    if( nums==NULL ) {
        fprintf(stderr,"Memory allocation error\n");
        exit(1);
    }
//	Assign the integer values
    for( x=0; x<3; x++ )
        *(nums+x) = x+1;
//	Output the results
    printf("%d %d %d\n",
            *(nums+0),
            *(nums+1),
            *(nums+2)
          );
    return(0);
}
Posted by: Guest on June-06-2021

Code answers related to "C"

Browse Popular Code Answers by Language