0

I want to add two arrays. Therefore I wrote the following code:

float x[2], y[2];
x[1]=1;
x[2]=2;
y[1]=2;
y[2]=1;
float* end=x+2; // n is the size of the array x
float* p;
float* q; //Given to arrays x and y.


for(p=x,q=y; q,p<end;q++,p++){
        printf("%f",*p+*q );

}

Why does this not work. I only get the first value of new array. Result should be:

3
3
11
  • 3
    After defining an array with two elements (float x[2];) the two elements are x[0] and x[1]. The "thing" x[2] does not exist. Commented Jul 1, 2020 at 9:57
  • 1
    In your question, you already describe the expected output you want. But what is the actual output? That's a generally useful thing to mention. Commented Jul 1, 2020 at 10:00
  • 1
    @P__J__ Doesn't the given code look remarkably like the code from an exercise? Commented Jul 1, 2020 at 10:06
  • 1
    so he need to reread the "arrays" chapter Commented Jul 1, 2020 at 10:08
  • 1
    @P__J__ Dependent upon the author and the book itself, maybe the book is low quality or they made a mistake, so that it is not her fault in particular. A sign for that would also be the redundant placed q inside of the condition. We just don't know. Commented Jul 1, 2020 at 10:32

1 Answer 1

2
float x[2], y[2];

x and y are both arrays of 2 elements of type float.

When using x[2] = 2; and y[2] = 1;, you attempt to write the values into a third element (which does not exist) beyond the bounds of the arrays which invokes undefined behavior since subscript indexing starts at 0, not 1.

For the reason why you can take a look at here:

Use:

x[0] = 1;
x[1] = 2;
y[0] = 2;
y[1] = 1;

instead.


Example (Online):

#include <stdio.h>

int main (void)
{
    float x[2], y[2];

    x[0] = 1;
    x[1] = 2;
    y[0] = 2;
    y[1] = 1;

    float* end = x + 2; // n is the size of the array x
    float* p;
    float* q; //Given to arrays x and y.


    for (p = x, q = y ; p < end ; q++, p++) {
        printf("%.2f\n", *p + *q);
    } 
}

Output:

3.00
3.00

Side Notes:

  • "I want to add two arrays."

    Something like that is not possible. In fact, You do not add the arrays; you don't even add certain elements of it. You only add the values of specific elements as argument in the call to printf(). The difference is important.

  • q, in the for loop condition q,p < end has no effect. The expression has the value and type of the right hand operand of the comma operator.

  • Please learn how to format/indent your code properly. It will help you and other readers of your code in the future.

  • Good and free C starting books are Modern C or The C Programming Language (2nd Edition). These and others you can find here:

    The Definitive C Book Guide and List

Sign up to request clarification or add additional context in comments.

11 Comments

Hey, I've got a question. Can you please tell me why index of any array starts with 0?
@Sarah "While you iterate through the array, the termination condition depends only on x." - Yes, but it is no problem in this case since the arrays x and y have equivalent length and we compare p to the address on past the array x, stored in end. --- "Since I consider two arrays of the same type, I can state, that the end of pointer p is the end of pointer q?" - No. p and q point to different array objects in memory and with that to different addresses. After the for loop, they both point one past the relative array, but not to the same location.
Thank you very much. I got it:)
I read the Quora's post about this and quoting a line from Anders Kaseorg's answer: "Zero-based indexing actually simplifies array-related math for the programmer, and simpler math leads to fewer bugs." I get it. At the end every answer points to this why numbering should start at zero?.
I will not bother you much. Thanks for the replies and yes, for the CAR's example too. @RobertSsupportsMonicaCellio I gotta ask the inventors of C or R, if I can find'em. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.