Pointers and multidimensional arrays in C

In summary, the code demonstrates how to pass a pointer to a multi-dimensional array to a function. The main function initializes a 3 by 4 array and sets a pointer to point to its first element. The for loop in main calls the printarray_1 function with the pointer to the first element, which is then incremented to point to the next element with each loop iteration. The printarray_1 function uses a type cast to make the pointer *p equal to the address in ptr, and then uses a for loop to print the elements of the four-element array. Similarly, the printarray_2 function uses the same process to print the elements of the n by four-element array, with the number of elements determined by the value
  • #1
AK2
39
0
Code:
  /* Demonstrates passing a pointer to a multidimensional */
  /* array to a function. */

  #include <stdio.h>

  void printarray_1(int (*ptr)[4]);
  void printarray_2(int (*ptr)[4], int n);

  main()
 {
    int  multi[3][4] = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       { 9, 10, 11, 12 } };

    /* ptr is a pointer to an array of 4 ints. */

    int (*ptr)[4], count;

    /* Set ptr to point to the first element of multi. */

    ptr = multi;

    /* With each loop, ptr is incremented to point to the next */
    /* element (that is, the next 4-element integer array) of multi. */

[b]    for (count = 0; count < 3; count++)
        printarray_1(ptr++);

    puts("\n\nPress Enter...");
    getchar();
    printarray_2(multi, 3);
    printf("\n");
    return(0);
 }

 void printarray_1(int (*ptr)[4])
 {
 /* Prints the elements of a single four-element integer array. */
 /* p is a pointer to type int. You must use a type cast */
 /* to make p equal to the address in ptr. */

      int *p, count;
      p = (int *)ptr; 


      for (count = 0; count < 4; count++)
          printf("\n%d", *p++);
 }

 void printarray_2(int (*ptr)[4], int n)
 {
 /* Prints the elements of an n by four-element integer array. */

      int *p, count;
      p = (int *)ptr;

      for (count = 0; count < (4 * n); count++)
          printf("\n%d", *p++);
 } [/b]
The code above is from a textbook I am using to learn C language.
I don't have a good understanding of the bolded part. But since the practice in this forum is to say what I understand I will do just that.From the beginning there is a for loop. After it there is a line where there is a call to printarray_1. The argument passed is a pointer to multi[0] (ie the first element of multi[3][4] which is also a four element array). In the definition of the function void printarray_1 after the for loop the pointer *p is incremented how come it was able to print all the integer elements of multi[0], multi[1] and multi[2]. The same thing also in the definition of void printarray_2. Also in the definition of void printarray_2, the condition of the four loop why is it count<(4*3) .
 
Technology news on Phys.org
  • #2
AK2 said:
Code:
  /* Demonstrates passing a pointer to a multidimensional */
  /* array to a function. */

  #include <stdio.h>

  void printarray_1(int (*ptr)[4]);
  void printarray_2(int (*ptr)[4], int n);

  main()
 {
    int  multi[3][4] = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       { 9, 10, 11, 12 } };

    /* ptr is a pointer to an array of 4 ints. */

    int (*ptr)[4], count;

    /* Set ptr to point to the first element of multi. */

    ptr = multi;

    /* With each loop, ptr is incremented to point to the next */
    /* element (that is, the next 4-element integer array) of multi. */

[b]    for (count = 0; count < 3; count++)
        printarray_1(ptr++);

    puts("\n\nPress Enter...");
    getchar();
    printarray_2(multi, 3);
    printf("\n");
    return(0);
 }

 void printarray_1(int (*ptr)[4])
 {
 /* Prints the elements of a single four-element integer array. */
 /* p is a pointer to type int. You must use a type cast */
 /* to make p equal to the address in ptr. */

      int *p, count;
      p = (int *)ptr; 


      for (count = 0; count < 4; count++)
          printf("\n%d", *p++);
 }

 void printarray_2(int (*ptr)[4], int n)
 {
 /* Prints the elements of an n by four-element integer array. */

      int *p, count;
      p = (int *)ptr;

      for (count = 0; count < (4 * n); count++)
          printf("\n%d", *p++);
 } [/b]
The code above is from a textbook I am using to learn C language.
I don't have a good understanding of the bolded part. But since the practice in this forum is to say what I understand I will do just that.From the beginning there is a for loop. After it there is a line where there is a call to printarray_1. The argument passed is a pointer to multi[0] (ie the first element of multi[3][4] which is also a four element array). In the definition of the function void printarray_1 after the for loop the pointer *p is incremented how come it was able to print all the integer elements of multi[0], multi[1] and multi[2]. The same thing also in the definition of void printarray_2. Also in the definition of void printarray_2, the condition of the four loop why is it count<(4*3) .

One of the most important concepts in using pointers is understanding what a pointer points to, and in particular how big a chunk of memory the address in a pointer represents. How a pointer is declared plays a major role in expressions involving incrementing or decrementing a pointer, and pointer arithmetic.

In your code you have these declarations for p and ptr:
int * p;
and
int (*ptr)[4];

p is declared to be a pointer to an int. When it is initialized with an address (as in both your display functions), it points to a location in memory that can hold a 32-bit int. Just for the sake of discussion, let's suppose that p's value is 0x40001004. When p is incremented, it will be set with the next location in memory that can hold an int, or 0x40001008, which is 4 bytes higher in memory.

ptr is declared as a pointer to a four-element array of int. This means that when ptr gets initialized, it will be set to the address of a 16 byte chunk of memory. If, for the sake of discussion, ptr is initialized with the address 0x40002000, incrementing ptr would reset the value to 0x40002010, an address that is 16 bytes higher in memory. Note that I'm giving the memory addresses in hexadecimal, which is how they are usually represented, and that 1016 = 1610.

Inside your two functions, the local pointer variable p is assigned the value passed in the ptr parameter. Without the cast I think the compiler would generate a warning, with a pointer being assigned the value of a different kind of pointer.

Now for your questions. printarray_1 is designed to print the four ints in a four-element array of ints. In main, printarray_1 is called four times, once for each row in the two-dimensional array. In contrast, printarray_2 is designed to print all of the elements of a two-dimensional array with n rows, and with four ints in each row. For this reason, main calls printarray_2 just once, since printarray_2 will print all n * 4 elements of the two-d array.

Inside printarray_2, the for loop controls printing. If the two-d array that is passed has 5 rows, with 4 ints in each row, the for loop has to iterate 5 * 4 = 20 times. In this case, it is as if the loop had been written as
Code:
for (count = 0; count < 20; count++)
{
   .
   .
   .
}
 
  • #3
p is declared to be a pointer to an int. When it is initialized with an address (as in both your display functions), it points to a location in memory that can hold a 32-bit int.

Im using Windows XP and Vista for my programming lessons and both are 32 bit operating systems. An int is 4 bytes. I understand that multi points to multi[0] and multi[0] also points to multi[0][0]. Thanks for the explanation.
 

FAQ: Pointers and multidimensional arrays in C

What is a pointer in C?

A pointer in C is a variable that stores the address of another variable. It allows us to indirectly access and manipulate data by pointing to the memory location where the data is stored.

How do you declare a pointer in C?

To declare a pointer in C, we use the asterisk symbol (*) before the variable name. For example, int *ptr; declares a pointer variable named ptr that can store the address of an integer variable.

3. What is the purpose of multidimensional arrays in C?

A multidimensional array in C allows us to store and access data in multiple dimensions, such as in a table or matrix. It is useful for representing complex data structures and performing operations on them efficiently.

4. How do you declare a multidimensional array in C?

To declare a multidimensional array in C, we use multiple square brackets to specify the number of dimensions and the size of each dimension. For example, int arr[3][4]; declares a 2D array with 3 rows and 4 columns.

5. How are pointers and multidimensional arrays related in C?

In C, a multidimensional array is represented as a pointer to a pointer. This means that the name of the array is essentially a pointer to the first element of the array, and each element in the array is a pointer to the first element of the next dimension. This allows us to use pointer arithmetic to access and manipulate elements in a multidimensional array.

Similar threads

Replies
2
Views
1K
Replies
23
Views
2K
Replies
25
Views
2K
Replies
3
Views
2K
Replies
22
Views
3K
Replies
19
Views
4K
Back
Top