Mickey Mathieson wrote:
> --- Rick <[EMAIL PROTECTED]> wrote:
>
>
>> I have a program I'm trying to write and can't
>> figure out how to do
>> it the way I want.
>>
>> To start with, I know I'll get a lot of "Don't use
>> C, use C++" and
>> "your program will crash if you enter more than
>> MAXSTRLN-1
>> characters" and "NEVER user fscanf()". This isn't
>> production code.
>> It's a quick-and-dirty test harness.
>>
>> I WANT to do this in C, not C++, (for now).
>> I know that the char arrays are actually being
>> passed by reference,
>> not by value.
>>
>> So, can someone show me the proper way to reference
>> the char arrays
>> within a function? Do I actually need to
>> de-reference them?
>>
>> Here is what I've written which is, obviously,
>> wrong.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> extern void loadcArray(char [][], int);
>> extern void printcArray(char [][], int);
>>
>> #define MAXSTRLN 16
>>
>> int main(int argc, char *argv[])
>> {
>> int result;
>> char array2[5][MAXSTRLN] = {"string1",
>> "String2", "String3",
>> "string4", "string5"};
>> int a2size = (sizeof(array2) / MAXSTRLN);
>>
>> loadcArray(array2, a2size);
>> printcArray(array2, a2size);
>>
>> system("PAUSE");
>>
>> return 0;
>> }
>>
>>
>> void loadcArray(char cArray[][], int alen)
>> {
>> int ix;
>> char val[MAXSTRLN];
>>
>> for (ix = 0; ix < alen; ix++)
>> {
>> printf("Enter a string value for
>> array[%d]: ", ix);
>> fscanf(stdin, "%s", val);
>> strncpy(cArray[ix][0], val,
>> MAXSTRLN-1);
>> }
>>
>> printf("Finished loading %d values\n",
>> ix);
>>
>> return;
>> }
>>
>>
>> void printcArray(char cArray[][], int alen)
>> {
>> int ix;
>>
>> printf("\n");
>> for (ix = 0; ix < alen; ix++)
>> {
>> printf("Array[%d] = %s\n", ix,
>> cArray[ix][0]);
>> }
>>
>> return;
>> }
>>
>>
>
>
> In a two dimensional array the compiler needs to know
> now many columns there are in order to calculate the
> address of the elements at he beginning of each row.
>
>
> You can do something like this :
>
> #define ROW 10
> #define COL 5
>
> void somefunct(char data[ROW][COL]);
>
> or
>
> void somefunct(char data[][COL], int NumOfRows);
>
> but this
>
> void somefunct(char data[][]);
>
> will not work in C.
>
>
> You would think there would be a way to pass the
> arrays dimension but C has no such feature.
>
> Again when passing two dimensional array to function
> in C you must at least specify the number of columns.
>
>
>
>
>
>
>
>
>
There are 2 methods of array representation in memory:
a. Row major - In this 'row i filled first, then column is filled'
b. Column major - In this 'column is filled first, then row is filled'.
Now can u guess, which representation 'C' follows !
> Mickey M.
> Construction Partner Inc.
> http://www.constructionpartner.com
>
>
>
> ____________________________________________________________________________________
> Don't pick lemons.
> See all the new 2007 cars at Yahoo! Autos.
> http://autos.yahoo.com/new_cars.html
>
>