Wir stellen vor: 

Prezi AI.

Ihr neuer Präsentationsassistent.

Verfeinern, verbessern und passen Sie Ihre Inhalte an, finden Sie relevante Bilder und bearbeiten Sie Bildmaterial schneller als je zuvor.

Inhalt wird geladen...
Wird geladen...
Transkript

Pointers,Command Line & Dynamic memory allocation

JELLYCORN

12-2-2021

POINTERS

AGENDA

  • Pointers
  • Pointers to pointers
  • Pointer Arithmetic
  • Dangling Pointer
  • Function Pointer

AGENDA

C Pointers

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.

POINTERS

Declaring a pointer

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

Declaring a pointer

example

#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;

}

POINTERS

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.

USAGE OF POINTER

POINTERS

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

PLACEMENT

POINTERS

https://www.indiabix.com/c-programming/pointers/

https://www.geeksforgeeks.org/c-language-2-gq/pointers-gq/

Double pointer

or

pointer to pointer

https://www.youtube.com/watch?v=Wjhbt3ICbJg

POINTERS

Pointer Arithmetic

Pointer

Arithmetic

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:

  • Increment
  • Decrement
  • Addition
  • Subtraction
  • Comparison

Dangling Pointer

Dangling

Pointer

Dangling

Pointer

Dangling

Pointer

Function pointer

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.

Function

pointer

Example

Example

#include <stdio.h>

int main()

{

printf("Address of main() function is %p",main);

return 0;

}

Command Line Arguments

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[] )

  • Here, argc counts the number of arguments. It counts the file name as the first argument.

  • The argv[] contains the total number of arguments. The first argument is the file name always.

Command

Line

Arguments

Example

Example

#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]);

}

}

OUTPUT

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

Dynamic

Memory

Allocation

The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.

  • malloc()
  • calloc()
  • realloc()
  • free()

DMA

Erfahren Sie mehr über das Erstellen von dynamischen und fesselnden Präsentationen mit Prezi