0

I don't understand why my sum is 33 instead of 21. If I delete all the previous for loop, then my answer would be correct. If you can explain it to me, i really appreciate it. Here is my code:

int charr[] = {12, 7, -3, 4, 1};
int *p = charr, sum = 0;
// Print the array
for (*p = charr[0]; p <= &charr[4]; p++) {
    printf("Array elements in the normal order: %d\n", *p);
}
    
// Print the reverse array
for (*p = charr[4]; p >= &charr[0]; p--) {
    printf("Array elements in the reverse order: %d\n", *p);
}
// print the sum of array
for (*p = charr[0]; p <= &charr[4]; p++) {
    sum+= *p;
}
printf("Sum of the array is %d\n", sum);
2
  • Why do you assign values to *p in your loop? Commented Jul 28, 2020 at 10:59
  • I'm learning about Pointer today. So i want to try to use pointer as much as possible. The problem can be solved by using array only, but that is not what i want. Commented Jul 28, 2020 at 11:02

3 Answers 3

3
for (*p = charr[0]; p <= &charr[4]; p++) {
    printf("Array elements in the normal order: %d\n", *p);
}

should be:

for (p = &charr[0]; p <= &charr[4]; p++) {
    printf("Array elements in the normal order: %d\n", *p);
}

because you want to set the pointer to the adres of the first element of charr. This mistake you make in all three loops.

Initially you write:

int *p = charr, sum = 0;

You set the p to the correct address because p = charr is equal to p = &char[0]. However you modify p and never let it point back to charr[0] again.

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

2 Comments

Ah, thanks very much. One more thing, when i assign "*p = charr". The compiler reports that: incompatible pointer to integer conversion assigning to 'int' from 'int [5]. How can i fix this?
that is because dereferencing a int pointer int* evaluates to a plain int, which is different from int array[5].
0

Let's review what you did:

int charr[] = {12, 7, -3, 4, 1};
int *p = charr, sum = 0;
/* here you create and initiate your pointer to point on your array
   Certainly because it's mixing the syntax to create a pointer and
   to initiate it it confused you for the remaining part. I advise you split it as:
int *p;
p = charr;
*/
// Print the array
for (*p = charr[0]; p <= &charr[4]; p++) {
/* *p=charr[0] is replacing the int pointed by p by the value at charr[0]
   which happen to be the same in this case as p points at the begin of the array
   what you wanted to do was certainly p = &charr[0] to initiate where p points
*/
    printf("Array elements in the normal order: %d\n", *p);
}
    
// Print the reverse array
for (*p = charr[4]; p >= &charr[0]; p--) {
/* so here *p = charr[4] is replacing the value pointed by p by the value at 
   charr[4], now p points on charr[5], so outside of your actual array. So
   I think you print twice "1" because you copied it next to your array
   and start too loop from there.
   What you want to do is : p = &charr[4]
*/
    printf("Array elements in the reverse order: %d\n", *p);
}
// print the sum of array
for (*p = charr[0]; p <= &charr[4]; p++) {
/* here again the same kind of mistake. Because of the previous loop,
   p now points at charr[-1] which is an array in memory next to your array.
   you write there the value of charr[0], which is 12 and start summing from here.
   Hence the results being 12 bigger than expected.
   What you should do is p = &charr[0]
*/
    sum+= *p;
}
printf("Sum of the array is %d\n", sum);

Comments

0

You can also do it in this way:

// code from first for loop
// Print the array
for (; p <= &charr[4]; ++p) {
printf("Array elements in the normal order: %d\n", *p);
}

putchar('\n');
// Print the reverse array
for (--p; p >= &charr[0]; p--) {
    printf("Array elements in the reverse order: %d\n", *p);
}

putchar('\n');
// print the sum of array
for (++p; p <= &charr[4]; p++) {
    sum+= *p;
}
printf("Sum of the array is %d\n", sum);

You declared and initialized p as int* to &charr[0] in the statement, int* p = charr;. So you can omit the initialization part of the 1st for loop.

When 1st for loop exits, p points to the memory address one next to &charr[4] so in the initialization part of the 2nd for loop the expression --p makes p to point to &charr[4].

Finally, when 2nd loop exits, p points to 1 less than the &charr[0], so in the initialization part of the 3rd for loop, I did ++p to make p point to &charr[0].

In this way you can also achieve what the sum you expected, but it is an alternative not the best way. Because everytime you need to keep track of the memory address to which p points to.

Best way is:

// 1st loop
for(p = &charr[0]; p <= &charr[4]; p++)
 // printing statement

// 2nd loop
for(p = &charr[4]; p >= &charr[0]; p--)
 // reverse printing statement

//3rd loop
for(p = &charr[0]; p <= &charr[4]; p++)
 // sum statement

Comments

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.