//flex table opened by JP

Click to See Complete Forum and Search --> : C question: pointer to an array


atari1356
11-11-2003, 06:23 PM
I'm learning C and am working through the book "Teach Yourself C Programming In 21 Days". I bought the book a long time ago, but it looks like it's online (http://lib.daemon.am/Books/C/) too (illegally?)... I'm having problems with exercise 9 and 10 of chapter 9 (http://lib.daemon.am/Books/C/ch09/ch09.htm#Heading20) :

9. Write a function named addarrays() that accepts two arrays that are the same size. The function should add each element in the arrays together and place the values in a third array.

10. ON YOUR OWN: Modify the function in exercise 9 to return a pointer to the array containing the totals. Place this function in a program that also displays the values in all three arrays.

Here's my program... I'm doing something wrong on line 14 (and maybe other places too), but don't know how to fix it... and for some reason they left out the answer from the back of the book. :confused:



#include <stdio.h>
#define ARRAYSIZE 5

int x[ARRAYSIZE] = { 1, 2, 3, 4, 5 };
int y[ARRAYSIZE] = { 10, 20, 30, 40, 50 };
int count;

int addarrays(int x[], int y[], int size);

main()
{
int *mypointer;

/* this is the line my compiler doesn't like */
mypointer = addarrays(x, y, ARRAYSIZE);

for(count = 0; count < ARRAYSIZE; count++)
{
printf("\n%d\t%d\t%d", x[count], y[count], *(mypointer + count));
}
}

int addarrays(int x[], int y[], int size)
{
int count, z[size];

int *ptr_to_z = z;

for( count = 0; count < size; count++)
{
z[count] = x[count] + y[count];
}

return *ptr_to_z;
}

atari1356
11-11-2003, 10:30 PM
Heh... I noticed that while the answer isn't in the copy of the book I have, they do have it online.

http://lib.daemon.am/Books/C/apg/apg.htm#Heading25

Their answer doesn't follow what they tell you to do though, since the function never "returns a pointer to the array containing the totals".

Oh well, starting to not like this book... I'll skip to the next chapter and move on with my life.

:rolleyes:

qball
11-12-2003, 12:31 AM
Write a function named addarrays() that accepts two arrays that are the same size.


you wrote:

int addarrays(int x[], int y[], int size)


The function should add each element in the arrays together and place the values in a third array.


you wrote:

return *ptr_to_z;

who cares???


Write a function named addarrays() that accepts two arrays that are the same size.


try, that first.


The function should add each element in the arrays together and place the values in a third array.


you seem to do that...

And then you hop to return pointer?, try returning array[], then pointer.

if your compiler no likey, it generally tells you why. posting that may, oops will be helpful.

atari1356
11-12-2003, 09:23 AM
Originally posted by qball
you wrote:

return *ptr_to_z;

who cares???


I'm just doing what the instructions said to do... "return a pointer to the array containing the totals".



try, that first.

I did write that function. It's at the bottom of my code.


you seem to do that...

And then you hop to return pointer?, try returning array[], then pointer.

if your compiler no likey, it generally tells you why. posting that may, oops will be helpful.


The error it gives is:
"exercise9_9.c:14: warning: assignment makes pointer from integer without a cast"

If by "try returning array[], then pointer" you mean "return z;" (since "z" is the name of my array)... I tried that too but that just adds more errors to the program.

fishybawb
11-12-2003, 10:10 AM
The first thing to check out is the function declaration of:

int addarrays(int x[], int y[], int size);

If you look at the code you wrote for that function, you're returning an integer pointer, not an integer. You need:

int *addarrays(..) in both the declaration and function code itself.

That sorts out that particular compiler hiccup, but there are a couple of other things too.

In the addarrays() function, you've got the line:

int count, z[size];

That's not valid. An array must be defined as a CONSTANT size, not using a variable. You need to find a way to know the size when you declare it - pretty easy when you think about it :)