CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Posts
    13

    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!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Join Date
    Jul 2008
    Posts
    13

    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.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why "char" is inside "int" loop?

    Quote Originally Posted by RyuMaster View Post
    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why "char" is inside "int" loop?

    Quote Originally Posted by RyuMaster View Post
    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:
    Code:
    *array + 1
    and this:
    Code:
    *(array + 1)
    Regards,

    Paul McKenzie

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #7
    Join Date
    Jul 2008
    Posts
    13

    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.

  8. #8
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: Why "char" is inside "int" loop?

    Quote Originally Posted by RyuMaster View Post
    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.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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
  •  





Click Here to Expand Forum to Full Width

Featured