LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error sum pointers in C ? (https://www.linuxquestions.org/questions/programming-9/error-sum-pointers-in-c-4175629941/)

turay 05-18-2018 03:22 AM

Error sum pointers in C ?
 
Error sum pointers in C ?
PHP Code:

#include <stdio.h>

int main() 
{
    
int sum 0;
    
int a[] = { 123};
    
int *aPtr;
    
aPtr a;
    
    for( 
int i 40i-- )
    {
        
printf"*aPtr = %d \n", *aPtr );
        
sum sum + ( *aPtr );
        *
aPtr--;
    }
    
    
printf"%d"sum );



Michael Uplawski 05-18-2018 04:27 AM

Your indices are wrong.

And PSE think about your thread-title. If we had known that you want to add array-values, not pointers, our brains could have worked more efficiently on your problem.

If you copied the homework-assignment to the post, above, verbatim.., for example.

The rants would come in more violently, though. My own inclusive.

jlinkels 05-18-2018 07:13 AM

Please use [ CODE] [ /CODE] tags, not [PHP].

Even if it homework, it is a fair question if you ask the proper question. That is, include why you think your code is correct, explain what is going wrong, where and when, and ask an explicit question like: "what is happening when I see this error"

I must add (again) to this that we expect you to demonstrate the understanding of this code. If you just copied the code from someone else, and now paste it here because it does not work, we won't do the work for you either.

jlinkels

pan64 05-18-2018 07:29 AM

Just ask, what was the output of that program? How do you know it was wrong?

rtmistler 05-18-2018 09:03 AM

[content edited, please see post #8]
turay,

As others have pointed out, your question does not contain enough information to explain what your problem description is. Likely you are already familiar with these guidelines, however if not, please see the LQ Site FAQ for some clear guidance about how to ask a clear question.

I would suggest you use the debugger to look at how the variables in your code change so that you can determine where exactly the code flaw is located.

As an example, you will be able to single step through this code, examine the variables, the pointers, and the locations which the pointers are referencing. From those available pieces of information, I'm confident that you can determine where the problem is.

Please update the forum as to whether or not you have any knowledge as to how to use the debugger.

Meanwhile if you wish a reference about how to use GDB, you can check my blog about using GDB, located here

turay 05-18-2018 09:52 AM

...

rtmistler 05-18-2018 10:13 AM

The verbiage of my earlier pun actually explains the flaw in that code.

My statement about the debugger is a way to help you to trace the code and thus learn firsthand how to determine the problem with that code.

If you're not inclined to do any actual work, that is fine.

turay 05-18-2018 10:35 AM

...

rtmistler 05-18-2018 10:48 AM

Quote:

Originally Posted by turay (Post 5856377)
I accept anything that is in line with the rules of the forum, but the cynicism and underestimation do not accept it

Hi Turay,

Well apologies if you felt my post was insensitive. I therefore have edited, it, but meanwhile your quote of it remains so there's is no removal of the original prose in case this is something of importance you wish to retain.

Please recheck my post (#5) for additional information about how to solve your problem.

Please update the forum with your progress and indicate if you require any additional assistance to help you solve this programming problem.

Best Regards.

turay 05-18-2018 11:28 AM

Thank you all,
I like this Forum,
I edited other replies.
I found the answer, I should see where pointer is in array then i use ++ or --.
Code:

#include <stdio.h>

int main()
{
    int sum = 0;
    int a[] = { 1, 2, 3, 4 };
    int *aPtr;
    aPtr = a;
   
    for( int i = 4; i > 0; i-- )
    {
        printf( "*aPtr = %d \n", *aPtr );
        sum = sum + ( *aPtr );
        *aPtr++;
    }
   
    printf( "%d", sum );
}


pan64 05-18-2018 12:55 PM

but I'm not really sure if that is the correct fix.
May I ask you to do the following:
Code:

...
int a[] = { -1, -15, 7, 3, 19, -19 }
....

and try that.

Especially there is an interesting line in your solution:
Code:

*aPtr++;
where * is irrelevant, you can safely omit it (if I knew it well).

And again, can you please post not only the code, but the output too. Thanks.

rtmistler 05-18-2018 03:07 PM

Hi pan64,

Had to look it up. But what they call Postfix operators like ++ or -- have higher precedence over Unary operators for de-referencing, http://en.cppreference.com/w/c/langu...tor_precedence

Therefore that term is interpreted where it will decrement the pointer, prior to de-referencing it.

Meanwhile, you are correct, the * does nothing because there is no assignment on the right of that. Therefore the * can go. Nice catch.

ntubski 05-18-2018 10:19 PM

Quote:

Originally Posted by pan64 (Post 5856438)
but I'm not really sure if that is the correct fix.

I think it's correct in that it sums all the right numbers. It's a bit confusing because "i" counts down, while the pointer goes in the opposite direction. But the number of increments/decrements is correct.

pan64 05-19-2018 09:59 AM

I would rather see something like:
Code:

...
for ( i=0; i<sizeof_array; i++)
...

You are right, I checked operator precedence too, that is actually correct.


All times are GMT -5. The time now is 04:33 PM.