Why Not Local Loops? Reasons Explained

  • Thread starter Thread starter jk22
  • Start date Start date
  • Tags Tags
    Local Loops
Click For Summary
Defining loop variables like 'i' within a for loop is generally accepted in modern programming practices, particularly since C99 allowed such declarations. The primary concern with this approach is scope; declaring 'i' inside the loop limits its visibility, which can be beneficial if 'i' is not needed outside the loop. Some instructors advocate for using while loops for more complex conditions, but many programmers find for loops sufficient for straightforward iterations. Coding standards vary by organization, with some emphasizing the importance of readability and maintainability, especially in collaborative environments. Overall, using localized variables in loops is now considered good practice, contrary to older programming conventions.
  • #31
newjerseyrunner said:
strcpy is sort of like this, but with one very big difference. destination=source won't copy the character, it'l copy the pointer (*destination = *source) does a copy, notice that you must dereference the pointer and like I said destination++ has no pointer.
I absolutely agree. My only excuse is that I was late to an appointment and a bug snuck in (never write code in a hurry). Here are the correct statements:

char *destination, *source;

while (*destination++ = *source++);
...
while (*++destination= *source++);
...
while (*destination++ = *++source);
...
while (*++destination= *++source);
...
 
  • Like
Likes newjerseyrunner
Technology news on Phys.org
  • #32
I never understood this, does ++source jump 4 bytes since it is an address ?
 
  • #33
jk22 said:
I never understood this, does ++source jump 4 bytes since it is an address ?
"Pointer arithmetic is scaled by the type of object pointed to."
What this means is that you can't know what incrementing, decrementing, adding an integer to, subtracting an integer from, or subtracting two pointers will do, unless you know what type they are pointers to.

If source is declared as char * source;
then incrementing source evaluates to the address in memory of the next character.

If source is declared as int * source;
then incrementing source evaluates to the address in memory of the next int, which would typically be four bytes higher in memory.

If source is declared as double * source;
then incrementing source evaluates to the address in memory of the next double, which would typically be eight bytes higher in memory.
 
  • Like
Likes jk22
  • #34
jk22 said:
I never understood this, does ++source jump 4 bytes since it is an address ?
No. It depends on what the pointer points to. Thus:
C:
char *cptr;
cptr++; /* Increases the pointer by 1, since it points to (an array of) characters */

short *sptr;
sptr++; /* Increases the pointer by 2, since it points to (an array of) shorts (16 bit) */

long *lptr;
lptr++; /* Increases the pointer by 4, since it points to (an array of) longs (32 bit) */

struct complicated *comptr;
comptr++; /* Increases the pointer by [I]sizeof(struct complicated[/I]), since it points to (an array of) structs */

But beware:

void *ptr;
ptr++;  /* Illegal, causes a compiler error. A void pointer is just a pointer, it cannot be used for anything but as a parameter in a call */

long *lptr;
lptr = ptr; /* Legal, since the void pointer is compatible with all pointers */
lptr++;  /* Now we know how to increment the pointer */
 
Last edited:
  • Like
Likes jk22

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K