Docsity

Pointers in C Programming, Lecture notes of C programming

Partial preview of the text

Download Pointers in C Programming and more Lecture notes C programming in PDF only on Docsity!

Chapter 6

Pointers

FE – C Programming

Prof. Namrata Jiten Patel

Assistant Professor Dept. of Computer Engineering, SIES Graduate School of Technology

Objectives :To provide exposure to problem-solving by developing an algorithm, flowchart and implement the logic using C programming language Outcomes: Learner will be able to …

  1. Understand the concept of pointers

Learning Outcomes Declaration, initialization and dereferencing of pointers Operations on Pointers Concept of dynamic memory allocation. Fundamentals of pointers

Understanding memory allocation

  • All computers have primary memory, also known as RAM or random- access memory.
  • For example, a computer may have 16, 32, 64, 128, 256, or 512 MB of RAM installed.
  • (^) RAM holds the programs that the computer is currently running along with the data they are currently manipulating
  • (^) All the variables used in a program (and indeed, the program itself) reside in the memory when the program is executed.
  • (^) When a program in C is written and compiled, the compiler will allocate the memory necessary to run the program.

Address of Operator(&)

  • Readers might have noticed that when we call certain functions in C the & sign is used.
  • For example,
    • scanf(“%d”, &n); Takes the input from the terminal and stores it in integer format in the variable named n.

Pointer

  • A pointer provides a way of accessing a variable without referring to the variable directly.
  • The mechanism used for this is the address of the variable.
  • (^) A program statement can refer to a variable indirectly using the address of the variable.
  • A pointer variable is a variable that holds the memory address of another variable.
  • (^) The pointer does not hold a value in the traditional sense; instead, it holds the address of another variable.
  • (^) They are called pointers for the simple reason that, by storing an address, they ‘point’ to a particular point in memory.
  • (^) The syntax for declaring a pointer variable is datatype * pointer_variable;
  • (^) where, datatype is the type of data that the pointer is allowed to hold the address of (that is, the type of data that the pointer is allowed to point to) and pointer_variable is the pointer variable name that is used to refer to the address of a variable of type datatype.
  • An example of a pointer declaration would be
    • *char ptr;
  • (^) The above declaration should be evaluated as: ptr is a pointer to char type data. char is not the data type of ptr.
  • (^) ptr is an identifier of type pointer and char is a data specifier that is used to indicate what type of data is at the memory address that ptr is holding.
  • (^) Pointers are variables that hold memory addresses. At the memory address, held in a pointer, is a value; this value has a data type of one of the C data types or a user-defined data type (e.g., structure).

Meaning of some pointer type variable declarations

Why should pointers have data-type

  • Point:
  • (^) A compiler that generates a code for a pointer, which accesses these objects using pointer arithmetic, requires information on generating offset.

Why should pointers have data-type

  • Point: Sizes of various data types are basically decided by the machine architecture and/or the implementation.

Indirection Operator and Dereferencing

  • The primary use of a pointer is to access and, if appropriate, change the value of the variable that the pointer is pointing to.
  • The other pointer operator available in C is ‘*’, called the ‘value at address’ operator.
  • (^) It returns the value stored at a particular address.
  • The value at address operator is also called indirection operator or dereference operator.