I'm having touble trying to send arrays from funciton to function. The thing I can't do is make a global variable (one of the rules set by the teacher). I don't know what I'm doing really, so let me know if it's even doable.

>>so let me know if it's even doable

Absolutely and most definitely YES. We do it all the time. just pass the name of the array. Here is a simple example.

void foo(int array[])
{
 // blabla
}

int main()
{
   int array[10];

   foo(array);
   return 0;
}

I'm having touble trying to send arrays from funciton to function. The thing I can't do is make a global variable (one of the rules set by the teacher). I don't know what I'm doing really, so let me know if it's even doable.

The answer is yes as specified above. Just if the array is big and you want to save stack then you can pass the address of the first element of array.

foo(int * array)
{
   /* some action with array[] */
}

int main()
{
   int array[MAX];  /* MAX must be defined */
   foo(array); /* or foo(&array[0]) */
   return 0;
}

arrays are always passed by reference, never by value. Both andor's example and mine pass the array by reference. The only difference between the two examples is in the function foo() -- In Andor's example the parameter is declared as a pointer and in mine it is not.

Thanks guys, I'm almost done with my program, but just for the heck of it, could someone tell me how tho clear the creen every here and there?

depends on the operating system. The simplest way is to call system() function with the os-dependent command.

MS-Windows:

system("cls");

*nix

system("clear");

Others: I have no idea

depends on the operating system. The simplest way is to call system() function with the os-dependent command.

MS-Windows:

system("cls");

*nix

system("clear");

Others: I have no idea

Can I have both in at the same time?
This is going to be used on a unix and MS system.

you can use precompile directives

#if defined(_WIN32)
system("cls");
#else
system("clear");
#endif

Can I have both in at the same time?
This is going to be used on a unix and MS system.

Actually you don't want to clear the screen. It's bad form and it's not portable, as you can see. Best to just leave it.

>>It's bad form
matter of opinion, not fact. I hate it when programs leave all that irrelevant crap on the screen that hides what I need to read. If there is no reason for it to be there I would rather see it erased.

Actually it depends...

Mr. WaltP's point of view is based on portable coding practices and using the standard functions so that the code can run irrespective of the machine.
Mr. Dragon's point of view is based on a better implementation for a single platform.

So you can chose your pick, keeping in mind the issues associated with each of them.

Back to the starting topic, my c++ book says to pass an array to a function with its name and also its length. This is because an array name is only a memory location of the first variable in the function and passing the array's length will stop error of reading off the end of the array

that is the desired way of doing it, but there is nothing to enforce that rule.

>>passing the array's length will stop error of reading off the end of the array

only if the function uses that value for error checking. If the function has no error checking then passing the array size does nothing at all.

Where would you need an array that you didnt know where the end was?

normally do not pass the size of character arrays, which is the cause of many buffer overrun problems.

Its not necessary to always pass a parameter that inticates the size of the array. Example:

int foo( int array[100] )
{

}

re array can be send to function using pointer

like
main()
{
int array[10];
void funct(a);
}

void funct(int *k)
{

now using k as array u can perform the operation

}

I'm having touble trying to send arrays from funciton to function. The thing I can't do is make a global variable (one of the rules set by the teacher). I don't know what I'm doing really, so let me know if it's even doable.

normally do not pass the size of character arrays, which is the cause of many buffer overrun problems.

I'm not sure what you are saying here. My interpretation of what you said is you may have a buffer overrun because you passed the array size into the function. Seems to me that passing the size helps you prevent overruns. Bu not passing the size it's really easy to access elements outside the bounds of the array since you don't know where to stop.

Or are you saying "the cause of many buffer overrun problems is because programmers normally do not pass the size of character arrays."

>>Or are you saying "the cause of many buffer overrun problems is because programmers normally do not pass the size of character arrays."
Yes, that is what I intended to say.

Several standard C string functions have that problem -- strcpy(), sprintf() and gets() are just a few of them.

arrays are always passed by reference, never by value. Both andor's example and mine pass the array by reference. The only difference between the two examples is in the function foo() -- In Andor's example the parameter is declared as a pointer and in mine it is not.

Technically incorrect. C only supports 1 parameter passback method, pass by value. However, the value of an array variable name contains an address, which is why arrays can be mutated by passing in the name. This is how pass by referance can be emulated in C, just as Java only supports pass by value, but we can use objects to emulate pass by reference.

>>Technically incorrect. C only supports 1 parameter passback method, pass by value

I'd like you to show us that in the C standards. the term "pass by reference" ( and here) means to pass the address of the object and not a copy of the object. It is not possible to pass an array by value in C or C++ language.

To be fair, I see where you get the idea -- from here, which I think is incorrect.

Technically speaking, a array variable's name in C, as defined in numerous C books, such as "A Book on C" (Fourth edition) is a value referring to the address of the beginning element in the array. I'm not going to get into an argument about it, because it is up for interpretation. What I do know however, is that languages which specifically allow more than 1 parameter passback method (C# for example) use explicit words in passing parameters to declare them as other than pass by value (out and ref in C#).

Now is passing an address in C going to give you the same effect as pass by referance? Yes. IS it pass by reference? That depends on the original value of the variable passed IMO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.