String Pointers

Q

What will be printed as the result of the operation below:

          main() {
              char *ptr = " Cisco Systems";
              *ptr++; printf("%s\n",ptr);
              ptr++;
              printf("%s\n",ptr);
          }

✍: FYIcenter

A

1) ptr++ increments the ptr address to point to the next address. In the prev example, ptr was pointing to the space in the string before C, now it will point to C.

2)*ptr++ gets the value at ptr++, the ptr is indirectly forwarded by one in this case.

3)(*ptr)++ actually increments the value in the ptr location. If *ptr contains a space, then (*ptr)++ will now contain an exclamation mark.

Answer: Cisco Systems

2007-02-26, 6525👍, 0💬