C programming: working with pointers

Click For Summary
SUMMARY

This discussion focuses on the intricacies of working with pointers in C programming, specifically addressing the validity of various pointer expressions. Participants confirm that adding an integer to a pointer (e.g., pi + ii) is valid, while adding a pointer to another pointer (e.g., pi + pj) is invalid, as it does not yield useful information. The discussion also highlights that pointer subtraction is valid and useful for determining the distance between elements in an array. Additionally, the importance of understanding memory addresses and data types is emphasized for effective pointer manipulation.

PREREQUISITES
  • Understanding of C programming syntax and semantics
  • Familiarity with data types and memory allocation in C
  • Knowledge of pointer arithmetic and its implications
  • Experience with basic debugging and compiler error messages
NEXT STEPS
  • Practice pointer arithmetic with various data types in C
  • Explore the concept of memory addresses and how they relate to pointers
  • Learn about the differences between pointer addition and subtraction
  • Investigate compiler-specific behaviors regarding pointer operations
USEFUL FOR

C programmers, computer science students, and anyone looking to deepen their understanding of pointers and memory management in C.

  • #31
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
  • #32
php tags

I am SO loving this! Yeah - I just went back and indented and it was so EASY!
 
  • #33
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?
 
  • #34
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
 
  • #35
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
 
  • #36
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:
 
  • #37
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.
 
  • #38
Thanks, again!
 
  • #39
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.
 
  • #40
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.
 
  • #42
robphy said:
For more qualities, consult this enlightening article on good :-p programming techniques:
How To Write Unmaintainable Code

LOL! That's hilarious! :smile:

I love the quotes at the beginning of each section, especially thie one:

Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
Replies
12
Views
2K
Replies
7
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
86
Views
2K