LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2018, 03:22 AM   #1
turay
LQ Newbie
 
Registered: Sep 2017
Posts: 22

Rep: Reputation: Disabled
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 );

 
Old 05-18-2018, 04:27 AM   #2
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian buster/sid
Posts: 639
Blog Entries: 20

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
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.

Last edited by Michael Uplawski; 05-18-2018 at 04:31 AM.
 
1 members found this post helpful.
Old 05-18-2018, 07:13 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,963

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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
 
1 members found this post helpful.
Old 05-18-2018, 07:29 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,678

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
Just ask, what was the output of that program? How do you know it was wrong?
 
1 members found this post helpful.
Old 05-18-2018, 09:03 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,620
Blog Entries: 12

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
[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

Last edited by rtmistler; 05-18-2018 at 10:46 AM.
 
Old 05-18-2018, 09:52 AM   #6
turay
LQ Newbie
 
Registered: Sep 2017
Posts: 22

Original Poster
Rep: Reputation: Disabled
...

Last edited by turay; 05-18-2018 at 11:24 AM.
 
Old 05-18-2018, 10:13 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,620
Blog Entries: 12

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
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.
 
Old 05-18-2018, 10:35 AM   #8
turay
LQ Newbie
 
Registered: Sep 2017
Posts: 22

Original Poster
Rep: Reputation: Disabled
...

Last edited by turay; 05-18-2018 at 11:24 AM.
 
Old 05-18-2018, 10:48 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,620
Blog Entries: 12

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
Quote:
Originally Posted by turay View Post
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.
 
Old 05-18-2018, 11:28 AM   #10
turay
LQ Newbie
 
Registered: Sep 2017
Posts: 22

Original Poster
Rep: Reputation: Disabled
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 );
}

Last edited by turay; 05-18-2018 at 11:30 AM.
 
Old 05-18-2018, 12:55 PM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,678

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
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.
 
Old 05-18-2018, 03:07 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,620
Blog Entries: 12

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
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.
 
Old 05-18-2018, 10:19 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,338

Rep: Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501Reputation: 1501
Quote:
Originally Posted by pan64 View Post
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.
 
Old 05-19-2018, 09:59 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,678

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Linux Lite 3.8 and getting error Hash Sum mismatch Gaurishankar Linux - Newbie 2 03-09-2018 03:32 AM
[SOLVED] hash sum error on debian 3.12.9-1 x86_64 kgb200805 Linux - Newbie 1 06-19-2014 04:06 AM
use of swap with pointers gave error. ofbodur Programming 4 05-16-2013 10:11 AM
TCP check sum error mshenbagaraj Linux - Kernel 0 05-11-2007 02:36 AM
RPM cpio: MD5 sum mismatch error louisb Linux - Enterprise 1 07-10-2005 01:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration