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
Sarah
  • 125
  • 5

1 Answers1

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

  • Hey, I've got a question. Can you please tell me why index of any array starts with 0? – Shubham Jul 01 '20 at 10:42
  • @Lucas https://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c – RobertS supports Monica Cellio Jul 01 '20 at 10:44
  • @RobertSsupportsMonicaCellio: Thank you for your answer. Of course the array start with index 0. Sorry for that. While you iterate through the array, the termiantion condition depends only on x. Since I consider two arrays of the same type, I can state, that the end of pointer p is the end of pointer q? – Sarah Jul 01 '20 at 10:50
  • 1
    @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. – RobertS supports Monica Cellio Jul 01 '20 at 10:56
  • @RobertSsupportsMonicaCellio I read the [https://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c](https://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c) and I came to know that this happens for the simplicity of addressing memory location, which makes sense with what I already know about _Digital Electronics_. But there are other languages like `AWK, Fortran, Lua, R, etc.`, which don't follow this convention. May I ask why? – Shubham Jul 01 '20 at 11:26
  • 1
    Thank you very much. I got it:) – Sarah Jul 01 '20 at 11:29
  • @Lucas I guess you have to ask the inventors of a specific language of the reason of this made choice particularly. A few links which might be helpful for you, too regarding this concern: https://www.quora.com/Why-do-array-indexes-start-with-0-zero-in-many-programming-languages and https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm – RobertS supports Monica Cellio Jul 01 '20 at 11:31
  • So, It's just a biased decision of the inventors? – Shubham Jul 01 '20 at 11:32
  • @Lucas "Biased" is maybe not the correct word, since the index starting at 0 is really helpful and beneficial, but in fact, yes. Everything is more or less up to the inventor(s) and at some point you have to make a decision, even if it is one you later regret. It is the same like develop a car f.e. The car's purpose is to bring you from A to B. If the car has 3 or 4 wheels is irrelevant for this, although 4 wheels give you better posture in curves. – RobertS supports Monica Cellio Jul 01 '20 at 11:41
  • 1
    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?](http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html). – Shubham Jul 01 '20 at 11:43
  • 1
    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. :-) – Shubham Jul 01 '20 at 11:46