C programming: working with pointers

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
41 replies · 8K views
Math Is Hard,

The assignment seemed to specifically ask for pointer incrementing, like this:

PHP:
void copy_ptr(double *ar1,double *ar2,int n)
{
   int i;
   for(i=0;i<n;i++)
      *(ar2++) = *(ar1++);
}
 
Physics news on Phys.org
chroot said:
Math Is Hard,

The assignment seemed to specifically ask for pointer incrementing, like this:

PHP:
void copy_ptr(double *ar1,double *ar2,int n)
{
   int i;
   for(i=0;i<n;i++)
      *(ar2++) = *(ar1++);
}

oops! sorry- I think I got carried away with the formatting fun stuff! :redface: so, I should use ++ for "pointer incrementing"?
using + i won't move to the correct position in the array?
 
Math Is Hard,

Remember the three qualities of a good programmer:

1) Laziness. No one should have to do much work, so a good programmer should strive to make the user's life utterly push-button easy.

2) Impatience. No one should ever have to wait for a computer to do anything, so programmers should be careful with their algorithm designs to make sure their users never have to wait for anything.

3) Hubris. Programmers should take excessive pride in their work, and should strive to make every program they write complete, totally functional, and elegant.

The PHP tags are really an example of some programmer on the vBulletin team demonstrating quality number one.

- Warren
 
Math Is Hard said:
oops! sorry- I think I got carried away with the formatting fun stuff! :redface: so, I should use ++ for "pointer incrementing"?
using + i won't move to the correct position in the array?
Your code using pointer arithmetic (the + signs) will certainly work, but it might not be the best way to meet the specification. The specification said to use pointer incrementing. Normally, I associate the ++ operator with the word "incrementing," but it may just be a semantic issue.

- Warren
 
chroot said:
Math Is Hard,

Remember the three qualities of a good programmer:

1) Laziness. No one should have to do much work, so a good programmer should strive to make the user's life utterly push-button easy.

2) Impatience. No one should ever have to wait for a computer to do anything, so programmers should be careful with their algorithm designs to make sure their users never have to wait for anything.

3) Hubris. Programmers should take excessive pride in their work, and should strive to make every program they write complete, totally functional, and elegant.
Wow - who knew?? I promise to try my best to acquire these ..uh.. virtues! :biggrin:
 
chroot said:
Normally, I associate the ++ operator with the word "incrementing," but it may just be a semantic issue.

That's good enough for me. I'll take your advice on that and use ++ in my final version.
 
chroot said:
Math Is Hard,

Remember the three qualities of a good programmer:

1) Laziness. No one should have to do much work, so a good programmer should strive to make the user's life utterly push-button easy.

2) Impatience. No one should ever have to wait for a computer to do anything, so programmers should be careful with their algorithm designs to make sure their users never have to wait for anything.

3) Hubris. Programmers should take excessive pride in their work, and should strive to make every program they write complete, totally functional, and elegant.

The PHP tags are really an example of some programmer on the vBulletin team demonstrating quality number one.

- Warren

You forgot 4)

4) Once you land a job as a programmer learn to obfuscate as much as possible so that you are the only programmer able to maintain certain chuncks of code thus ensuring job security.
 
faust9 said:
You forgot 4)

4) Once you land a job as a programmer learn to obfuscate as much as possible so that you are the only programmer able to maintain certain chuncks of code thus ensuring job security.

OMG! I had a job once where ALL the programmers lived religiously by that guideline. Not even the tiniest cryptic comment was ever written. My job? To go around to the programmers and produce documentation on what they were coding. Since they all refused to document or comment my boss thought I should be the one to do it. He wanted to farm out some of the work overseas so he wanted to create a guidebook for these contracted developers who would write new modules of the app.
It was horrible. The programmers were really protective and defensive about showing anyone their code or explaining to anyone what it did.
And what a mess. By the time I came to work their the application was three fourths done and there was no formal spec - just shoe boxes full of meeting notes and emails from the client.