-
March 8th, 2013, 09:44 AM
#1
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!
-
March 8th, 2013, 10:53 AM
#2
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.
-
March 8th, 2013, 11:05 AM
#3
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.
-
March 8th, 2013, 11:19 AM
#4
Re: Why "char" is inside "int" loop?
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.
-
March 8th, 2013, 11:25 AM
#5
Re: Why "char" is inside "int" loop?
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.
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
-
March 8th, 2013, 11:30 AM
#6
Re: Why "char" is inside "int" loop?
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 8th, 2013, 12:08 PM
#7
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.
-
March 8th, 2013, 09:43 PM
#8
Re: Why "char" is inside "int" loop?
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.
-
March 9th, 2013, 07:31 AM
#9
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|