0% found this document useful (0 votes)
97 views

Lesson 7 C Pointers

The document discusses C pointers and related concepts. It covers: - What pointers are and how they store memory addresses - Common pointer operators like & and * - Using pointers with arrays, functions, and strings - Pointer arithmetic and incrementing pointers - Key features of pointers like saving memory and allowing dynamic memory allocation

Uploaded by

Jontex 254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Lesson 7 C Pointers

The document discusses C pointers and related concepts. It covers: - What pointers are and how they store memory addresses - Common pointer operators like & and * - Using pointers with arrays, functions, and strings - Pointer arithmetic and incrementing pointers - Key features of pointers like saving memory and allowing dynamic memory allocation

Uploaded by

Jontex 254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C programming pointers notes

Area coverage

 C pointers
 Pointers to arrays
 Pointers to function
 Pointers to string

C Pointers

 The pointer in C language is a variable which stores the address of another


variable.
 A pointer is an object that holds one of the following: a null pointer, the address of an
object, or garbage (because it’s uninitialized or holds the address of a now-freed
object).
 Pointers are a lot faster compared to direct references.
 They’re used to optimize a program to run faster, or use less memory than it
would otherwise.
 Another reason why we use pointers is that they allow for dynamic memory
allocation. Pointers are necessary for dynamic memory allocation.
 Dynamic memory allocation comes in when you’re working with an unknown
amount of data— it allows you to create variables, arrays, and more complex data
structures in memory while the program is running.
 This variable can be of type int, char, array, function, or any other pointer. The
size of the pointer depends on the architecture.

The * is a unary operator. It is used to indicate that the variable is a pointer.


variable it points to
– * indicates that a variable is a pointer
– & is used to store the address of a variable in a pointer
– If a pointer points to NULL, it means that the pointer is not pointing to
anything.

1|P ag e
C programming pointers notes

Symbol Name Description

& (ampersand sign) Address operator Determine the address of a variable.

∗ (asterisk sign) Indirection operator Access the value of an address.

Features of Pointers:
1. Pointers save memory space.
2. Execution time with pointers is faster because data are manipulated with the
address, that is, direct access to memory location.
3. Memory is accessed efficiently with the pointers. The pointer assigns and releases
the memory as well. Hence it can be said the Memory of pointers is dynamically
allocated.
4. Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
5. An array, of any type can be accessed with the help of pointers, without
considering its subscript range.
6. Pointers are used for file handling.
7. Pointers are used to allocate memory dynamically.

2|P ag e
C programming pointers notes

8. In C++, a pointer declared to a base class could access the object of a derived
class. However, a pointer to a derived class cannot access the object of a base
class.
9. To create complex data structure such as linked list, where one data structure
must contain references to other data structures.

Arrays and Pointers Relation ship

Arrays and pointers are intimately related in C++ and may be used almost
interchangeably.
 An array name can be thought of as a constant pointer.
 Pointers can be used to do any operation involving array subscripting.
 Assume the following declarations:
o int b[ 5 ]; // create 5-element int array b
o int *bPtr; // create int pointer bPtr

//Program to find the sum of six numbers with arrays and pointers.
#include <stdio.h>
int main(){
int i,class[6],sum=0;
printf("Enter 6 numbers:\n");
for(i=0;i<6;++i){
scanf("%d",(class+i)); // (class+i) is equivalent to &class[i]
sum += *(class+i); // *(class+i) is equivalent to class[i]
}
printf("Sum=%d",sum);
return 0;
}

3|P ag e
C programming pointers notes

Pointer to an Array in C

Pointer to an array is also known as array pointer. We are using the pointer to access
the components of the array.
pointers are dynamic, which means the memory allocated can be resized later at any
point in time.

data structure that contains a group of elements. Pointers are an important for creating,
using, and destroying all types of data structures.

Real Application example of pointers to arrays

Library catalogues reference book through a numbering system very like a software pointer.

Once you store the address of the first element in 'p', you can access the array elements
using *p, *(p+1), *(p+2) and so on.

#include <stdio.h>

int main () {

/* an array with 5 elements */


double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;

p = balance;

/* output each array element's value */


printf( "Array values using pointer\n");

for ( i = 0; i < 5; i++ ) {


printf("*(p + %d) : %f\n", i, *(p + i) );
}

printf( "Array values using balance as address\n");

for ( i = 0; i < 5; i++ ) {


printf("*(balance + %d) : %f\n", i, *(balance + i) );
}

4|P ag e
C programming pointers notes

return 0;
}
output
Array values using pointer
*(p + 0) : 1000.000000
*(p + 1) : 2.000000
*(p + 2) : 3.400000
*(p + 3) : 17.000000
*(p + 4) : 50.000000
Array values using balance as address
*(balance + 0) : 1000.000000
*(balance + 1) : 2.000000
*(balance + 2) : 3.400000
*(balance + 3) : 17.000000
*(balance + 4) : 50.000000

Expression Equivalent Expression


Y=*P+1 Y=X+1
*P=*P+10 X=X+10

*P+=2 X+=2

++*P ++X

(*P)++ X++

Pointer Arithmetic in C

We can perform arithmetic operations on the pointers like addition, subtraction,


Incrementing Pointer in C

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

new_address= current_address + i * size_of(data type)

Incrementing Pointer Example

5|P ag e
C programming pointers notes

#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %u \n",p); // in our case, p will get
incremented by 4 bytes.
return 0;

Since p currently points to the location 0 after adding 1, the value will become 1, and
hence the pointer will point to the memory location 1.
Pointers to functions

function pointers to avoid code redundancy.

1) Unlike normal pointers, a function pointer points to code, not data.

Typically, a function pointer stores the start of executable code.

2) Unlike normal pointers, we do not allocate de-allocate memory using function


pointers.

3) A function's name can also be used to get functions' address.

Function pointers can be useful when you want to create callback mechanism, and need
to pass address of a function to another function.

#include <stdio.h>

/* function declaration */
double getAverage(int *arr, int size);

6|P ag e
C programming pointers notes

int main () {

/* an int array with 5 elements */


int balance[5] = {1000, 2, 3, 17, 50};
double avg;

/* pass pointer to the array as an argument */


avg = getAverage( balance, 5 ) ;

/* output the returned value */


printf("Average value is: %f\n", avg );
return 0;
}

double getAverage(int *arr, int size) {

int i, sum = 0;
double avg;

for (i = 0; i < size; ++i) {


sum += arr[i];
}

avg = (double)sum / size;


return avg;
}

C Pointers and Strings


A string is an array of char objects, ending with a null character ‘\ 0’.

A null pointer is a pointer which points nothing. Some uses of the null pointer are:

a) To initialize a pointer variable when that pointer variable isn't assigned any valid
memory address yet.

b) To pass a null pointer to a function argument when we don't want to pass any valid
memory address

Logically, the answer is no. char *a is not a string. It is a pointer variable.. which can be used
to point the character array.

char *a = “Rohit”
char a[5] = “Rohit”
Both are two different things.

7|P ag e
C programming pointers notes

What does char* mean?

char* is how you declaring a pointer to a char variable. It’s useful when you want a string with
unknown length.

Revision Question

8|P ag e
C programming pointers notes

Remember that &i will give the address of variable i. Build and run the following

program, show the output and answer the question.

#include <stdio.h>

void main(void)

int i = 7, j = 11;

printf("i = %d, j = %d\n", i, j);

printf("&i = %p, &j = %p\n", &i, &j); // %p means access address using a pointer

Code Analysis

As you may recall, each variable has a data type, name, value and address associated

with it. In the following Figure, inside the boxes, under the label of “value”, show the

values of i and j. Likewise, on the lines under the label marked “Address”, show the

addresses of i and j in RAM from your output.

9|P ag e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy