Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may be unselfish, and under another set quite selfish.
2. True freedom can’t be attained by perfect unselfishness.
3. Whatever is the source of religion/ethics, the central idea is to constantly think of the puny, little self.
Only 1 & 2
Only 2&3
Only 1 & 3
All 1,2 & 3
Which of the following statements about selfishness/unselfishness is/are INCORRECT?
1. The same action under one set of circumstances may
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
U3_4_PointerArithmetic.pdf people will use the above preaentationa dn
1. UNIT III
Modular programming:
Function prototype
Function definition
Function call
Built-in functions
Recursion:
Recursive functions
Pointers:
Pointer increment
Pointer arithmetic
Parameter passing:
Pass by value
Pass by reference
Pointer and arrays
Dynamic memory allocation with malloc/calloc
Topics to be Covered:
12/13/2023 1
BVL_Computer Centre, Madras Institute of Technology
2. Address in C
• Whenever a variable is defined in C, then we can access the
memory address of that variable using & symbol.
• For example, if we define a variable named num then if we
use &num, it will give the variable num’s address in memory.
12/13/2023 2
BVL_Computer Centre, Madras Institute of Technology
Output:
3. Pointers in C
• Pointers are variables that store the address of other variables.
• A pointer can be used to store the memory address of other
variables, functions, or even other pointers.
• The use of pointers allows low-level memory access, dynamic
memory allocation, and many other functionality in C.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 3
4. Pointer declaration
• Like ordinary variables, pointers also have a data type and
must be declared before they are used to store the address
of any variable.
• Pointers are declared with the help of an asterisk (*)
followed by the pointer's name.
• Syntax:
datatype *pointer_variable_name;
• In the above syntax, type refers to what data
type of the variable the pointer will point
to.
• All the pointers, whether integer, float, character, etc., are
of the same data type, a long hexadecimal number
representing the memory address.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 4
5. Pointer declaration – Cont’d
• Pointers can be declared in two ways:
• One way is to attach the asterisk with the name of the pointer
variable during the declaration
• i.e. datatype *pointerVariable
• Example: int *ptr;
• Another way is to attach the asterisk at the end of the data
type of which the pointer variable is to be created
• i.e. datatype* pointerVariable
• Example: int* ptr;
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 5
6. Pointer declaration – Cont’d
• In the previous example, ptr is a pointer, and its type
will be specifically be referred to as "pointer to int",
because it stores the address of an integer variable.
• The type is important.
• While pointers are all the same size, as they just store
a memory address, we have to know what kind of
thing they are pointing to.
• double * dptr; // a pointer to a double
• char * cptr; // a pointer to a character
• float * fptr; // a pointer to a float
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 6
7. Pointer declaration – Cont’d
• Recall that we can declare multiple variables on one line
under the same type, like this:
int x, y, z; // three variables of type int
• Since the type of a "pointer-to-int" is (int *), we might ask,
does this create three pointers?
int* a, b, c;
• This is not three pointers. Instead, this is one pointer and
two integers.
• If we want to create multiple pointers on one declaration,
we must repeat the * operator each time:
int * a, * b, * c; // three pointers-to-int
• int * a, b, c; // a is a pointer, b and c are integers.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 7
8. Initialize a pointer
• After declaring a pointer, we should initialize it like
standard variables with a variable address.
• In C programming, If pointers are uninitialized and used in
the program, then the results are unpredictable and
potentially disastrous.
• Syntax:
pointer = &variable;
• Example: ptr=#
• Note:
• Pointer values and integer values are entirely different
• We cannot store any address in integer variable.
• To store address of any variable we have to declare one variable
as pointer.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 8
9. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 9
Output:
Note: In our program pointer variable ptr
contains the address of integer variable num.
To store address of float, char or any other type
variable, we have to declare a pointer of that
type.
10. Accessing address of register variable
• In the above program, the code tries to get the address of variable
i into the pointer variable p but as i is declared as a register
variable, the code won’t compile and will display the error ” Error:
address of register variable requested”.
• Only certain types of variables are placed into registers.
• Register variables are not given an initial value by the compiler.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 10
11. * value at address Operator
(dereference operator)
• This is the second operator used for pointers.
• It is used to access the value present at some address.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 11
13. UNIT III
Modular programming:
Function prototype
Function definition
Function call
Built-in functions
Recursion:
Recursive functions
Pointers:
Pointer increment
Pointer arithmetic
Parameter passing:
Pass by value
Pass by reference
Pointer and arrays
Dynamic memory allocation with malloc/calloc
Topics to be Covered:
12/13/2023 13
BVL_Computer Centre, Madras Institute of Technology
14. Pointer Arithmetic
• We can perform arithmetic operations on the pointers
like addition, subtraction, etc.
• The C language allows following operations to be
performed on pointers.
• Increment/Decrement of a Pointer.
• Addition/Subtraction of Integer to a Pointer.
• Differencing (Subtraction of two Pointer of similar type)
• Comparison of two Pointer of similar type.
12/13/2023 14
BVL_Computer Centre, Madras Institute of Technology
15. Pointer Increment
• If we increment a pointer by 1, the pointer will start pointing
to the immediate next location.
• This is somewhat different from the general arithmetic since
the value of the pointer will get increased by the size of the
data type to which the pointer is pointing.
• The Rule to increment the pointer is given below:
Next_address= current_address + size_of(data type)
• In 32-bit Machine:
• int variable, it will be incremented by 2 bytes.
• In 64-bit Machine:
• int variable, it will be incremented by 4 bytes.
• Similarly, a pointer to a float will be incremented by
sizeof(float) and pointer to char will be incremented by
sizeof(char) .
12/13/2023 15
BVL_Computer Centre, Madras Institute of Technology
17. Pointer decrement
• Decrementing a pointer in C simply means to decrease the
pointer value step by step to point to the previous location.
Previous Address = Current Address – size_of(data type)
• Decrementing a pointer to an int will cause its value to be
decremented by sizeof(int).
• In 32-bit Machine:
• int variable, it will be decremented by 2 bytes.
• In 64-bit Machine:
• int variable, it will be decremented by 4 bytes.
• Similarly, a pointer to a float will be decremented by
sizeof(float) and pointer to char will be decremented by
sizeof(char) .
12/13/2023 17
BVL_Computer Centre, Madras Institute of Technology
19. Addition/Subtraction of Integer to a Pointer
• C allows integers to be added to or subtracted from pointers.
• When an integer i is added to the pointer, then the new value will
be,
(current address in pointer) + i * sizeof(data_type)
• Example:
• int *ptr, n=20; ptr=&n; ptr=ptr+3
• This code will increment the address in ptr by 3*sizeof(int).
• If the sizeof(int) is 4, then the pointer will increment by 12.
• n=20 and ptr=1000 after ptr=ptr+3 value of ptr becomes 1012.
• Likewise when an integer i is subtracted from a pointer, the the
new value will be,
(current address in pointer) – i * sizeof(data_type)
• Example:
• int *ptr, n=20; ptr=&n; ptr=ptr-3
• This code will decrement the address in ptr by 3*sizeof(int).
• If the sizeof(int) is 4, then the pointer will decrement by 12.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 19
21. Subtracting of two pointers of similar type
(Differencing)
• Differencing is the subtraction of two pointers.
• Subtraction of two pointers is only possible, if they have
same data type.
• When two pointers of same data type is subtracted , their
value is subtracted then result will be divided by the size of
data type.
(address in pointer1 - address in pointer2) / sizeof(data_type)
• If 2 pointers points to 2 different array index of same array
then the subtraction of two pointers gives the number of
elements between the two pointers.
• Example:
int a[5] = {10,20,30,40,50};
int *p, *q;
p=&a[0]; q=&a[3];
printf(“%d”, q-p);
The output of q-p will be (1012-1000)/sizeof(int) i.e 12/4=3.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 21
24. Comparison of two pointers of similar
type
• Comparison of two pointers is only possible, if they have
same data type.
• Two pointers can be compared by the operators >, ==,
<= , >=.
• It returns TRUE if condition valid, otherwise return
FALSE.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 24