Wir stellen vor:
Ihr neuer Präsentationsassistent.
Verfeinern, verbessern und passen Sie Ihre Inhalte an, finden Sie relevante Bilder und bearbeiten Sie Bildmaterial schneller als je zuvor.
Suchtrends
Pointers,Command Line & Dynamic memory allocation
12-2-2021
The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.
int *a;//pointer to int
char *c;//pointer to char
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
return 0;
}
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance
https://www.indiabix.com/c-programming/pointers/
https://www.geeksforgeeks.org/c-language-2-gq/pointers-gq/
https://www.youtube.com/watch?v=Wjhbt3ICbJg
We can perform arithmetic operations on the pointers like addition, subtraction, etc. However, as we know that pointer contains the address, the result of an arithmetic operation performed on the pointer will also be a pointer if the other operand is of type integer. In pointer-from-pointer subtraction, the result will be an integer value. Following arithmetic operations are possible on the pointer in C language:
we can also create a pointer pointing to a function. The code of a function always resides in memory, which means that the function has some address. We can get the address of memory by using the function pointer.
#include <stdio.h>
int main()
{
printf("Address of main() function is %p",main);
return 0;
}
The arguments passed from command line are called command line arguments. These arguments are handled by main() function.
To support command line argument, you need to change the structure of main() function as given below.
int main(int argc, char *argv[] )
#include <stdio.h>
void main(int argc, char *argv[] ) {
printf("Program name is: %s\n", argv[0]);
if(argc < 2){
printf("No argument passed through command line.\n");
}
else{
printf("First argument is: %s\n", argv[1]);
}
}
Run this program as follows in Linux:
---------------------------------------------------
./program hello
---------------------------------------------------
Run this program as follows in Windows from command line:
program.exe hello
-----------------------------------------------------
Output:
Program name is: program
First argument is: hello
If you pass many arguments, it will print only one.
------------------------------------------------------
./program hello c how r u
Output:
Program name is: program
First argument is: hello
But if you pass many arguments within double quote, all arguments will be treated as a single argument only.
--------------------------------------------------------
./program "hello c how r u"
Output:
Program name is: program
First argument is: hello c how r u