#I need help with pointers and arrays

30 messages · Page 1 of 1 (latest)

proper ivy
#

Create a C function named square_reverse with three parameters. The two first
parameters are 64-bit floating-point pointers x and y, and the third parameter is an integer
parameter called len. Pointer x points to an array of length len of floating-point values.
The function reads out each element of the array, computes the square value of the element
(x2) and then writes back the result into the array y in reverse order. That is, the output
array y has also the length len. The function also returns the sum of all input values
(those in x)

So far this is what I've done

void square_reverse(double *x, double *y, int len){

    for (int i = 0; i<len; i++){
        
    }
}
dry shoalBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

proper ivy
#

*x points at the an array right? So how can I access the array?

static vigil
#

Use the array access syntax as normal.

proper ivy
static vigil
#

x[index].

proper ivy
#

I wanted to iterate over the array but can I just use "x" as the array and do like

static vigil
#

You can.

proper ivy
#

isn't x just a pointer

static vigil
#

Because the syntax is really working with pointers.

#

An array is implicitly converted into a pointer.

#

Then x[i] is just syntactic sugar for *(x + i).

proper ivy
# static vigil Then `x[i]` is just syntactic sugar for `*(x + i)`.

I kinda get that but what I am struggling to understand is, we have a pointer that is pointing at the array right? The only thing stored in the pointer is the adress so how come you can get the first adress by just doing x[i] when x is not even an array to begin with.

static vigil
#

When x is a pointer to an element of an array, x + i is the pointer to the element i positions after that.

proper ivy
static vigil
#

You will need to dereference the pointer to get the array first.

#

x there is pointer to an element, not the whole array.

proper ivy
#

I forgot doing the sum

static vigil
#

The syntax is correct but the value of j is not correct.

static vigil
#

Yes.

#

Or you could just initilize j using len - 1.

proper ivy
#

Here is the most recent ```int square_reverse(double *x, double *y, int len)
{
int j = len - 1;
double sum = 0;

for (int i = 0; i < len; i++)
{
    sum += x[i];
    y[j] = x[i] * x[i];
    j--;
}
return sum;

}```

#

It also says "Note that your function should also declare parameters as const when appro-
priate." What do they mean? A constant pointer is a pointer that can't change the adress right? So how do I know which one should be constant or if they both should be?

static vigil
#

If you never modify the values they can be const.

#

constness for a pointer can apply to two points: the element the pointer points to or the pointer variable itself.