Pointers and multidimensional arrays in C

Click For Summary
SUMMARY

This discussion focuses on the use of pointers with multidimensional arrays in C, specifically how to pass a pointer to a multidimensional array to functions. The code demonstrates two functions, printarray_1 and printarray_2, which print elements of a 3x4 integer array. The pointer ptr is incremented to access each row of the array, while printarray_2 calculates the total number of elements to print using the formula count < (4 * n), where n is the number of rows.

PREREQUISITES
  • Understanding of C programming language syntax
  • Knowledge of pointers and memory management in C
  • Familiarity with multidimensional arrays in C
  • Basic understanding of function parameters and return types in C
NEXT STEPS
  • Study pointer arithmetic in C, focusing on how pointer types affect memory addressing
  • Learn about dynamic memory allocation and its relationship with pointers
  • Explore advanced topics in C, such as function pointers and callback functions
  • Practice implementing and manipulating multidimensional arrays in C
USEFUL FOR

C programmers, computer science students, and software developers looking to deepen their understanding of pointers and multidimensional arrays in C.

AK2
Messages
39
Reaction score
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
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++)
{
   .
   .
   .
}
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
7
Views
5K
Replies
5
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
20
Views
2K
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K