Why Not Local Loops? Reasons Explained

  • Thread starter Thread starter jk22
  • Start date Start date
  • Tags Tags
    Local Loops
Click For Summary
SUMMARY

The discussion centers on the use of local loop variables in C and C++ programming, specifically the declaration of the loop index variable within the for loop, such as in for (int i=0; i < 100; i++) {...}. Participants argue that declaring the variable within the loop is beneficial for scope management and prevents variable reuse issues. They highlight that modern standards, including C99, support this practice, and emphasize that coding standards vary across organizations, with some advocating for more explicit variable naming to enhance code readability and maintainability.

PREREQUISITES
  • Understanding of C and C++ programming languages
  • Familiarity with variable scope and lifetime
  • Knowledge of coding standards and best practices
  • Awareness of C99 features and improvements
NEXT STEPS
  • Research the C99 standard and its impact on variable declarations
  • Explore best practices for naming variables in programming
  • Learn about the MISRA-C coding standards and their applications
  • Investigate the differences in variable handling between C and C++
USEFUL FOR

Software developers, particularly those working with C and C++, coding standards advocates, and anyone interested in improving code quality and maintainability.

  • #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   Reactions: 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   Reactions: 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   Reactions: jk22

Similar threads

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