Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
35 views
Pointers in C Programming - What Is Pointer, Types - Examples
Uploaded by
Sadman Sakib
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Pointers in C Programming_ What is Pointer, Types ... For Later
Download
Save
Save Pointers in C Programming_ What is Pointer, Types ... For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
35 views
Pointers in C Programming - What Is Pointer, Types - Examples
Uploaded by
Sadman Sakib
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Pointers in C Programming_ What is Pointer, Types ... For Later
Carousel Previous
Carousel Next
Save
Save Pointers in C Programming_ What is Pointer, Types ... For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 14
Search
Fullscreen
ourv2? (y) Pointers in C Programming: What is Pointer, Types & Examples Whatis Pointer in C? The Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time. How to Use Pointers in C If we declare a variable v of type int, v will actually store a value. vis equal to zero now. However, each variable, apart from value, also has its address (or, simply put, where it is located in the memory). The address can be retrieved by putting an ampersand (&) before the variable name. FSV (//cdn.euru99.com/images/1/020819 0745 PointersinC2,pne) Ifyou print the address of a variable on the screen, it will look like a totally random number (moreover, it can be different from run to run). Let's try this in practice with pointer in C example(J/cdn.guru99,com/images/1/020819_0745_PointersinC3,png) The output of this program is 480613588. Now, what is a pointer? Instead of storing a value, a pointer will y store the address of a variable. Pointer Variable Int *y = &; VARIABLE POINTER Avalue stored in a named storage/memory _A variable that points to the storage/memory address address of another variable What You will Leam: [show Declaring a Pointer Like variables, pointers in C programming have to be declared before they can be used in your program. Pointers can be named anything you want as long as they obey C's naming rules. A pointer declaration has the following form. data_type * pointer_variable_name; Here,‘+ data_type is the pointer's base type of C's variable types and indicates the type of the variable that the pointer points to. * The asterisk (*: the same asterisk used for multiplication) which is indirection operator, declares a pointer. Let's see some valid pointer declarations in this C pointers tutorial: int *ptr_thing; /* pointer to an integer */ int *ptri,thing;/* ptr1 is a pointer to type integer and thing is an integer variable */ double *ptr2;—/* pointer to a double */ float — *ptr3; /* pointer to a float */ char ch 5 /* pointer to a character */ float ‘ptr, variable;/*ptr is a pointer to type float and variable is an ord inary float variable */ Initialize a pointer After declaring a pointer, we initialize it like standard variables with a variable address. If pointers in C programming are not uninitialized and used in the program, the results are unpredictable and potentially disastrous. To get the address of a variable, we use the ampersand (&)operator, placed before the name of avariable whose address we need. Pointer initialization is done with the following syntax. Pointer Syntax pointer = avariable; Asimple program for pointer illustration is given below:w#include
int main() { int a=16; //variable declaration int *p3 //pointer variable declaration p=8a; //store address of variable a in pointer p print#("Address stored in 2 variable p is:%x\n",p); //accessing the addre ss printf("Value stored in a variable p is:%d\n",*p); //accessing the value return @; Output: Address stored in a variable p is:60ffe8 Value stored in a variable p is:10 Operator Meaning * Serves 2 purpose 1. Declaration of a pointer 2, Returns the value of the referenced variable & Serves only 1 purpose * Returns the address of a variable Types of Pointers in C Following are the different Types of Pointers in C: Null Pointer We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0. Following program illustrates the use of a null pointer:#include
int main() { int *p = NULL; //null pointer printf(“The value inside variable p is:\n%x”,p); return 0; Output: The value inside variable p is: e Void Pointer In C programming (/c-programming-tutorial,html), a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable. Following program illustrates the use of a void pointer: #include
int main() { void *p = NULL; //void pointer printf ("The size of pointer is:%d\n",sizeof(p)); return Q; } Output: The size of pointer is:4 Wild pointer A pointer is said to be a wild pointer if it is not being initialized to anything. These types of C pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers.Following program illustrates the use of wild pointer: #include
int main() { int *p; //wild pointer printf("\n%d",*p) 5 return 0; } Output timeout: the monitored command dumped core sh: line 1: 95298 Segmentation fault timeout 10s main Other types of pointers in 'c’ are as follows: * Dangling pointer * Complex pointer * Near pointer * Far pointer * Huge pointer Direct and Indirect Access Pointers In C, there are two equivalent ways to access and manipulate a variable content * Direct access: we use directly the variable name * Indirect access: we use a pointer to the variable Let's understand this with the help of program below#include
/* Declare and initialize an int variable */ int var = 1; /* Declare a pointer to int */ int *ptr; int main( void ) { /* Initialize ptr to point to var */ ptr = &var; 7* Access var directly and indirectly */ printf("\nDirect access, var = %d", var); printf("\nIndirect access, var = %d", *ptr); /* Display the address of var two ways */ printf("\n\nThe address of var = %d", avar); print#("\nThe address of var = %d\n", ptr); /*change the content of var through the pointer*/ *ptr=48; printf("\nIndirect access, var = %d", *ptr); return @;} After compiling the program without any errors, the result is: Direct access, var = 1 Indirect access, var = 1 The address of var = 4202496 The address of var = 4202496 Indirect access, var = 48 Pointer Arithmetics in C The pointer operations are summarized in the following figureA) per_thing = &thing; Assigns thing’s address Variable to ptr_thing Pointer 0x1002 ox1002 B) other = *ptr_thing; Assigns to other the value pointed by ptr_thing Assigns new value to 1e pointed variable (J/cdn.guru99.com/images/1/020819 0745 PointersinC4.png) Pointer Operations Priority operation (precedence) When working with C pointers, we must observe the following priority rules: © The operators * and & have the same priority as the unary operators (the negation!, the incrementation++, decrement:-). * Inthe same expression, the unary operators *, &!, ++, - are evaluated from right to left. Ifa P pointer points to an X variable, then * P can be used wherever X can be written. The following expressions are equivalent:int X=10 int"P = &y; For the above code, below expressions are true Expression Y=*P+1 *P=*P+10 *P4=2 +P (PH Equivalent Expression Y=X+1 X=X+10 X+=2 +X XH In the latter case, parentheses are needed: as the unary operators * and ++ are evaluated from right to left, without the parentheses the pointer P would be incremented, not the object on which P points. Below table shows the arithmetic and basic operation that can be used when dealing with C pointers Operation Assignment Incrementation and decrementation Adding an offset (Constant) C Pointers & Arrays Explanation int *P1,*P2 P1=P2; P1 and P2 point to the same integer variable Int *P1; PL+45P1—; This allows the pointer to move N elements in a table. The pointer will be increased or decreased by N times the number of byte (s) of the type of the variable. P1+5; h Examples Traditionally, we access the array elements using its index, but this method can be eliminated by using pointers. Pointers make it easy to access each array element.#include
int main() { int a[5]=(1,2,3,4,5}; //array initialization int *p; //pointer declaration /*the ptr points to the first element of the array*/ p=a; /*We can also type simply ptr==8a[@] */ print#("Printing the array elements using pointer\n"); for(int i=0;i<5;i++) //loop for traversing array elements { printf("\n&x",*p); //printing array elements p++; //incrementing to the next element, you can also writ e peptl } return @; y Output 1 2 3 4 5 Adding a particular number to a pointer will move the pointer location to the value obtained by an addition operation. Suppose p is a pointer that currently points to the memory location 0 if we perform following addition operation, p+1 then it will execute in this manner: (Licdn.guru99.com/images/1/020819 0745 PointersinC5.png) Pointer Addition/Increment 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. C Pointers and Strings with ExamplesAstring is an array of char objects, ending with a null character '\ 0 We can manipulate strings using pointers. This pointer in C example explains this section #include
#include
int main() { char str[]="Hello Guru99!"; char *p3 pestr; printf("First character is:%c\n",*p); P =ptl; printf("Next character is:%c\n",*p); printf("Printing all the characters in a string\n"); p=str; //reset the pointer for(int i=@;icstrlen(str) ;i++) { printf ("%c\n",*p) 5 pees y return 0; y Output First character is:H Next character is:e Printing all the characters in a string 4 ore -wewcstca Another way to deal strings is with an array of pointers like in the following progra#include
int main(){ char *materials[ ] = { “iron”, “copper”, “gold"}; print#("Please remember these materials :\n"); int i; for (i i < 35 ist) { printf("%s\n", materials[ i ])3} return @;} Output: Please remember these material: iron copper gold Advantages of Pointers in C * Pointers are useful for accessing memory locations. * Pointers provide an efficient way for accessing the elements of an array structure. * Pointers are used for dynamic memory allocation as well as deallocation. * Pointers are used to form complex data structures such as linked list, graph, tree, etc. Disadvantages of Pointers in C * Pointers are alittle complex to understand. * Pointers can lead to various errors such as segmentation faults or can access a memory location which is not required at all. * fan incorrect value is provided to a pointer, it may cause memory corruption: * Pointers are also responsible for memory leakage. * Pointers are comparatively slower than that of the variables. * Programmers find it very difficult to work with the pointers; therefore it is programmer's responsibility to manipulate a pointer carefully. Summary * Apointer is nothing but a memory location where data is stored. * Apointer is used to access the memory location.* There are various types of pointers such as a null pointer, wild pointer, void pointer and other types of pointers. * Pointers can be used with array and string to access elements more efficiently. * We can create function pointers to invoke a function dynamically. * Arithmetic operations can be done on a pointer which is known as pointer arithmetic. * Pointers can also point to function which make it easy to call different functions in the case of defining an array of pointers. * When you want to deal different variable data type, you can use a typecast void pointer. € Prev Reporta Bug Next > You Might Like C Files I/O: Create, Open, Read, Write and Close a File (/c-file-input-output.html) Functions in C Programming with Examples: Recursive, Inline (/c-functions html) Pointers in C Programming: What is Pointer, Types & Examples (/c-pointers. html) Functions Pointers in C Programming with Examples (/c-function-pointers.htm|) f (https://www.facebook.com/guru99com/), W (https://twitter.com/guru99com) in (https://www.linkedin.com/company/guru99/), a (https://www.youtube.com/channel/UC19i1XD6k88KqHIET8atqFQ), Zz (https://forms.aweber.com/form/46/724807646.htm) About About Us (/about-us.html) Advertise with Us (/advertise-us.htm|) Write For Us (/become-an-instructor.htm!) Contact Us (/contact-us.html)Career Suggestion SAP Career Suggestion Tool (/best-sap-module.html) Software Testing as a Career (/software-testing-career- complete-guide.html) Interesting eBook (/ebook-pdf.html) Blog (/blog/) Quiz (/tests.html) SAP eBook (/sap-ebook-pdf,htm!) Exeaute online Exi va Online (/try-java-editor.html) Execute Javascript (/execute-javascript-online.html), Execute HTML (/execute-html-online.html) Execute Python (/execute-python-online,html) © Copyright - Guru99 2021 Privacy Policy (/privacy-policy.html) | Affiliate Disclaimer (affiliate-earning-disclaimer.html) | ToS (/terms-of-service.html)
You might also like
Pointers in C Programming
PDF
100% (1)
Pointers in C Programming
31 pages
Module 5 Pointers & Files
PDF
No ratings yet
Module 5 Pointers & Files
19 pages
PPS Unit-8 Pointers 2marks Ques With Ans
PDF
No ratings yet
PPS Unit-8 Pointers 2marks Ques With Ans
6 pages
Pointers
PDF
No ratings yet
Pointers
15 pages
cccc
PDF
No ratings yet
cccc
49 pages
Command Line Arguments
PDF
No ratings yet
Command Line Arguments
21 pages
Unit IV Prog in C
PDF
No ratings yet
Unit IV Prog in C
29 pages
C - Pointers
PDF
No ratings yet
C - Pointers
29 pages
Pointers
PDF
0% (1)
Pointers
23 pages
Module 6
PDF
No ratings yet
Module 6
87 pages
UNIT 4 C Language Pointers
PDF
No ratings yet
UNIT 4 C Language Pointers
17 pages
C Pointers 12
PDF
No ratings yet
C Pointers 12
11 pages
Pointer C
PDF
No ratings yet
Pointer C
21 pages
Lecture 3.1.1 Basics of Pointers
PDF
No ratings yet
Lecture 3.1.1 Basics of Pointers
24 pages
Pointers in C sample
PDF
No ratings yet
Pointers in C sample
8 pages
TTS_Module11-CC102
PDF
No ratings yet
TTS_Module11-CC102
16 pages
3025 - File - WEEK 7 To 8 POINTER
PDF
No ratings yet
3025 - File - WEEK 7 To 8 POINTER
13 pages
Pointer_Topic
PDF
No ratings yet
Pointer_Topic
22 pages
Pointers in C
PDF
No ratings yet
Pointers in C
3 pages
POINTERS
PDF
No ratings yet
POINTERS
30 pages
Unit 3 PC
PDF
No ratings yet
Unit 3 PC
70 pages
Pointeri
PDF
No ratings yet
Pointeri
13 pages
Pointers
PDF
No ratings yet
Pointers
60 pages
Pointers
PDF
No ratings yet
Pointers
8 pages
Unit 5: C Pointers
PDF
No ratings yet
Unit 5: C Pointers
37 pages
Pointers I
PDF
No ratings yet
Pointers I
33 pages
UNIT 5
PDF
No ratings yet
UNIT 5
43 pages
Unit 6
PDF
No ratings yet
Unit 6
11 pages
Pointers in C
PDF
No ratings yet
Pointers in C
38 pages
TCS 305-As04
PDF
No ratings yet
TCS 305-As04
28 pages
C - Pointer
PDF
No ratings yet
C - Pointer
29 pages
C Pointers Fundamentals Explained With Examples
PDF
No ratings yet
C Pointers Fundamentals Explained With Examples
16 pages
Complete - Unit 5 POINTER AND File Handling
PDF
No ratings yet
Complete - Unit 5 POINTER AND File Handling
16 pages
Pointers in C
PDF
No ratings yet
Pointers in C
32 pages
C - Pointers
PDF
No ratings yet
C - Pointers
4 pages
Unit 4 Complete Autonomous
PDF
No ratings yet
Unit 4 Complete Autonomous
33 pages
Chapter 1 Pointers and DMA
PDF
No ratings yet
Chapter 1 Pointers and DMA
48 pages
C Pointers
PDF
No ratings yet
C Pointers
5 pages
Pointers in C - C++
PDF
No ratings yet
Pointers in C - C++
41 pages
Pointers in C
PDF
No ratings yet
Pointers in C
14 pages
Unit 3
PDF
No ratings yet
Unit 3
29 pages
C lecture 5.1
PDF
No ratings yet
C lecture 5.1
13 pages
C - Pointing To Data
PDF
100% (2)
C - Pointing To Data
4 pages
Unit 3 of C
PDF
No ratings yet
Unit 3 of C
91 pages
Pointers
PDF
No ratings yet
Pointers
39 pages
C Day - 2 Topics Covered 1. Pointers 2. Structures 3. Unions 1.0 Introduction To Pointers
PDF
No ratings yet
C Day - 2 Topics Covered 1. Pointers 2. Structures 3. Unions 1.0 Introduction To Pointers
17 pages
Unit 6
PDF
No ratings yet
Unit 6
19 pages
C Programming Unit 4.1 Pointers
PDF
No ratings yet
C Programming Unit 4.1 Pointers
33 pages
Pointers in C
PDF
No ratings yet
Pointers in C
32 pages
Pointers Structures PDF
PDF
No ratings yet
Pointers Structures PDF
26 pages
UNIT 4 1 Pointers Stuctures Unions PDF
PDF
No ratings yet
UNIT 4 1 Pointers Stuctures Unions PDF
28 pages
CP Chapter6.pointers
PDF
No ratings yet
CP Chapter6.pointers
24 pages
CC102 Finals Reviewer
PDF
No ratings yet
CC102 Finals Reviewer
37 pages
lecture6 (1)
PDF
No ratings yet
lecture6 (1)
18 pages
Concept of Pointer
PDF
No ratings yet
Concept of Pointer
6 pages
POINTER
PDF
No ratings yet
POINTER
16 pages
Pointers
PDF
No ratings yet
Pointers
20 pages
Programming Fundamentals Manual 09
PDF
No ratings yet
Programming Fundamentals Manual 09
9 pages
Unit 6 C Programming
PDF
No ratings yet
Unit 6 C Programming
8 pages
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
Pointers in C Programming
PDF
Pointers in C Programming
Module 5 Pointers & Files
PDF
Module 5 Pointers & Files
PPS Unit-8 Pointers 2marks Ques With Ans
PDF
PPS Unit-8 Pointers 2marks Ques With Ans
Pointers
PDF
Pointers
cccc
PDF
cccc
Command Line Arguments
PDF
Command Line Arguments
Unit IV Prog in C
PDF
Unit IV Prog in C
C - Pointers
PDF
C - Pointers
Pointers
PDF
Pointers
Module 6
PDF
Module 6
UNIT 4 C Language Pointers
PDF
UNIT 4 C Language Pointers
C Pointers 12
PDF
C Pointers 12
Pointer C
PDF
Pointer C
Lecture 3.1.1 Basics of Pointers
PDF
Lecture 3.1.1 Basics of Pointers
Pointers in C sample
PDF
Pointers in C sample
TTS_Module11-CC102
PDF
TTS_Module11-CC102
3025 - File - WEEK 7 To 8 POINTER
PDF
3025 - File - WEEK 7 To 8 POINTER
Pointer_Topic
PDF
Pointer_Topic
Pointers in C
PDF
Pointers in C
POINTERS
PDF
POINTERS
Unit 3 PC
PDF
Unit 3 PC
Pointeri
PDF
Pointeri
Pointers
PDF
Pointers
Pointers
PDF
Pointers
Unit 5: C Pointers
PDF
Unit 5: C Pointers
Pointers I
PDF
Pointers I
UNIT 5
PDF
UNIT 5
Unit 6
PDF
Unit 6
Pointers in C
PDF
Pointers in C
TCS 305-As04
PDF
TCS 305-As04
C - Pointer
PDF
C - Pointer
C Pointers Fundamentals Explained With Examples
PDF
C Pointers Fundamentals Explained With Examples
Complete - Unit 5 POINTER AND File Handling
PDF
Complete - Unit 5 POINTER AND File Handling
Pointers in C
PDF
Pointers in C
C - Pointers
PDF
C - Pointers
Unit 4 Complete Autonomous
PDF
Unit 4 Complete Autonomous
Chapter 1 Pointers and DMA
PDF
Chapter 1 Pointers and DMA
C Pointers
PDF
C Pointers
Pointers in C - C++
PDF
Pointers in C - C++
Pointers in C
PDF
Pointers in C
Unit 3
PDF
Unit 3
C lecture 5.1
PDF
C lecture 5.1
C - Pointing To Data
PDF
C - Pointing To Data
Unit 3 of C
PDF
Unit 3 of C
Pointers
PDF
Pointers
C Day - 2 Topics Covered 1. Pointers 2. Structures 3. Unions 1.0 Introduction To Pointers
PDF
C Day - 2 Topics Covered 1. Pointers 2. Structures 3. Unions 1.0 Introduction To Pointers
Unit 6
PDF
Unit 6
C Programming Unit 4.1 Pointers
PDF
C Programming Unit 4.1 Pointers
Pointers in C
PDF
Pointers in C
Pointers Structures PDF
PDF
Pointers Structures PDF
UNIT 4 1 Pointers Stuctures Unions PDF
PDF
UNIT 4 1 Pointers Stuctures Unions PDF
CP Chapter6.pointers
PDF
CP Chapter6.pointers
CC102 Finals Reviewer
PDF
CC102 Finals Reviewer
lecture6 (1)
PDF
lecture6 (1)
Concept of Pointer
PDF
Concept of Pointer
POINTER
PDF
POINTER
Pointers
PDF
Pointers
Programming Fundamentals Manual 09
PDF
Programming Fundamentals Manual 09
Unit 6 C Programming
PDF
Unit 6 C Programming