protecting array contents

This is a discussion on protecting array contents within the C Programming forums, part of the General Programming Boards category; Hi, I've done a programming exercise from a book on C programming (see code below). It compiles and runs. What ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25

    protecting array contents

    Hi,
    I've done a programming exercise from a book on C programming (see code below). It compiles and runs. What I wanted to ask about was I get some compiler warnings and I can't figure out why they come up. I'd appreciate some hints please.
    Code:
    /* pe6b.c -- Write a program that initializes a two-dimensional array-of-double and uses one of the copy functions from exercise 2 to copy it to a second two-dimensional array. (Because a two-dimensional array is an array of arrays, a one-dimensional copy function can be used with each subarray.) */
    #include <stdio.h>
    
    #define ROW 5
    #define COL 5
    
    void copy_arr(const double s[], double t[], int n);
    void show_arr(const double ptr[][COL], int row);
    
    int main(void)
    {
    	int i;
    	double source[ROW][COL] = {
    		{1.1,2.2,3.3,4.4,5.5},
    		{6.6,7.7,8.8,9.9,10.1},
    		{11.1, 12.1,13.1,14.1,15.1},
    		{16.1,17.1,18.1,19.1,20.1},
    		{21.1,22.1,23.1,24.1,25.1}
    	};
    	double target[ROW][COL];
    
    	for (i = 0; i < ROW; i++)
    		copy_arr(source[i], target[i], ROW);
    
    	printf("SOURCE\n");
    	show_arr(source, ROW);
    	printf("\nTARGET\n");
    	show_arr(target, ROW);
    
    	return 0;
    }
    
    void copy_arr(const double s[], double t[], int n)
    {
    	int c;
    	for (c = 0; c < COL; c++)
    		t[c] = s[c];
    }
    
    void show_arr(const double ptr[][COL], int row)
    {
    	int r, c;
    	for (r = 0; r < row; r++)
    		for (c = 0; c < COL; c++)
    			printf("%g ", ptr[r][c]);
    	putchar('\n');
    }
    Here are the warnings I get when I compile it on Windows XP with gcc 4.4.0:

    Code:
    pe6b.c: In function 'main':
    pe6b.c:26: warning: passing argument 1 of 'show_arr' from incompatible pointer t
    ype
    pe6b.c:8: note: expected 'const double (*)[5]' but argument is of type 'double (
    *)[5]'
    pe6b.c:28: warning: passing argument 1 of 'show_arr' from incompatible pointer t
    ype
    pe6b.c:8: note: expected 'const double (*)[5]' but argument is of type 'double (
    *)[5]'
    In the book it says that if array contents are to be protected the formal parameter to a function that will do some work on the array should be declared as type const. The array itself doesn't have to be declared as type const.

    Look forward to hearing from anyone who's willing to help.

    Thanks in advance,

    Alexander Kapshuk.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have not tried what you're doing here, before. It seems to me that if you want to protect the source array, it should be declared constant, instead of just being changed to constant upon arrival in the copy_arr function.

    Could you make a constant pointer to array of double, and assign it the base address of the row you're about to have copied, and send that pointer, instead?

    Sasha, I have one thing to ask of you. Please shorten any line length inside your code. Otherwise, it won't line wrap, and the width is much too great for the forum tables.

    Thanks, and let's see what others suggest.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    Quote Originally Posted by Adak View Post
    I have not tried what you're doing here, before. It seems to me that if you want to protect the source array, it should be declared constant, instead of just being changed to constant upon arrival in the copy_arr function.

    Could you make a constant pointer to array of double, and assign it the base address of the row you're about to have copied, and send that pointer, instead?

    Sasha, I have one thing to ask of you. Please shorten any line length inside your code. Otherwise, it won't line wrap, and the width is much too great for the forum tables.

    Thanks, and let's see what others suggest.
    Thanks for getting back to me, Adak. Please see the code below. Is this what you had in mind? Thanks.

    Code:
    /* pe6b_new.c -- Write a program that initializes 
     * a two-dimensional array-of-double and uses one 
     * of the copy functions from exercise 2 to copy 
     * it to a second two-dimensional array. 
     * (Because a two-dimensional array is an array 
     * of arrays, a one-dimensional copy function 
     * can be used with each subarray.) */
    #include <stdio.h>
    
    #define ROW 5
    #define COL 5
    
    /* compiler warnings did not refer to copy_arr(), 
     * but to show_arr().*/
    void copy_arr(const double s[], double t[], int n); 
    /* declared 1st formal parameter as non-const */
    void show_arr(double ptr[][COL], int row);
    
    int main(void)
    {
    	int i;
    	/* declared a const pointer to an array of 5 doubles */
    	const double (*ptr)[COL];
    	double source[ROW][COL] = {
    		{1.1,2.2,3.3,4.4,5.5},
    		{6.6,7.7,8.8,9.9,10.1},
    		{11.1, 12.1,13.1,14.1,15.1},
    		{16.1,17.1,18.1,19.1,20.1},
    		{21.1,22.1,23.1,24.1,25.1}
    	};
    	double target[ROW][COL];
    
    	for (i = 0; i < ROW; i++)
    		copy_arr(source[i], target[i], ROW);
    
    	printf("SOURCE\n");
    	/* assigned address of the 1st source array of 5 doubles to ptr */
    	ptr = source;
    	/* passed it to show_arr() */
    	show_arr(ptr, ROW);
    	printf("\nTARGET\n");
    	/* assigned address of the 1st source array of 5 doubles to ptr */
    	ptr = target;
    	/* passed it to show_arr() */
    	show_arr(ptr, ROW);
    
    	return 0;
    }
    
    void copy_arr(const double s[], double t[], int n)
    {
    	int c;
    	for (c = 0; c < COL; c++)
    		t[c] = s[c];
    }
    
    void show_arr(double ptr[][COL], int row)
    {
    	int r, c;
    	for (r = 0; r < row; r++)
    		for (c = 0; c < COL; c++)
    			printf("%g ", ptr[r][c]);
    	putchar('\n');
    }

    Code:
    pe6b_new.c: In function 'main':
    pe6b_new.c:38: warning: assignment from incompatible pointer type
    pe6b_new.c:40: warning: passing argument 1 of 'show_arr' from incompatible point
    er type
    pe6b_new.c:17: note: expected 'double (*)[5]' but argument is of type 'const dou
    ble (*)[5]'
    pe6b_new.c:43: warning: assignment from incompatible pointer type
    pe6b_new.c:45: warning: passing argument 1 of 'show_arr' from incompatible point
    er type
    pe6b_new.c:17: note: expected 'double (*)[5]' but argument is of type 'const dou
    ble (*)[5]'

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    First, ignore the notes. What you do is perfectly valid. It just warns you in a way, in case you wanted to pass only constant arrays.
    const can be used in two ways:
    1) To declare that the variable won't be changed, possibly enabling some optimizations
    2) For the function to only take constant variables, just because that is what you want

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    Quote Originally Posted by C_ntua View Post
    First, ignore the notes. What you do is perfectly valid. It just warns you in a way, in case you wanted to pass only constant arrays.
    const can be used in two ways:
    1) To declare that the variable won't be changed, possibly enabling some optimizations
    2) For the function to only take constant variables, just because that is what you want
    Thanks for getting back to me. Your input is much appreciated!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In Turbo C, the original program compiles with only one warning, about a variable that isn't used in one function, and the program runs correctly.

    Your original program was good to go, imo.
    Last edited by Adak; 01-22-2010 at 07:56 AM.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    Quote Originally Posted by Adak View Post
    In Turbo C, the original program compiles with only one warning, about a variable that isn't used in one function, and the program runs correctly.

    Your original program was good to go, imo.
    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Getting the contents of edit boxes into an array
    By earth_angel in forum Windows Programming
    Replies: 3
    Last Post: 07-05-2005, 01:17 PM
  3. Using the debugger to see array contents
    By JohnnyCat in forum C++ Programming
    Replies: 2
    Last Post: 06-23-2005, 03:17 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 01:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21