Pointers

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

#include <stdio.h> void add(int a, int b) { printf("Addition is %d\n", a+b); } void subtract(int a, int b) { printf("Subtraction is %d\n", a-b); } void multiply(int a, int b) { printf("Multiplication is %d\n", a*b); } int main() { // fun_ptr_arr is an array of function pointers void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply}; unsigned int ch, a = 15, b = 10; printf("Enter Choice: 0 for add, 1 for subtract and 2 " "for multiply\n"); scanf("%d", &ch); if (ch > 2) return 0; (*fun_ptr_arr[ch])(a, b); return 0; }

Example of array of unction pointers

True.. resort to this method

Function pointer can be used in place of switch case. True or false?

1. Function code is stored in memory 2. Start of the function code or the address of a function is a "function pointer" 3. Function pointer is "different" from other pointers since you do not allocate or deallocate memory with them 4. Function pointers can be passed as arguments to other functions or return from functions

Functions as pointers

Int cube(int *ptr) // def of pointer done here Int main { int num= 5; cube(&num); } void cube( int *ptr) { *ptr = *ptr * *ptr * *ptr }

How to Pass by reference, arguments to a function?

Int *countrPtr // this is read as the variable countrPtr is pointer to int or it points to a object of type int

How to define a pointer?

definition = int *ptr assigning value = ptr=&a get value in the referenced address = *ptr example: printf("%d",*ptr ) gives the value at the address in the pointer

How to define, assign value and use a pointer?

sizeof( array )/ sizeof( array[ 0 ] )

How to find the number of elements in an array using sizeof?

Three ways - (a) 0 (b) NULL (c) address 0 is the only integer that can be assigned to a pointer

How to initialise pointers?

int * ptrInteger; // defining a pointer to an integer int foo(int); // defining a normal function int (*foo)(int); // declares a pointer to a function

How to pass pointer to a function?

True

Just like an array A function's name can also be used to get functions' address. True or false?

This will give the address of y in hexadecimal To get the value of y printf ( "%d",*yPtr)

Output?? Int y = 5; Int *yPtr; yPtr = &y; printf("yPtr contains %p",yPtr)

array name points to starting adress so to initialize a ptr to an array 'test' just write ptr=test; this is same as ptr = &test[0];

Relation between pointer and an array?

& - address operator * - dereferencing operator

What are '&' and '*' operators called?

Pointers are variables whose values are memory addresses. A pointer contains the address of a variable that contains a value

What are pointers?

printf ( "%d",*yPtr) Usage of star like this and then printing the value of the variable to which the pointer is pointing

What does dereferencing a pointer mean?

They keep the variable constant and cant be changed. If this const is passed as argument it will not get changed by any chance

What does the const qualifier do?

say a array starts at 3000 address to get to 3002 if we add ptr+2 we will not get 3002 instead we will get ptr + (2* size of the datatype) if int of 2 bytes used then it goes to 3004. check the above sizeof operater is helpful here apart from this we can do ++ and --

What will we get if we do the addition arithmetic to a pointer?

True

a pointer can be assigned to another pointer if both have same data type. True or false?

It returns the size of the datatype in bytes

what does a sizeof operator do?

This pointer will have the address of the starting of that particular function code. a function pointer points to code, not data. Typically a function pointer stores the start of executable code.

what is meant by pointer to a function?


Ensembles d'études connexes

Chapter 28: Growth and Development of the School-Age Child - ML3

View Set

Managerial Accounting Chapter 1 Learnsmart

View Set

109. Pharmacology of Alcohol Abuse

View Set

Heating the Atmosphere 2: The Atmosphere

View Set