Arrays and function prototypes
I know then when you use an array with a function the prototype must take this form
int somefunction(int somearray[]);
If I have a multi-dimensional array for example
int somearray[15][15];
does it need to be declared in the prototype with two sets of brackets?
int somefunction(int somearray[][]);
When an array is passed to a function as a parameter, it "decays" into a pointer. So you have two options
int somefunc(int *array[])
int somefunc(int **array)
int somefunc(int *array[])
int somefunc(int **array)
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Which goes some way to demonstrating why arrays are evil (i.e. neither of those options are valid syntax). Here''s a helpful link: How do I allocate multidimensional arrays using new?
If you really want to use multidimensional arrays, then you need to specify the size of the second dimension in the function declaration, or just use a pointer to the start of the array. Either way, you''re going to have to also pass some information about the array''s dimensions to ensure you don''t over-run.
If you really want to use multidimensional arrays, then you need to specify the size of the second dimension in the function declaration, or just use a pointer to the start of the array. Either way, you''re going to have to also pass some information about the array''s dimensions to ensure you don''t over-run.
arrays are not evil, not until you go past the end!
usually what I do is this:
somefunc(int *array, int size)
{
for(int i = 0; i < size; ++i)
array = 1000; //there should be brakets and an i after array, but doesn't show in the reply
}
this is the correct syntax, don't use any brackets in the funct prototype of declaration
I usually just skip the whole multi-dimensional array thing and make my arrays longer instead of more dimensions
[edited by - KingSalami on August 20, 2002 4:31:39 PM]
usually what I do is this:
somefunc(int *array, int size)
{
for(int i = 0; i < size; ++i)
array = 1000; //there should be brakets and an i after array, but doesn't show in the reply
}
this is the correct syntax, don't use any brackets in the funct prototype of declaration
I usually just skip the whole multi-dimensional array thing and make my arrays longer instead of more dimensions
[edited by - KingSalami on August 20, 2002 4:31:39 PM]
Trust the C++ FAQ!
While they''re not "evil" in the sense that you should never ever use them, there are times when you shouldn''t.
How the array is passed into a function depends on how you allocate it, so it''s a good idea to read up on them. It''s probably also a good idea to know how arrays and pointers work inside out before moving on to more advanced containers.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
While they''re not "evil" in the sense that you should never ever use them, there are times when you shouldn''t.
How the array is passed into a function depends on how you allocate it, so it''s a good idea to read up on them. It''s probably also a good idea to know how arrays and pointers work inside out before moving on to more advanced containers.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
quote: Original post by KingSalami
arrays are not evil, not until you go past the end!
What does the FAQ mean by "such-and-such" is evil?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement