GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Apr-2005, 04:10
brett brett is offline
New Member
 
Join Date: Apr 2005
Posts: 7
brett is on a distinguished road

Noob question on c arrays and functions


Hi,

I am teaching myself C using Deitel & Deitel and have got stuck with a problem updating arrays in a function. What I want to do is to update a 2D array but I get errors in Dev-C++ and gcc both. To demonstrate the problem, I have a 1D array of ints that I update in a function. When I try to extend this to a 2D array I get the errors "arithmetic on pointer to an incomplete type" in Dev-C++ (running in c mode) and "invalid use of array with unspecified bounds" in gcc. I assume that the problem is the way I am trying to update the array and use / misuse of pointers but I don't know what I should be doing to correct the error. I enclose the code for the single dimensioned array (that compiles and runs o.k.) and the 2D array that produces the error. Any help or pointers would much appreciated.

CPP / C++ / C Code:
/* Deitel & Deitel 'C How to program' pgs. 221-2             */
/* passing arrays and individual array elements to functions */
#include <stdio.h>
#define SIZE 5

void modifyArray(int[], int);
void modifyElement(int); /* doesn't do what you might expect */

main() {
       int a[SIZE] = { 0, 1, 2, 3, 4 };
       int i;
       
       printf("Original values of array are : \n");
       for (i=0; i<SIZE; i++)
           printf("%3d", a[i]);
       
       modifyArray(a, SIZE);
       printf("\nModified values of array are : \n");
       for (i=0; i<SIZE; i++)
           printf("%3d", a[i]);
           
       printf("\nEffects of passing an array element : \n");
       
       modifyElement(a[3]);
       printf("The value of a[3] is %d\n", a[3]);
       return 0;
}

void modifyArray(int b[], int size) {
     int j;
     
     for (j=0; j < size; j++)
         b[j] *= 2;
}

void modifyElement(int e) {
     printf("The value in modifyElement is %d\n", e *= 2);
}
                      
*********** now the 2d array update ***************

/* passing arrays and individual array elements to functions */
/* this one doesn't work */
#include <stdio.h>
#define SIZE 5

void modifyArray(float[][], int);
void modifyElement(float);

main() {
       float a[SIZE][SIZE] = { 0.0, 1.9, 2.4, 3.4, 4.6,
                               1.1, 2.1, 3.1, 4.1, 5.1,
                               2.3, 3.2, 4.2, 4.0, 7.1,
                               4.2, 5.5, 4.5, 5.1, 2.1,
                               3.2, 5.1, 5.3, 5.3, 1.1 };
       int i, j;
       
       printf("Original contents of the array are: \n");
       for (i=0; i<SIZE; i++) {
           for (j=0; j<SIZE; j++) {
               printf("%6.2f", a[i][j]);
           }
       }        
       
       modifyArray(a, SIZE);
       printf("\nThe modified values are :\n");    
       for (i=0; i<SIZE; i++) {
           for (j=0; j<SIZE; j++) {
               printf("%6.2f", a[i][j]);
           }
       }
       
       printf("\nEffects of passing an individual element by value"
              "\nThe value of a[3] is %6.2f\n", a[3]);
       modifyElement(a[3][3]);
       printf("The new value of a[3][3] is %6.2f\n", a[3][3]);
       return 0;
}

void modifyArray(float b[][], int size) {
     int j, k;
     
     for (j=0; j<size; j++) {
         for (k =0; k<size; k++) {
             b[j][k] *= 2;
         }
     }    
}

void modifyElement(float e) {
     printf("The value in modifyElement is %d\n", e);
}

Thanks,
Brett
Last edited by LuciWiz : 20-Apr-2005 at 05:30. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 20-Apr-2005, 04:59
brett brett is offline
New Member
 
Join Date: Apr 2005
Posts: 7
brett is on a distinguished road
Hi again,

well I had checked around on the web but must have missed the bit in the C Faq, I looked again at section 6.18 in the faq and changed the function dec. to:

void modifyArray(float b[][SIZE], int size) {

and this now compiles and runs as expected. I guess that I need to read this again until I understand the way functions, pointers and arrays work. Any advice will be appreciated.

Rgds,
Brett
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse functions and Reverse checking functions jenmaz C Programming Language 4 24-Nov-2004 09:48
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26
Help on passing in arrays in functions? nusstu C Programming Language 10 02-Apr-2004 11:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.