Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C Programming – Pointers

By Brian Moriya | on May 25, 2006 |
C Language

Pointers are widely used in programming; they are used to refer to memory location of another variable without using variable identifier itself. They are mainly used in linked lists and call by reference functions.

Diagram 1 illustrates the idea of pointers. As you can see below; Yptr is pointing to memory address 100.

Diagram 1: 1. Pointer and memory relationship

Pointer Declaration

Declaring pointers can be very confusing and difficult at times (working with structures and pointer to pointers). To declare pointer variable we need to use * operator (indirection/dereferencing operator) before the variable identifier and after data type. Pointer can only point to variable of the same data type.

Syntax
  1. Datatype * identifier;
Example

Character Pointer

  1.  #include <stdio.h>
  2. int main()
  3. {
  4. char a='b';
  5. char *ptr;
  6. printf("%cn",a);
  7. ptr=&a;
  8. printf("%pn",ptr);
  9. *ptr='d';
  10. printf("%cn",a);                                                   
  11. return 0;
  12. }
Output

Figure 1 – Code Result

Description of code: In line 1 we are declaring char variable called a; it is initialized to character ‘b’, in line 2, the pointer variable ptr is declared. In line 4, the address of variable a is assigned to variable ptr. In line 6 value stored at the memory address that ptr points to is changed to ‘d’

Note: & notation means address-of operand in this case &a means ‘address-of a’.

Address operator

Address operator (&) is used to get the address of the operand. For example if variable x is stored at location 100 of memory; &x will return 100.

This operator is used to assign value to the pointer variable. It is important to remember that you MUST NOT use this operator for arrays, no matter what data type the array is holding. This is because array identifier (name) is pointer variable itself. When we call for ArrayA[2]; ‘ArrayA’ returns the address of first item in that array so ArrayA[2] is the same as saying ArrayA+=2; and will return the third item in that array.

Pointer arithmetic

Pointers can be added and subtracted. However pointer arithmetic is quite meaningless unless performed on arrays. Addition and subtraction are mainly for moving forward and backward in an array.

Note: you have to be very careful NOT to exceed the array elements when you use arithmetic; otherwise you will get horrible errors such as “access violation”. This error is caused because your code is trying to access a memory location which is registered to another program.

Operator

Result

++

Goes to the next memory location that the pointer is pointing to.

—

Goes to the previous memory location that the pointer is pointing to.

-= or –

Subtracts value from pointer.

+= or +

Adding to the pointer

Example:

Array and pointer arithmetic

  1.  #include <stdio.h>
  2. int main()
  3. {
  4. int ArrayA[3]={1,2,3};
  5. int *ptr;
  6. ptr=ArrayA;
  7. printf("address: %p - array value:%d n",ptr,*ptr);
  8. ptr++;
  9. printf("address: %p - array value:%d n",ptr,*ptr);                                
  10. return 0;
  11. }
Output

Figure 2 Code 2 result

Description of ‘code 2’: in line 1 we are declaring ‘ArrayA’ integer array variable initialized to numbers ‘1,2,3’, in line 2, the pointer variable ptr is declared. In line 3, the address of variable ‘ArrayA’ is assigned to variable ptr. In line 5 ptr is incremented by 1.
Note: & notation should not be used with arrays because array’s identifier is pointer to the first element of the array.

Pointers and functions

Pointers can be used with functions. The main use of pointers is ‘call by reference’ functions. Call by reference function is a type of function that has pointer/s (reference) as parameters to that function. All calculation of that function will be directly performed on referred variables.

  1.  #include <stdio.h>
  2. void DoubleIt(int *num)
  3. {
  4. 	*num*=2; 
  5. }
  6. int main()
  7. { 
  8. 	int number=2; 
  9. 		DoubleIt(&number);
  10. 			printf("%d",number);
  11. return 0; 
  12. }
Output

Figure 3 code 3 result

Description of ‘code 3’: in line 1 we are declaring ‘DoubleIt’ function, in line 4, the variable number is declared and initialized to 2. In line 5, the function DoubleIt is called.

Pointer to Arrays

Array identifier is a pointer itself. Therefore & notation shouldn’t be used with arrays. The example of this can be found at code 3. When working with arrays and pointers always remember the following:

  • Never use & for pointer variable pointing to an array.
  • When passing array to function you don’t need * for your declaration.
  • Be VERY CAREFUL not to exceed number of elements your array holds when using pointer arithmetic to avoid errors.

Pointers and Structures

Pointers and structures is broad topic and it can be very complex to include it all in one single tutorial. However pointers and structures are great combinations; linked lists, stacks, queues and etc are all developed using pointers and structures in advanced systems.

Example:

Number Structure

  1.  #include <stdio.h>
  2.  
  3. struct details {
  4. int num;
  5. };
  6.  
  7. int main()
  8. { 
  9.  
  10. struct details MainDetails;
  11. struct details *structptr;
  12. structptr=&MainDetails;
  13. structptr->num=20; 
  14. printf("n%d",MainDetails.num);
  15.  
  16.  
  17. return 0; 
  18. }
Output

Figure 4. code 4 result

Description of ‘code 4’: in line 1-3 we are declaring ‘details’ structure, in line 4, the variable Maindetails is declared.in line 6, pointer is set to point to MainDetails. In line 7, 20 is assigned to MainDetails.num through structptr->num.

Pointer to Pointer

Pointers can point to other pointers; there is no limit whatsoever on how many ‘pointer to pointer’ links you can have in your program. It is entirely up to you and your programming skills to decide how far you can go before you get confused with the links. Here we will only look at simple pointer to pointer link. Pointing to pointer can be done exactly in the same way as normal pointer. Diagram below can help you understand pointer to pointer relationship.

Diagram 2. Simple pointer to pointer relationship

Code for diagram 2:

Char *ptrA;
Char x=’b’;
Char *ptrb;
ptrb=&x;
ptrA=&ptrb;

ptrA gives 100, *ptrA gives 101 , ptrb is 101 ,*ptrb gives b, **ptrA gives b
comment: **ptrA means ‘value stored at memory location of value stored in memory location stored at PtrA’

« « C Programming – Structures and Unions
C Programming – Dynamic Memory allocation » »

Author Description

Avatar

Free Training

RSSSubscribe 392 Followers
  • Popular
  • Recent
  • Concept of Pixel in C Graphics

    July 11, 2006 - 0 Comment
  • C Programming – Arrays

    April 13, 2006 - 0 Comment
  • TSR in C – An Introduction

    July 11, 2006 - 0 Comment
  • C Programming – Handling of Character String

    April 17, 2006 - 0 Comment
  • C Programming – Data Types : Part 2

    August 21, 2011 - 0 Comment
  • C Programming – An Overview

    March 2, 2006 - 0 Comment
  • C Programming – Functions (Part-II)

    May 22, 2006 - 0 Comment
  • C Doubly Linked Lists

    June 26, 2011 - 0 Comment
  • C Programming – Constants and Identifiers

    March 2, 2006 - 0 Comment
  • C Programming – Functions (Part-I)

    May 23, 2006 - 0 Comment
  • C Programming – Data Types : Part 2

    August 21, 2011 - 0 Comment
  • C Circular Linked Lists

    June 26, 2011 - 0 Comment
  • C Doubly Linked Lists

    June 26, 2011 - 0 Comment
  • TSR in C – An Introduction

    July 11, 2006 - 0 Comment
  • Concept of Pixel in C Graphics

    July 11, 2006 - 0 Comment
  • Call by Value and Call by Reference

    July 6, 2006 - 0 Comment
  • C Language – The Preprocessor

    May 31, 2006 - 0 Comment
  • C Programming – File management in C

    May 31, 2006 - 0 Comment
  • C Programming – Linked Lists

    May 29, 2006 - 0 Comment
  • C Programming – Dynamic Memory allocation

    May 29, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C Programming – Data Types : Part 2
  • C Circular Linked Lists
  • C Doubly Linked Lists
  • TSR in C – An Introduction
  • Concept of Pixel in C Graphics

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT