GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-May-2007, 13:59
alkalinetri3o alkalinetri3o is offline
New Member
 
Join Date: May 2007
Location: Illinois
Posts: 1
alkalinetri3o is on a distinguished road

Building Errors


Hello Everyone! I'm new to GID and relatively new to C++ and was wondering if anyone could help me with a problem i'm having. In the following program im trying to pass arrays using two different methods. I just cant understand the errors im getting and how to correct them. Any help would be appreciated thanks.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

double function1(double a1, double a2);
double function2(double a1, double a3);

int main()
{	
	double a1[10] = {240.5, 300.1, 189.6, 310.6, 280.7, 216.9, 199.4,160.3,177.4,192.3};
	double a2[10], a3[10];
	function1 (a1[0], a2[0]);
	function2 (a1[0], a3[0]);
	for (int c = 0; c < 10; c++)
	{
		cout << a2[c] << "     " << a3[c] << endl;
	}
	return 0;
}

double function1 (double a1, double a2)
{
	for(int i = 0; i < 10; i++)
	{
line 24:		a2[i] = a1[i];
	}
	return a2;
}

double function2 (double a1, double a3)
{
	for(int k = 0; k < 10; k++)
	{
line 33:		double *ptr = &a1[0];
line 34:   	a3[k] = *ptr;
		ptr++;
	}
	return a3;
}

and the errors i am recieving are:

Code:
whoknows.cpp(24) : error C2109: subscript requires array or pointer type whoknows.cpp(24) : error C2109: subscript requires array or pointer type whoknows.cpp(33) : error C2109: subscript requires array or pointer type whoknows.cpp(34) : error C2109: subscript requires array or pointer type
Last edited by LuciWiz : 10-May-2007 at 06:04. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 09-May-2007, 16:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Building Errors


Quote:
Originally Posted by alkalinetri3o
whoknows.cpp(24) : error C2109: subscript requires array or pointer type
whoknows.cpp(24) : error C2109: subscript requires array or pointer type
whoknows.cpp(33) : error C2109: subscript requires array or pointer type
whoknows.cpp(34) : error C2109: subscript requires array or pointer type

Two important things arise:

1. You can not pass arrays as arguments to functions, and functions can not return arrays. (You can, however, pass pointers to the elements of the arrays back and forth.)

2. When you use the name of an array by itself (without the brackets []) in the argument of a function call or as the return value of a function it is treated as a pointer whose value is the address of the first element of the array.

Therefore, looking at your functions: you need to pass the addresses for the arrays and let the functions operate on the elements.

Since you don't actually use the return values of the arrays, why don't you declare them with a "void" return type? If you really want to return the address of the first element of an array, then you can make the function return type a pointer.

Like this:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

double *function1(double *a1, double *a2);
double *function2(double *a1, double *a3);

int main()
{
    double a1[10] = { 240.5, 300.1, 189.6, 310.6, 280.7, 
                      216.9, 199.4, 160.3, 177.4, 192.3 };
    double a2[10], a3[10];

    function1(a1, a2);
    function2(a1, a3);

    for (int i = 0; i < 10; c++) {
        cout << a2[i] << " " << a3[i] << endl;
    }
    return 0;
}

double *function1(double *a1, double *a2)
{
    for (int i = 0; i < 10; i++) {
      a2[i] = a1[i];
    }
    return a2;
}

// Note that I don't know what the heck this is supposed to do,
// but at least it compiles now, and you have the opportunity
// to test and debug it.
//
double *function2(double *a1, double *a3)
{
    for (int k = 0; k < 10; k++) {
      double *ptr = &a1[0];
      a3[k] = *ptr;
        ptr++;
    }
    return a3;
}

Note that the functions could have been declared and defined like this:

CPP / C++ / C Code:
double *function1(double a1[], double a2[]);

And the results would have been exactly the same for your functions. It may look to you as if this notation means that you are passing arrays, but, and I hate to repeat myself, you can not pass arrays as arguments to functions. You can pass pointers and that is what happens for both notations that I showed.

Note that the notation for calling the functions and the contents of the functions would not change if you decide you like the second notation that I showed for the function header. In other words, the program itself would be exactly the same (and the results would be the same).

Regards,

Dave
 
 

Recent GIDBlogFlickr uploads of IA pictures 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
Compile Errors jdbrine C++ Forum 3 30-Jun-2006 11:11
Link Errors jdbrine C++ Forum 10 18-Jun-2006 14:55
Compile Errors due to Default Parameters jdbrine C++ Forum 1 17-Jun-2006 14:45
In Need of Some Serious Help...compiling errors [C++] needcishelp C++ Forum 25 21-Dec-2005 01:26
Help with compiler errors Krandygrl00 C++ Forum 3 01-Jun-2005 07:45

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

All times are GMT -6. The time now is 23:40.


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