Thread: This 2D array is causing dismay: Exercise 17 in chapter 12

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    This 2D array is causing dismay: Exercise 17 in chapter 12

    Hello, I'm working on an exercise involving pointer arithmetic and 2D arrays. I can't use subscription and it must be done in a single loop. Here's the code that causing me heart ache:

    Code:
    #include <stdio.h>
    // C Programming - a modern approach, 2nd edition by K.N. King
    
    
    const int b[4][6]= {{1,2,3,4,5,6},
                    {1,2,3,4,5,6},
                    {1,2,3,4,5,6},
                    {1,2,3,4,5,6}};
    
    
    //very bottom page.268 a funct for 1 dimensional array works for 2 dimensional
    int sum_two_dimensional_array(const int a[], int n);
    
    
    int main(void){
        int total;
        int num =(sizeof(b)/sizeof(b[0]));
        total = sum_two_dimensional_array(b, num);  //p265
        printf("total = %d\n",total);
    }
    
    
    int sum_two_dimensional_array(const int a[], int n){ //p265 look for 'const'
        const int *p;
        int sum;
        p = a;
        for(p=0; p<a+n; p++){           // top of p264 and bottom of p.269
            printf("p=%d\n",*p);
            sum += *p;
        }
        printf("sum = %d\n",sum);
        return sum;
    }

    Here is the compiler log:
    Code:
    ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
    C:\work\programming-c\KN-King-C-programming\chapt-12-ex17.c||In function 'main':|
    C:\work\programming-c\KN-King-C-programming\chapt-12-ex17.c|15|warning: passing argument 1 of 'sum_two_dimensional_array' from incompatible pointer type [-Wincompatible-pointer-types]|
    C:\work\programming-c\KN-King-C-programming\chapt-12-ex17.c|10|note: expected 'const int *' but argument is of type 'const int (*)[6]'|
    ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    The compiler is telling me that in line 15 the parameter 'b' is from "incompatible pointer type". I'm miss understanding something from my book because this should work.


    Thank you for any insight.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,439
    What exactly are you trying to sum?

    The whole array?
    The first row?
    The first column?

    > int num =(sizeof(b)/sizeof(b[0]));
    This is only going to tell you how many rows your array has (ie, 4)
    If you want the number of ints in the whole array, use b[0][0] in the divisor expression.

    > sum_two_dimensional_array(b, num); //p265
    You're expecting a 1-D array.
    sum_two_dimensional_array(b[0], num); //p265



    > for(p=0; p<a+n; p++)
    You just set p to point to the array, and now you've set it back to being a null pointer.
    Maybe
    for(p=a; p<a+n; p++)

    > int sum;
    Initialise your local variables.
    Code:
    $ gcc -Wall -Wextra -O2 -g foo.c
    foo.c: In function ‘sum_two_dimensional_array’:
    foo.c:32:12: warning: ‘sum’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       32 |     return sum;
          |            ^~~
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Thank you. I was trying to total the whole array.

    It now works. I changed:
    Code:
    for(p = 0; p<a+n; p++){
    //to
    Code:
    for(p = a; p<a+n; p++){
    // I was getting tired. I can't believe I missed that.

    regarding:
    Code:
    int num =(sizeof(b)/sizeof(b[0]));
    //vs
    Code:
    int num =(sizeof(b)/sizeof(b[0][0]));
    // to be honest, I wasn't sure. I was going to change it once I got past the issue I created with the null pointer.

    I know I'm not supposed to randomly try stuff to much. I'm trying to understand more and do less dart throwing. Telling me about the null pointer mistake is what did it.

    On behalf of all the people trying to learn. Thank you for responding.

    Warm Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2016, 05:50 AM
  2. Replies: 3
    Last Post: 08-04-2016, 05:35 AM
  3. Jumping into C++ Chapter 14, exercise 6.
    By CppProgrammer88 in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2016, 07:27 PM
  4. Alex's exercise Chapter 6 Ex3
    By Valentas in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2013, 10:55 AM

Tags for this Thread