Moderators: phlip, Moderators General, Prelates
Little Richie wrote:PM 2Ring wrote:It can also be written as
char s[11];
char *p;
p = s + 10;
If this is also the same, then I understand:
char s[11];
char *p;
p = s;
p = 10;
Would you just use an indexed array and write to s[x], instead of the pointer?
What are the advantages / disadvantages of pointers over indexes?
Yakk wrote: There are serious quirks (sequence points etc) you do not need to learn.
Yakk wrote: In general, debugging code is harder than writing it. If you write code that is as clever as you can, you won't be smart enough to debug it.
Little Richie wrote:If this is also the same, then I understand:
char s[11];
char *p;
p = s;
p = 10;
Little Richie wrote:Actually, my mentor encourages making the code more efficient. I learned about " pre/post increment" in my first assignment, a bit counter. Personally, I'd also rather see it on one line than written in two.
Little Richie wrote:Would you just use an indexed array and write to s[x], instead of the pointer?
Little Richie wrote:What are the advantages / disadvantages of pointers over indexes?
Little Richie wrote:PM 2Ring wrote:char s[11];
char *p;
p = s + 10;
If this is also the same, then I understand:
char s[11];
char *p;
p = s;
p = 10;
Actually, my mentor encourages making the code more efficient. I learned about " pre/post increment" in my first assignment, a bit counter. Personally, I'd also rather see it on one line than written in two.
*p = '\0';
p--;#include <stdio.h>
void print_pair(int a, int b) {
printf("a=%d, b=%d\n", a, b);
}
int main() {
int x = 1;
print_pair(x, x++);
return 0;
}print_pair(x++, x++)PM 2Ring wrote:Well, it can be done without a pointer [. . .]
Would you just use an indexed array and write to s[x], instead of the pointer?
What are the advantages / disadvantages of pointers over indexes?
EvanED wrote: undefined behavior
Little Richie wrote:This guy has been coding forever,
Little Richie wrote:This guy has been coding forever,
Carnildo wrote:Little Richie wrote:This guy has been coding forever,
If he's been programming forever, he may have learned C before the days of optimizing compilers. If you're using a compiler from the late 70s/early 80s, there's a decent chance that i++ is faster than i+=1. If your compiler was written in the mid-80s or later, it's almost certain to generate the same machine code for both cases.
*p = '\0';
++p;EvanED wrote:Carnildo wrote:Little Richie wrote:This guy has been coding forever,
If he's been programming forever, he may have learned C before the days of optimizing compilers. If you're using a compiler from the late 70s/early 80s, there's a decent chance that i++ is faster than i+=1. If your compiler was written in the mid-80s or later, it's almost certain to generate the same machine code for both cases.
One more point inspired by this. We're not saying avoid the ++ and -- operators, just to put them in their own statement. For instance, this is hunky-dory:
- Code: Select all
*p = '\0';
++p;
In fact, I'd rather see p++ than p += 1 personally, especially if p is a pointer.
PM 2Ring wrote:Beware: some of us old-timers have developed bad habits that we occasionally inflict on the newbies.
EvanED wrote:Right; not everyone shares our disdain for that sort of use of the inc/decrement operators. Just look at the proclivity of C programmers to giggle with glee at the ability to write a string copying function as while(*p++ = *q++);.
It seems like the fondness for mixing in those operators is a lot greater with old-school C programmers than folks who have grown up recently. So I'd say it's just as likely that the fact that he's been coding forever would suggest that he would have a differing opinion.
(And, of course, jareds is right. If you really do insist, following the rule that "if you say x++ then you can't use x anywhere else in that statement" is probably pretty close to C's rules for undefined behavior in that instance. And note that assignments are actually expressions; e.g. x = x++ is undefined behavior.)
PM 2Ring wrote:It's a Good Idea to include headers for any library functions that your program uses. You're using printf(), so you should include <stdio.h>, otherwise the compiler will have to guess what the argument types of printf() are. Luckily, automatic promotion makes everything work properly in this instance, but you shouldn't rely on that.
PM 2Ring wrote:This process may look a bit dodgy. What happens if we're converting a large number that totally fills s? At the end of the loop, p points to a memory address that's outside of s. Fortunately, that's ok - to permit algorithms like this one, the C standard says that pointer arithmetic is allowed to reference the location just before or just after an array. But it's definitely not wise to dereference a pointer when it points to a location that you don't legitimately own.
Sandor wrote:PM 2Ring wrote:This process may look a bit dodgy. What happens if we're converting a large number that totally fills s? At the end of the loop, p points to a memory address that's outside of s. Fortunately, that's ok - to permit algorithms like this one, the C standard says that pointer arithmetic is allowed to reference the location just before or just after an array. But it's definitely not wise to dereference a pointer when it points to a location that you don't legitimately own.
The C standard only says that pointer arithmetic is allowed to reference one past the end of an array. I don't think one before the start is defined.
Users browsing this forum: No registered users and 5 guests