#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    28
    Rep Power
    0

    Unhappy Little execution problem in C


    I use ubuntu gcc platform for my c programing. I was trying to execute a simple program as follows
    Code:
    #include <stdio.h>
    
    void printArray( const int a[][3]);
    
    int main(){
    	
    	int array1[2][3] = {{1,2,3},{4,5,6}};
    	int array2[2][3] = {1,2,3,4,5,6};
    	int array3[2][3] = {{1,2}, {4}};
    	
    	printf("Values in array1 by row are:\n");
    	printArray(array1);
    	
    	printf("Values in array2 by row are:\n");
    	printArray(array2);
    	
    	printf("Values in array3 by row are:\n");
    	printArray(array3);
    	
    	return 0;
    }
    
    
    void printArray( const int a[][3]) {
    
    	int i,j;
    	
    	for (i=0; i<=1; i++){
    		for (j=0;j<=2; j++) {
    			printf("%d", a[i][j]);
    			}
    		printf("\n");
    		}
    		
    	printf("\n");
    }
    I got an error as

    Code:
    Ubuntu:~/Desktop/Main Panel/CCPP/Chapter 6$ gcc 6.21.c -lm -o 6.21
    6.21.c: In function ‘main’:
    6.21.c:12:2: warning: passing argument 1 of ‘printArray’ from incompatible pointer type [enabled by default]
    6.21.c:3:6: note: expected ‘const int (*)[3]’ but argument is of type ‘int (*)[3]’
    6.21.c:15:2: warning: passing argument 1 of ‘printArray’ from incompatible pointer type [enabled by default]
    6.21.c:3:6: note: expected ‘const int (*)[3]’ but argument is of type ‘int (*)[3]’
    6.21.c:18:2: warning: passing argument 1 of ‘printArray’ from incompatible pointer type [enabled by default]
    6.21.c:3:6: note: expected ‘const int (*)[3]’ but argument is of type ‘int (*)[3]’
    but i dont get this error when I dont use const option in printArray function. who to resolve this problem?

    Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    At first note these are not errors but warnings - they are about things which will supposedly work, but may get you into trouble. They did not prevent compilation of the program so you can run it anyway.

    If you currently have no idea on using the "const" keyword, you may safely ignore this just for now. I suppose later you'll come to this. Shortly speaking it is trying to tell compiler that you are willing to prevent some kind of modifications to array passed as parameter, but due to nested structure of array this could not be insured with given arrays. So compiler express some worriness.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by rodiongork
    At first note these are not errors but warnings - they are about things which will supposedly work, but may get you into trouble. They did not prevent compilation of the program so you can run it anyway.

    If you currently have no idea on using the "const" keyword, you may safely ignore this just for now. I suppose later you'll come to this. Shortly speaking it is trying to tell compiler that you are willing to prevent some kind of modifications to array passed as parameter, but due to nested structure of array this could not be insured with given arrays. So compiler express some worriness.
    Thanks.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Qualifiers (like const) on pointers have several meanings, depending on where you place the const keyword relative to the pointer.

    const int a;
    const int *p = &a;

    Here p is a pointer to a const integer, so saying *p = something; is illegal.
    However, we can still do p = &something; to make p point at another const integer.

    Similarly, we can say
    int a;
    int * const p = &a;

    Now the pointer itself is const (we can't do p = &something;), but what it points to is no longer const (so *p = something; is valid).

    To really nail things down, we do this
    const int a;
    const int * const p = &a;


    Now when it comes to 2D arrays, you have to express the parameter in pointer notation rather than array notation, in order to be able to place the const keyword in the right context.
    So
    void printArray( int a[][3]);
    becomes
    void printArray( int (*a)[3]);

    Having done that, you can place a const keyword in the right context.
    void printArray( int (* const a)[3] );

    However, this still doesn't prevent you from doing a[0][0] = 0; inside the function, which is what I think you wanted to achieve with all of this.
    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

IMN logo majestic logo threadwatch logo seochat tools logo