Arrays, functions pointers

This is a discussion on Arrays, functions pointers within the C Programming forums, part of the General Programming Boards category; Still working through my primer book, it's asking me to make a program that takes input to fill a 3x5 ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    39

    Arrays, functions pointers

    Still working through my primer book, it's asking me to make a program that takes input to fill a 3x5 array with values and then perform various tasks on those values. Right now I'm stuck at filling it. Basically I can't figure out the proper way either to pass the pointer to the array through the function or the proper way to use scanf inside the function. It seems to take the values, and it prints the array fine, but the value never make it into the outside array, they seem to only get stored in the temporary arr. Thanks for your help

    I've tried passing it as double *arr, double arr[rows][cols], double arr[][5], and a few other things I'm just not sure where I'm going wrong in the setup. Without functions this would be easy, but the book is asking for function based (understandably).

    Code:
    #include <stdio.h>
    void copy_arr(int rows, int cols, double * arr[3][5]);
    void copy_prnt(int rows, int cols, double tgt[rows][cols]);
    int main(void)
    {
    	double orig[3][5];
    	
    	copy_arr(3, 5, orig);
    	copy_prnt(3, 5, orig);
    		
    	return 0;
    }						
    	
    void copy_arr(int rows, int cols, double * arr[3][5])
    	{
    	int i;
    	
    	for (i = 0; i < rows; i++)
    		{
    		printf("Enter five values:\n");
    		scanf("&#37;f %f %f %f %f", &arr[i][0], &arr[i][1], &arr[i][2], &arr[i][3], &arr[i][4]);
    		}
    	}
    	
    void copy_prnt(int rows, int cols, double tgt[rows][cols])
    	{
    	int i, j;
    	
    	for (i = 0; i < rows; i++)
    		{
    		for (j = 0; j < cols; j++)
    			printf("%.2f  ", tgt[i][j]);
    		printf("\n");
    		}	
    	}

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Declaring the functions like this
    Code:
    void copy_arr(int rows, int cols, double arr[][5]);
    void copy_prnt(int rows, int cols, double tgt[][5]);
    and using &#37;lf instead of %f for the scanf() (because you're reading into a double, not a float) works.

    You can't use a previous parameter to specify the size of an array in the parameter list, unfortunately. It has to be a constant.

    Note that double tgt[3][5] would work as well, or double (*tgt)[5].

    Here's the complete code:
    Code:
    #include <stdio.h>
    void copy_arr(int rows, int cols, double arr[][5]);
    void copy_prnt(int rows, int cols, double tgt[][5]);
    int main(void)
    {
    	double orig[3][5];
    	
    	copy_arr(3, 5, orig);
    	copy_prnt(3, 5, orig);
    		
    	return 0;
    }						
    	
    void copy_arr(int rows, int cols, double arr[][5])
    	{
    	int i;
    	
    	for (i = 0; i < rows; i++)
    		{
    		printf("Enter five values:\n");
    		scanf("%lf %lf %lf %lf %lf", &arr[i][0], &arr[i][1], &arr[i][2], &arr[i][3], &arr[i][4]);
    		}
    	}
    	
    void copy_prnt(int rows, int cols, double tgt[][5])
    	{
    	int i, j;
    	
    	for (i = 0; i < rows; i++)
    		{
    		for (j = 0; j < cols; j++)
    			printf("%.2f  ", tgt[i][j]);
    		printf("\n");
    		}	
    	}
    Input/output:
    Code:
    Enter five values:
    1 2 3 4 5
    Enter five values:
    6 7 8 9 10
    Enter five values:
    11 12 13 14 15
    1.00  2.00  3.00  4.00  5.00
    6.00  7.00  8.00  9.00  10.00
    11.00  12.00  13.00  14.00  15.00
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    ah thanks dwks, dunno why the scanf calls keep messing me up, guess I need to write down a reference sheet for myself until i get them down. At least I was getting the passing correct. Thanks again

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you turn on compiler warnings for GCC, it will warn you about that.
    Code:
    21 file.c [Warning] float format, double arg (arg 2)
    Add -W -Wall (and perhaps -ansi -pedantic afterwards as well) to the compiler parameters and that's the sort of warning you will get.

    For Dev-C++ 4.9.9.2, click Tools->Compiler Options, check the checkbox "Add the following commands when calling compiler", and type -W -Wall into the textbox provided.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    well I'm compiling and writing in Apple Xcode, and I need to start getting the basics memorized anyhow, but I'll keep that in mind when I compile in the Unix terminal, thanks again

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're right, just because your compiler tells you when you do something wrong doesn't mean that you shouldn't learn how to do it right. But those sorts of warnings are very helpful, and they can save you hours of debugging.

    You can enable warnings in Xcode as well, I just don't know how, since I've never tried. This article seems to explain how: http://developer.apple.com/tools/xco...canalysis.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    At the heart of Xcode 2.0 is Apple’s version of gcc 4.0, the next generation of the industry-standard gcc compiler.
    http://www.apple.com/macosx/features/xcode/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  2. Problem with arrays, pointers and functions when combined
    By The Wazaa in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2006, 10:44 AM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 10:41 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

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