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

7 Key excerpts on "Pointer Array C"

  • Book cover image for: C Programming
    No longer available |Learn more

    C Programming

    A Self-Teaching Introduction

    These addresses are stored into formal argu- ments, which are nothing other than pointer variables. Now whatever changes are made in the formal arguments are reflected directly to the actual arguments. 3.8 ARRAY OF POINTERS Like a normal data-type array, we can also create an array of pointers. If we want to store the addresses of 5-integer variables, we write: int *ptr[5]; This declaration reserves 10 bytes of contiguous memory for 5 pointers. For example, consider the following program: #include void main( ) { int a = 10, b= 20, c=30,d= 40,e= 50}; int ptr[5]; ptr[0] = &a; ptr[1] = &b; ptr[2] = &c; ptr[3] = &d; ptr[4] = &e; for(int i=0; i<5;i++) printf(”\nValue stored at address=”, *ptr[i]); } OUTPUT (after running): Value stored at address = 10 Value stored at address = 20 Value stored at address = 30 Value stored at address = 40 Value stored at address = 50 244 • C PROGRAMMING 3.9 PASSING ARRAYS AS ARGUMENTS Just like ordinary variables, we can also pass a complete array by using its base address. For example, #include void main( ) { int a[5]; void aread(int *, int); void adisplay(int*, int); printf(“\nEnter any 5 integers:”); aread(a, 5); printf(“Array elements are :”); adisplay(a, 10); } void aread(int *aptr, int n) { for (int i=0; i
  • Book cover image for: Programming Microcontrollers in C
    • Ted VanSickle(Author)
    • 2001(Publication Date)
    • Newnes
      (Publisher)
    65 Chapter 2 Advanced C Topics Pointers The use of pointers sets the C language apart from most other high-level languages. Pointers, under the name of indirect addressing , are commonly used in assembly language. Most high-level languages completely ignore this powerful programming technique. In C, point-ers are simply variables that are the addresses of other variables. These variables can be assigned, be used as operands, and treated much the same as regular variables. Their use, however, can simplify greatly many of the truly difficult problems that arise in day-to-day program-ming. Let’s discuss pointers with the view that they offer us a powerful new tool to make our programming jobs easier. How To Use Pointers A pointer to a variable is the address of a variable. Thus far in our discussion of variables, the standard variable types were found to occupy 8-, 16-, or 32 bits depending on the type. Pointers are not types in the sense of variables. Pointers might occupy some number of bits, and their size is implementation dependent. In fact, there are cases of pointers to like types having different sizes in the same pro-gram depending on context. If the variable px is a pointer to the type i nt and x is an int , px can be assigned the address of x by px = &x; The ampersand (&) notifies the compiler to use the address of x rather than the value of x in the above assignment. The reverse op-eration to that above is 66 Chapter 2 Advanced C Topics x = *px; Here the asterisk (*) is a unary operator that applies to a pointer and directs the compiler to use the integer pointed to by the pointer px in the assignment. The unary * is referred to as the dereference operator. With these two operators, it is possible to move from vari-able to pointer and back again with ease.
  • Book cover image for: C++ for Financial Mathematics
    Chapter 11 Arrays, Strings, and Pointers One useful way to view C++ is as a collection of languages: (i) the C language; (ii) an object-oriented programming language; (iii) a template programming language. This chapter is about some of the C language aspects of C++. We have ig-nored them so far, because you should focus your efforts on the object-oriented features of C++. Similarly we will not discuss the template programming fea-tures until Chapter 17. Pointers are a central feature of the C language. They are a tool for directly manipulating computer memory. You will not find them in a more modern pure object-oriented programming language like Java. The feature is omitted from other languages because pointer-based programs are typically harder to understand than object-oriented programs. Most of what you learn in this chapter you should never use directly. The only thing you need to use is shared_ptr . We will discuss this towards the end of the chapter. Unfortunately, to understand shared_ptr you must first understand ordinary pointers. There are three other motivations for studying the material in this chapter. (i) The first is that C++ was originally designed for people with C pro-gramming experience. So some C++ code is designed to be intuitive to use if you have experience with C pointers. If you don’t study pointers, you will find things like the C++ standard library very unintuitive. (ii) The second is that job interviewers may assume that you know C and so may ask some trick questions about pointers, so it is good to be prepared! (iii) The third is that to write maximally efficient C++ programs it can be helpful to manage memory consciously. Pointers are a tool for direct memory manipulation. Because this chapter and the next are more about C programming than object-oriented programming, we’ve temporarily abandoned developing FM-Lib for this chapter. Instead the project PointerExamples.zip contains all code for this chapter. 175
  • Book cover image for: C++ for Financial Mathematics
    Arrays are only of interest if you know the size of your arrays when your code is compiled. For example, when working with 3D computer graphics, arrays of length 3 can be useful.
    Tip: Don’t use arrays Vectors do everything you want to do with arrays and much more.

    11.2   Pointers

    11.2.1   new and delete
    Because arrays are not flexible enough, the C language also contains the concept of a pointer. This allows a C programmer to work with sequences of data of varying lengths.
        int n = 5;    int * myArray = new int [n];    for (int i=0; i<n; i++) {        cout <<" Entry " <<i<<"=";        cout << myArray [i];        cout << "\n";    }    delete [] myArray ;
    The code above uses the new ...[] operator to allocate a chunk of memory. In this case we are creating a sequence of int data types in memory, but you could use other data types instead.
    The good news is that you can choose the size of the chunk of memory at run time. Unlike with an array, you don’t have to choose the size in advance when you write your program.
    The downside of using new [] is that the memory will not be automatically deleted when the variable goes out of scope (see Section 4.7 for the meaning of the word scope). You must use delete [] to free the memory created with the new[] operator. This is good and bad. With arrays we couldn’t return arrays because the memory was deleted automatically, but on the other hand we didn’t have to remember to call delete[] . With memory created using new[] we have to remember to delete the memory by hand, but you can safely return the data.
    The value returned by new int[n] is called a pointer . It has type int* which means “a pointer to an int ”. In actual fact the array discussed in the previous section was of type int* too. It’s just that the notation for arrays in C hides the fact that an array-valued variable is just a pointer to a memory location. All that the int* myArray contains is the memory address where the array starts. As a result, we have to remember ourselves that the block of memory is of length n
  • Book cover image for: Basic Computation and Programming with C
    You also note that accessing strings in both cases are also same. But allocations in memory are not same. In case of 2D array we need to declare a table whose number of columns is same as the length of the string which has the highest number of characters. But in case of array of pointers to string for each string requires memory according to its size. Thus if the length of the string varies a lot, a significant amount of memory will be wastage. In contrast to the above example if we want to store the same set of strings into a 2D array, we have to declare it as: char days[][10] = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” }; The memory map of the above declaration will be: S u n d a y 0 M o n d a y 0 T u e s d a y 0 W e d n e s d a y 0 T h u r s d a y 0 F r i d a y 0 S a t u r d a y 0 It is clear from the above figures that the allocation for 2D array is 70 bytes whereas in case of array of pointer it is 57. Another advantage we may find. Suppose we want to sort a set of strings. To do that, we need to exchange strings. In case of 2D array, we have to swap each character of those strings but in case of array of pointers only two addresses are need to exchange. Basic Computation and Programming with C 388 Thus we can see that array of pointers to string is much better than 2D character array to store multiple string of varying length. Now we will discuss some special types of pointer. 14.9.1 Void Pointer Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. This is also known as generic pointer. Except void pointer, all pointer variables can store the address of only corresponding data type. The address placed in a pointer must have the same type as the pointer. Consider the following example: int i; float f; int* p; p = &f; The above statement will produce an error. The address of the float variable is stored in an integer pointer that is incorrect.
  • Book cover image for: C++ Programming
    eBook - PDF

    C++ Programming

    Program Design Including Data Structures

    The statement stdPtr++; increments the value of stdPtr by 40 bytes. The increment operator increments the value of a pointer variable by the size of the data type or structure to which it is pointing. Similarly, the decrement operator decre-ments the value of a pointer variable by the size of the data type or structure to which it is pointing. Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Dynamic Arrays | 837 1 2 Moreover, the statement p = p + 2; increments the value of p (an int pointer) by 8 bytes. Thus, when an integer is added to a pointer variable, the value of the pointer variable is incremented by the integer times the size of the data type or structure to which the pointer is pointing. Similarly, when an integer is subtracted from a pointer variable, the value of the pointer variable is decremented by the integer times the size of the data type or structure to which the pointer is pointing. Pointer arithmetic can be very dangerous. Using pointer arithmetic, the program can acci-dentally access the memory locations of other variables and change their content without warning, leaving the programmer trying to find out what went wrong. If a pointer variable tries to access either the memory spaces of other variables or an illegal memory space, some systems might terminate the program with an appropriate error message. Always exercise extra care when doing pointer arithmetic. Dynamic Arrays In Chapter 8, you learned how to declare and process arrays. The arrays discussed in Chapter 8 are called static arrays because their size was fixed at compile time. One of the limitations of a static array is that every time you execute the program, the size of the array is fixed, so it might not be possible to use the same array to process differ-ent data sets of the same type. One way to handle this limitation is to declare an array that is large enough to process a variety of data sets.
  • Book cover image for: Brief C++
    eBook - PDF

    Brief C++

    Late Objects

    • Cay S. Horstmann(Author)
    • 2017(Publication Date)
    • Wiley
      (Publisher)
    • The array/pointer duality law states that a[n] is identical to *(a + n), where a is a pointer into an array and n is an integer offset. • When passing an array to a function, only the starting address is passed. Use C++ string objects with functions that process character arrays. • A value of type char denotes an individual character. Character literals are enclosed in single quotes. • A literal string (enclosed in double quotes) is an array of char values with a zero terminator. • Many library functions use pointers of type char*. • The c_str member function yields a char* pointer from a string object. • You can initialize C++ string variables with C strings. • You can access characters in a C++ string object with the [] operator. Allocate and deallocate memory in programs whose memory requirements aren’t known until run time. • Use dynamic memory allocation if you do not know in advance how many values you need. • The new operator allocates memory from the free store. • You must reclaim dynamically allocated objects with the delete or delete[] operator. • Using a dangling pointer (a pointer that points to memory that has been deleted) is a serious program- ming error. • Every call to new should have a matching call to delete. [0] [1] [2] [3] [4] [5] 'H' 'a' 'r' 'r' 'y' '\0' Points to 'H' char_pointer = 258 Chapter 7 Pointers and Structures Work with arrays and vectors of pointers. Draw diagrams for visualizing pointers and the data to which they point. • Draw the data that is being processed, then draw the pointer variables. When drawing the pointer arrows, illustrate a typical situation. Use structures to aggregate data items. • A structure combines member values into a single value. • You use the dot notation to access members of a structure. • When you assign one structure value to another, all members are assigned. Work with pointers to structures. • Use the -> operator to access a structure member through a pointer.
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.