Computer Science

Pointer Array C

A pointer array in C is an array that contains pointers to other variables. Each element of the array is a pointer that points to a memory location where the actual data is stored. This allows for efficient manipulation of data structures and dynamic memory allocation.

Written by Perlego with AI-assistance

5 Key excerpts on "Pointer Array C"

  • C++ Programming
    eBook - ePub
    • Li Zheng, Yuan Dong, Fang Yang(Authors)
    • 2019(Publication Date)
    • De Gruyter
      (Publisher)
    6Arrays, Pointers, and Strings
    After studying the concepts and applications of basic control structures, functions, and classes of C++, many problems can be described and solved. However, for large scales of data, especially when they are mutually correlated or are similar and correlated data, how to present and organize them efficiently remains a problem. The array type in C++ provides an effective way to organize objects of the same type.
    An important characteristic, which C++ inherits from C, is that we can directly use an address to access memory, and the pointer variable is an important data type for realizing this feature. Using pointers, we can conveniently process large amounts of data continuously stored in memory, achieve massive data sharing between functions at a comparatively low cost, and easily realize dynamic memory allocations.
    Using character arrays to make up the deficiency of string variables is an effective method inherited from C. However, from the object-oriented and security perspectives, strings represented by character arrays have deficiencies. Therefore, the standard class library of C++ provides the string class, which is a good example of expanding data types based on the class library.
    In this chapter, we will introduce the array type, pointer type, dynamic memory allocation, and how to store and process string data.

    6.1Arrays

    To understand the function of an array, please consider this problem: how does one store and process a series of n integers in a program? If n is small, for example, n is 3, we can easily declare three variables of the int type. If n is 10,000, then we need to declare 10,000 variables of int type to represent these 10,000 numbers by int
  • Learn C Programming
    Chapter 14 : Understanding Arrays and Pointers
    C pointers and arrays are closely related; so closely, in fact, that they are often interchangeable! However, this does not mean pointers and arrays are identical. We will explore this relationship and why we might want to use either array notation or pointer notation to access array elements. Most often, we will need to understand this relationship when we pass arrays to functions. So, understanding the interchangeability of arrays and pointers will be essential to making any function that manipulates arrays more flexible and expressive.
    Having read two previous chapters, Chapter 11 Working with Arrays , and Chapter 13 Using Pointers , is essential to understanding the concepts presented here. Please do not skip those chapters before reading this one.
    The following topics will be covered in this chapter:
    • Reviewing the memory layout of arrays
    • Understanding the relationship between array names and pointers
    • Using pointers in various ways to access and traverse arrays
    • Expanding the use of pointer arithmetic, specifically for array elements
    • Creating an array of pointers to arrays and a two-dimensional (2D ) array to highlight their differences

    Technical requirements

    Continue to use the tools you chose from the Technical requirements  section of Chapter 1 Running Hello, World! .
    The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter14 .

    Understanding array names and pointers

    As we have seen, elements of arrays can always be accessed via indices and traversed by means of an integer offset from the zeroth element. Sometimes, however, it is more convenient to access array elements via a pointer equivalent.
  • Introduction to Windows® and Graphics Programming with Visual C++®
    • Roger Mayne(Author)
    • 2015(Publication Date)
    • WSPC
      (Publisher)
    In reality, like all variables of type float, the entries in Vector[5] each occupy four bytes in memory (where a “byte” contains eight “bits”, i.e. ones and zeros) with two bytes used for the number itself and two bytes used for its mantissa. One special feature of using pointers to specify addresses in memory is that incrementing a pointer will automatically move within memory from one variable in an array to the next. It is not necessary to worry about the byte-to-byte details. This is true regardless of the variable type (integer, float, double, etc.) as well for structures and objects which will be discussed shortly. This is also the reason why pointers are always declared in association with a particular type of variable, structure or object.
    Figure 2.3 “Pointing” to Locations in Memory for Entries in the Array Vector[5]
    2.2.3Using a pointer with a one-dimensional array
    In Section 2.1 we worked with a simple one-dimensional array and in Section 2.2.2 we discussed the “pointer” concept for addressing the entries in an array. In this section we will bring the two ideas together in a simple program and use a pointer to allow a function to have direct access to an array. The program below is from the example project VecPointer that you can find in the folder Chap2\3_VecPointer. Double clicking on VecPointer.sln will make the file VecPointer.cpp available to you. The listing of the file is shown in Table 2.3 . The VecPointer example performs the same operations as the VectorG example of Section 2.1 . However, in this case, the array Vector is not declared globally. Instead, it is declared locally in main() and passed to the function now called PtVecFunction by use of a pointer to the array.
    In looking through the listing of VecPointer.cpp in Table 2.3 , we see that the floating point array Vector[5] is only declared locally in main() at line 08. Line 09 declares the floating point pointer PtVector. In line 10 the pointer PtVector is set to the address of the first entry in the array. In summary, after lines 08-10, the pointer PtVector contains the address of the array entry Vector[0].
    The function PtVecFunction is initially declared at line 04 and is shown in prototype form in lines 25-35. It has been prepared to manipulate the one-dimensional array by having access to it through a pointer. The input argument “float*” shown for PtVecFunction in line 04 means that a floating point pointer is expected as an input to PtVecFunction. In the redeclaration of PtVecFunction in line 25, the pointer is given the variable name Ptr for use within the function. The for loop of lines 30–34 then manipulates the contents of the array Vector[5] in line 32 using Ptr. It addresses each entry using the expression *(Ptr + i) and adds two to its contents. In this expression, the “*” should be read as “contents of” according to the discussion of Section 2.2.1
  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    11 Pointers
    DOI: 10.1201/9781003188254-11

    11.1 Introduction

    A pointer is something that indicates or points. From the layman’s point of view a pointer is a long-tapered stick for indicating objects, as on a chart or a blackboard (Figure 11.1 ). Sometimes we use a laser pointer to point or show an object displayed on a screen (Figure 11.1). So, we can say a pointer is indirectly pointing to a device of our concerns.
    Figure 11.1 Pointing device.
    To understand the way in which pointers operate, it is first necessary to understand the concept of indirection. You are familiar with this concept from your everyday life. For example, suppose you need to buy a new computer for your department. In the company that you work for, all purchases are handled by the purchasing department. So, you call Mr. X in purchasing and ask him to order the new computer for you. Mr. X, in turn, calls the local supply store to order the computer. This approach to obtain your new computer is actually an indirect one because you are not ordering the computer directly from the supply store yourself.
    This same notion of indirection applies to the way pointers work in C. A pointer provides an indirect means of accessing the value of a particular data item. In C programming, a pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element. Pointers play an important role in the development process. A pointer has a number of useful applications. For example, pointers can be used to pass information back and forth between a function and its reference point. In particular, pointers provide a way to return multiple data items from a function via function arguments.

    11.2 Basic Knowledge

    Within the computer’s memory, every stored data item occupies one or more contiguous memory cells (i.e., adjacent words or bytes). The number of memory cells required to store a data item depends on the type of data item. For example, a single character will typically be stored in one byte (eight bits) of memory; an integer usually requires four contiguous bytes; a floating-point number requires four contiguous bytes; and a double-precision quantity requires eight contiguous bytes.
  • Ivor Horton's Beginning Visual C++ 2008
    • Ivor Horton(Author)
    • 2011(Publication Date)
    • Wrox
      (Publisher)
    Of course, all this applies to pointers to any type. A pointer to type char is used here purely for illustrative purposes. Pointers and Arrays
    Array names can behave like pointers under some circumstances. In most situations, if you use the name of a one-dimensional array by itself, it is automatically converted to a pointer to the first element of the array. Note that this is not the case when the array name is used as the operand of the sizeof operator.
    If you have these declarations,
    double* pdata; double data[5];
    you can write this assignment:
    pdata = data; // Initialize pointer with the array address
    This is assigning the address of the first element of the array data to the pointer pdata . Using the array name by itself refers to the address of the array. If you use the array name data with an index value, it refers to the contents of the element corresponding to that index value. So, if you want to store the address of that element in the pointer, you have to use the address-of operator:
    pdata = &data[1];
    Here, the pointer pdata contains the address of the second element of the array.
    Pointer Arithmetic
    You can perform arithmetic operations with pointers. You are limited to addition and subtraction in terms of arithmetic, but you can also perform comparisons of pointer values to produce a logical result. Arithmetic with a pointer implicitly assumes that the pointer points to an array, and that the arithmetic operation is on the address contained in the pointer. For the pointer pdata for example, you could assign the address of the third element of the array data to a pointer with this statement:
    pdata = &data[2];
    In this case, the expression pdata+1 would refer to the address of data[3] , the fourth element of the data array, so you could make the pointer point to this element by writing this statement:
    pdata += 1; // Increment pdata to the next element
    This statement increments the address contained in pdata by the number of bytes occupied by one element of the array data . In general, the expression pdata+n , where n can be any expression resulting in an integer, adds n*sizeof(double) to the address contained in the pointer pdata because it was declared to be of type pointer to double . This is illustrated in Figure 4-8
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.