(I'm a Computer Science 101 student writing programs in C on a virtual Linux machine.)

Suppose I wanted to apply a single (arbitrary) function to every element of an array, one at a time. Doing this once is easy enough, but I'd like to find a method that could accomplish the task in a variety of scenarios. In other words, I'm trying to make a function that would do this:

/*  With "array[y][x]" already defined and containing values */ 

int i, j;

for (j = 0; j < y; j++) {
    for (i = 0; i < x; j++) {

        array[j][i] = other_fxn(array[j][i], ...); } }

...rather than having to write an entirely new, array-specific version of every single function I intend to use this way. (I've typed the above code so many times that I'm SURE it can be encapsulated in a function.)

Ideally, the argument passed for other_fxn would only be limited to using array's elements one at a time and outputting the correct data type. (Example: find the square root of a[j][i] vs. divide a[j][i] by a consistent, previously specified n.) If this isn't possible, I would appreciate if someone could explain the limitations. I realize this is a rather vague question; I can provide more specific examples if neccessary.


Edited by Twilitbeing

4 Years
Discussion Span
Last Post by dmanw100

I'm not sure I quite understand your question, but I believe what you mean is "can a function accept a generic array parameter?" The answer to this question is yes, just remember to make sure the types you use are correct (for example: be careful not to mix char arrays with int arrays depending on what you're doing in the function).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.