We help IT Professionals succeed at work.

Program Problem

burttonboarder
on
244 Views
Last Modified: 2010-04-15
I am new to C and I am trying to figure out how it works. The program I am working on now creates two dynamically allocated arrays, one being the exact opposite of the other. Then the program is supposed to add the the two two dimensional arrays. This is what I thought should work, unfortunately it doesn't. Could you please helpe me. Thanks in advance.

int addmatrix(int *array1, int *array2, int cells){
      int i, *array;
      for(i=0; i<cells; i++){
            *(array + i) = *(array2 + i) + *(array1 + i);
      }
      return(array);
}
      
Comment
Watch Question

Commented:
Hi burttonboarder,

"int *array" is a pointer to an int data type, you are not creating an actual array here.
"return (array); You are not returning an array (that's impossible), but the pointer to the "array"

Even if you created the array as a 'real' array it would be a local variable to the function. This means it would be created on the stack and therefore it would go away when the function finishes (after the return).

Since you are learning just a few hints:

create the array as a global variable in your main() function (How did you define your two arrays array1 and array2?) and pass it into addmatrix().
If you want to use pointers instead of declaring the array like " int array[100]" or if you want to create it dynamic use the malloc() function.

Have fun with C
=====
Werner

Author

Commented:
I created array1 and array2 and sum, the pointer that gets the return value, using malloc().

Commented:
As I told you, you cannot create an array as a local variable in your function and return a pointer to the calling program. The array for the sum has to exist BEFORE you call addmatrix() because local variables are created on the stack which will become invalid after the return().

Data Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks, I actually came up with the exact same answer as your third suggestion just minutes ago, I really appreciate the input though. Thanks.

Commented:
I tried to avoid to explain to a beginner why it is possible to create 'persitant' variables in a function. Oh well, YOU tell him about the heap now :-)
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:

Hi griessh,

The heap -- that's that big pile over in the corner, isn't it?  :)

Seriously, I suspect that those questions won't be long in coming....


Kent
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.