100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Pointers and data structures in c R141,77   Add to cart

Class notes

Pointers and data structures in c

 6 views  0 purchase
  • Course
  • Institution

Complete knowledge about pointers and data structures in an easy way.

Preview 3 out of 22  pages

  • March 25, 2023
  • 22
  • 2022/2023
  • Class notes
  • Dr karthik
  • All classes
avatar-seller
Pointers

Introduction

 A pointer is a derived data type. This is the one which stores the address of data in memory, we will
be in position to access the data directly and do calculations over it.
 The standard concept is, access the data from memory using variable name it gets the data and
operations are done over them.
 But the pointer is different that the accessing is done by address the data is stored so that it will be
advantage of decreasing the instructions and overheads of standard usage.

Definition:

A pointer is a variable which contains the address of another variable.
Advantages of pointer

 Enables us to access a variable that is defined outside the function.
 Can be used to pass information back and forth between a function and its reference point.
 More efficient in handling data tables.
 Reduces the length and complexity of a program.
 Sometimes pointers also increases the execution speed.

Declaring of a pointer variable

General form:
data_type *pointer_name;
where,
• The asterisk (*) tells that the variable pointer_name is a pointer variable.
• Pointer_name is a identifier.
• pointer_name needs a memory location.
• pointer_name points to a variable of type data_type which may be int, float, double etc..
Example:




where
 ptr is not an integer variable but ptr can hold the address of the integer variable i.e. it is a pointer to an
integer variable’ ,but the declaration of pointer variable does not make them point to any location .
 We can also declare the pointer variables of any other data types .
For example:
double * dptr;
char * ch;
float * fptr;

,Dereference operator (*)
The unary operator (*) is the dereferencing pointer or indirection pointer , when applied to the
pointer can access the value the pointer points to.

Example:

int i= 15;
int * ptr;
ptr=&i;
printf(“Value of i is :%d”,i);
printf(“Address of i is :%d”,&i);
printf(“Value of i is :%d”,*ptr);
printf(“address of i is :%d”,ptr);
printf(“address of pointer is :%x”,&ptr);

Value of i is : 15
Address of i is : 1003
Value of i: 15
Address of i is : 1003
Address of ptr is : 2712

The null pointer
 Sometimes it is useful to make our pointers initialized to nothing . This is called a null pointer.
 A null pointer is the pointer does not point to any valid reference or memory address .We assign a
pointer a null value by setting it to address 0:
 Example
int *iptr;
iptr=0; or iptr = NULL;
 This statement assign address 0 to iptr . This statement describe a special preprocessor define called NULL
that evaluates to 0.
 Hence A null pointer is a pointer type which has a special value that indicate it is not pointing to any valid
reference.

Initialization of pointers
Initializing a pointer variable is a important thing, it is done as follows:
Step 1: Declare a data variable
Step 2:Declare a Pointer variable
Step 3:Assign address of data variable to pointer variable using & operator and assignment
operator.
Example:
int x;
int *p
p = &x;

, Pointer arithmetic

Pointer arithmetic operations are different from normal arithmetic operations.
The operations allowed to on Pointers are:
1. Add or subtract integers to/from a pointer. The result is a pointer.
2. Subtract two pointers to the same type. The result is an int.
3. Comparison of two pointers

Note: Multiplication, addition, division of two pointers not allowed.
1. Add or subtract integers to/from a pointer

 Simple addition or subtractions operations can be performed on pointer variables.
 If *iPtr points to an integer, *iPtr + 1 is the address of the next integer in memory after *iPtr. *iPtr
- 1 is the address of the previous integer before *iPtr
Examples:

int a=57; int a=57;
int *iptr=&a; iptr= iptr +1; iptr = iptr +(2B) iptr int *iptr=&a; iptr= iptr -1; iptr = iptr -(2B) iptr
=1003+2 =1003-2
iptr =1005 iptr =1001

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller hemantkumar1. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R141,77. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

77254 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R141,77
  • (0)
  Buy now