Ted Jensen Tutorial's journal picture

Ted Jensen Tutorial

followFollow
🤴
Administrator: eZine
🕒
Created 2 Jul 2022
📄
14 Articles
This tutorial covers the basics of using pointers and arrays in the C programming language. All chapters are released by the autor (Ted Jensen) to the Public Domain.
show more

A TUTORIAL ON POINTERS AND ARRAYS IN C

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.2 (HTML version) Feb. 2000 This material is hereby placed in the public domain Available in various formats via http://www.netcom.com/~tjensen/ptr/cpoint.htm TABLE OF CONTENTS Preface Introduction Chapter 1: What is a Pointer? Chapter 2: Pointer Types and Arrays. Chapter 3: Pointers and Strings Chapter 4: More on Strings Chapter 5: Pointers and Structures Chapter 6: More on Strings and Arrays of Strings Chapter 7: More on Multi-Dimensional Arrays Chapter 8: Pointers to Arrays Chapter 9: Pointers and Dynamic Allocation of Memory Chapter 10: Pointers to Func...

Preface

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
This document is intended to introduce pointers to beginning programmers in the C programming language. Over several years of reading and contributing to various conferences on C including those on the FidoNet and UseNet, I have noted a large number of newcomers to C appear to have a difficult time in grasping the fundamentals of pointers. I therefore undertook the task of trying to explain them in plain language with lots of examples. The first version of this document was placed in the public domain, as is this one. It was picked up by Bob Stout who included it as a file called PTR-HELP.TXT in his widely distributed collection of SNIPPE...

Introduction

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
If you want to be proficient in the writing of code in the C programming language, you must have a thorough working knowledge of how to use pointers. Unfortunately, C pointers appear to represent a stumbling block to newcomers, particularly those coming from other computer languages such as Fortran, Pascal or Basic. To aid those newcomers in the understanding of pointers I have written the following material. To get the maximum benefit from this material, I feel it is important that the user be able to run the code in the various listings contained in the article. I have attempted, therefore, to keep all code ANSI compliant so that it wil...

Chapter 1: What is a Pointer?

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
One of those things beginners in C find difficult is the concept of pointers. The purpose of this tutorial is to provide an introduction to pointers and their use to these beginners. I have found that often the main reason beginners have a problem with pointers is that they have a weak or minimal feeling for variables, (as they are used in C). Thus we start with a discussion of C variables in general. A variable in a program is something with a name, the value of which can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. The size of tha...

Chapter 2: Pointer Types and Arrays

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
Okay, let's move on. Let us consider why we need to identify the type of variable that a pointer points to, as in: int *ptr; One reason for doing this is so that later, once ptr "points to" something, if we write: *ptr = 2; the compiler will know how many bytes to copy into that memory location pointed to by ptr . If ptr was declared as pointing to an integer, 4 bytes would be copied. Similarly for floats and doubles the appropriate number will be copied. But, defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting of ten ...

Chapter 3: Pointers and Strings

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
The study of strings is useful to further tie in the relationship between pointers and arrays. It also makes it easy to illustrate how some of the standard C string functions can be implemented. Finally it illustrates how and when pointers can and should be passed to functions. In C, strings are arrays of characters. This is not necessarily true in other languages. In BASIC, Pascal, Fortran and various other languages, a string has its own data type. But in C it does not. In C a string is an array of characters terminated with a binary zero character (written as '\0' ). To start off our discussion we will write some code which, wh...

Chapter 4: More on Strings

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
Well, we have progressed quite a way in a short time! Let's back up a little and look at what was done in Chapter 3 on copying of strings but in a different light. Consider the following function: char *my_strcpy(char dest[], char source[]) { int i = 0; while (source[i] != '\0') { dest[i] = source[i]; i++; } dest[i] = '\0'; return dest; } Recall that strings are arrays of characters. Here we have chosen to use array notation instead of pointer notation to do the actual copying. The results are the same, i.e. the string gets copied using this notation just as accurately as it did before. This raises some interesting poi...

Chapter 5: Pointers and Structures

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
As you may know, we can declare the form of a block of data containing different data types by means of a structure declaration. For example, a personnel file might contain structures which look something like: struct tag { char lname[20]; /* last name */ char fname[20]; /* first name */ int age; /* age */ float rate; /* e.g. 12.75 per hour */ }; Let's say we have a bunch of these structures in a disk file and we want to read each one out and print out the first and last name of each one so that we can have a list of the people in our files. The remaining information will not be printed out. We will wan...

Chapter 6: More on Strings and Arrays of Strings

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
Well, let's go back to strings for a bit. In the following all assignments are to be understood as being global, i.e. made outside of any function, including main(). We pointed out in an earlier chapter that we could write: char my_string[40] = "Ted"; which would allocate space for a 40 byte array and put the string in the first 4 bytes (three for the characters in the quotes and a 4th to handle the terminating '\0' ). Actually, if all we wanted to do was store the name "Ted" we could write: char my_name[] = "Ted"; and the compiler would count the characters, leave room for the nul character and store the total of the four cha...

Chapter 7: More on Multi-Dimensional Arrays

eZine's profile picture
eZine lover (@eZine)
Published in 
 · 2 Jul 2022
In the previous chapter we noted that given #define ROWS 5 #define COLS 10 int multi[ROWS][COLS]; we can access individual elements of the array multi using either: multi[row][col] or *(*(multi + row) + col) To understand more fully what is going on, let us replace *(multi + row) with X as in: *(X + col) Now, from this we see that X is like a pointer since the expression is de-referenced and we know that col is an integer. Here the arithmetic being used is of a special kind called "pointer arithmetic" is being used. That means that, since we are talking about an integer array, the address pointed to by (i.e. value of) X + col + 1 must be ...
loading
Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT