Why "char" is inside "int" loop?
Can someone explain this to me:
Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;
int num_words = 0;
char *ptr,*ptr2;
char *result;
....
for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++)
{
*ptr2 = *ptr;
....
}
I'm confused with this last [for] loop;
How is ptr++ applied for non-integer value? Ptr is clearly a char, it comes from str, which reads string line from file.
I come from C# background, I have never met for...loop which irretates by using [somechar]++, not [someinteger]++;
What is actually going on there?
Some other similar example might be:
Code:
(iColor+(_parts[j]%length)*3),
where iColor is static unsigned char iColor[] array;
That has no sense to me. I would expect to see iColor[somevalue] + (_parts[j]%length)*3), but here in C++ I sometimes see that integer is being added directly to the array. What does it mean, what happens?
Thanks!
Re: Why "char" is inside "int" loop?
ptr is pointer, not a char. You may want to read up on pointers and pointer arithmetic. Here's the first link I found
http://www.learncpp.com/cpp-tutorial...er-arithmetic/
Here's the pertinent part.
"The C language allows you to perform integer addition or subtraction operations on pointers. If pnPtr points to an integer, pnPtr + 1 is the address of the next integer in memory after pnPtr. pnPtr - 1 is the address of the previous integer before pnPtr.
Note that pnPtr+1 does not return the address after pnPtr, but the next object of the type that pnPtr points to. If pnPtr points to an integer (assuming 4 bytes), pnPtr+3 means 3 integers after pnPtr, which is 12 addresses after pnPtr. If pnPtr points to a char, which is always 1 byte, pnPtr+3 means 3 chars after pnPtr, which is 3 addresses after pnPtr.
When calculating the result of a pointer arithmetic expression, the compiler always multiplies the integer operand by the size of the object being pointed to. This is called scaling."
All that code is doing is incrementing the pointer so it points to the next char in the array with each iteration of the loop.
Re: Why "char" is inside "int" loop?
Thank you. Probably looping using pointers has some speed gain, but really, that is my 3rd question regarding C++ and solution is related to pointers again. Next time I stuck, I'll just go back and read about the pointers. So can I assume then, that:
C++: *array+1 is same as
C#: array[0+1]
for chars, and for other value types I have to get their sizes first.
Re: Why "char" is inside "int" loop?
Quote:
Originally Posted by
RyuMaster
Thank you. Probably looping using pointers has some speed gain, but really, that is my 3rd question regarding C++ and solution is related to pointers again. Next time I stuck, I'll just go back and read about the pointers. So can I assume then, that:
C++: *array+1 is same as
C#: array[0+1]
for chars, and for other value types I have to get their sizes first.
Right. From what I posted above "When calculating the result of a pointer arithmetic expression, the compiler always multiplies the integer operand by the size of the object being pointed to. This is called scaling."
The compiler knows how much to increment the pointer. You don't need to worry about it.
Re: Why "char" is inside "int" loop?
Quote:
Originally Posted by
RyuMaster
Thank you. Probably looping using pointers has some speed gain,
With today's compilers, very little, if any speed gain is achieved by using pointers in this manner.
Quote:
So can I assume then, that:
C++: *array+1 is same as
C#: array[0+1]
No.
Code:
*(array + 1) is equivalent to array[1]
Note the parentheses. There is a big difference between this:
and this:
Regards,
Paul McKenzie
Re: Why "char" is inside "int" loop?
Quote:
C++: *array+1 is same as
C#: array[0+1]
No. *array+1 is not the same as array[0+1] because of c++ operator precedence. To be the same you would use *(array+1).
Because of c++ operator precedence, the * pointer dereference is done first then the addition is applied. So *array+1 is the same as array[0]+1.
Code:
int array[] = {10, 20, 30, 40, 50};
cout << *array << endl;
cout << *array+1 << endl;
cout << *(array+1) << endl;
This would show
10
11
20
I know this can be confusing. Hope this helps.
Re: Why "char" is inside "int" loop?
Thank you everyone, it is totally clear to me now. Also I'll postpone my porting job for a while and will go read more into pointers, seems like that pointers are bottleneck of difference between C++ and C# aside of memory management.
Re: Why "char" is inside "int" loop?
Quote:
Originally Posted by
RyuMaster
Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;
int num_words = 0;
char *ptr,*ptr2;
char *result;
....
for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++)
{
*ptr2 = *ptr;
....
}
*ptr2 = '\0';
On a side note, make sure you terminate the copy of the string.
Re: Why "char" is inside "int" loop?
The code also assumes that str will be NULL terminated as the for loop will continue copying chars until a '\0' is found - potentially overwriting memory.
Code:
......
static char str[BIG_STRING];
static char str_copy[BIG_STRING];
char **words;
int max_words = 10;
int num_words = 0;
char *ptr,*ptr2;
char *result;
....
for (ptr = str, ptr2 = str_copy; *ptr != '\0' && ptr < (str + (BIG_STRING - 1)); ptr++, ptr2++)
{
*ptr2 = *ptr;
....
}
*ptr2 = '\0';
It should also test that the end of the char array hasn't been reached before a '\0' is found.