• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Array using pointers

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!! I have to make a function who find the three largest element of an array using pointers. I have written my code but my programm does not run and i can not find my false.Can you help me??Pointers is a unit that i hane found some difficulties so it is important for me your help!Thank you!
Here is my code:
 
Rancher
Posts: 494
15
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark - after a quick initial look I think the problem is

The max* variables are pointers, and they point to elements in the array which hold the largest values. They are references. So when you find new largest values, you need to change where they point to, not the contents of what they are pointing at (array element values), which is what you are doing. Basically in those lines you need to drop the *s ie.
That changes where the pointers are pointing, instead of the values of what they are pointing at.

Right - hope that helps get you started, back to my own work

Will check back later.
 
John Matthews
Rancher
Posts: 494
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I should have mentioned:
I think what you're trying to do is initially make all the pointers point to the first element in the array, hence why the for() loop starts from 1 rather than 0 - you've effectively already set the first element as the max value(s). The intention is good.

But again you're not setting where they're pointing - you're setting the contents of what they're pointing at. And in fact you haven't set them pointing at anything yet, so I wouldn't be surprised if your program crashes? That's usually what happens when you try to store something via an uninitialized pointer.
 
Mark Spen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help!I fixed my programm with the things that you said me!It works!!Really thank you!!
 
John Matthews
Rancher
Posts: 494
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 77939
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be careful about code formatting; pease indent it consistently and put spaces round your binary operatos. I edited your code a bit and think it is easier to read like this:-It might be better style to write three_elements() rather than ThreeElements(). What happens if you change lines 21‑23 to this?That is the old way to use array pointers, but, as JM might tell you, most people nowadays would use the indices as you wrote them at first.
 
John Matthews
Rancher
Posts: 494
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is the old way to use array pointers

Not sure about that - yes the original K&R book does point out that *(a+i) and a[i] are equivalent, but I can't spot, and don't remember reading, where it recommends the *(a+i) form.

Minor point. Campbell's code is much nicer, which makes it easier to read and hence spot bugs. I'm a big fan of neat code myself, as colleagues who have been at the receiving end of my code reviews will testify

[Campbell - the 'book' word above is supposed to be a URL to http://www2.cs.uregina.ca/~hilder/cs833/Other%20Reference%20Materials/The%20C%20Programming%20Language.pdf ; what have I done wrong?]
 
Campbell Ritchie
Marshal
Posts: 77939
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:. . . don't remember reading, where it recommends the *(a+i) form. . . .

I was expecting you simply to say that a[i] = ... is the preferred form, as I think Gustedt would. It probably doesn't recommend using the pointer directly.
I shall enquire about the link.
 
Campbell Ritchie
Marshal
Posts: 77939
373
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem with link sorted out.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic